Search code examples
mavenintellij-ideagoogle-cloud-endpointsgoogle-visionapp-engine-flexible

google vision api failed imports in intelliJ


i am using intelliJ IDE and tried https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/vision/label

my pom.xml shows no errors and looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>
  <version>1.0-SNAPSHpOT</version>
  <groupId>com.example.endpoints</groupId>
  <artifactId>endpoints</artifactId>

  <parent>
    <artifactId>doc-samples</artifactId>
    <groupId>com.google.cloud</groupId>
    <version>1.0.0</version>
    <relativePath>../..</relativePath>
  </parent>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.war.plugin>2.6</maven.war.plugin>

    <appengine.maven.plugin>1.0.0</appengine.maven.plugin>
    <jetty.maven.plugin>9.3.8.v20160314</jetty.maven.plugin>

    <failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-vision</artifactId>
      <version>v1-rev347-1.22.0</version>
    </dependency>
      <dependency>
          <groupId>com.google.api-client</groupId>
          <artifactId>google-api-client</artifactId>
          <version>1.22.0</version>
          <exclusions>
              <exclusion> <!-- exclude an old version of Guava -->
                  <groupId>com.google.guava</groupId>
                  <artifactId>guava-jdk5</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>20.0</version>
      </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
    <!--  Gson: Java to Json conversion -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.6.2</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <!-- for hot reload of the web application -->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
    <plugins>
      <plugin>  <!-- TEMPORARY -->
        <groupId>com.google.appengine</groupId>
        <artifactId>gcloud-maven-plugin</artifactId>
        <version>2.0.9.121.v20160815</version>
      </plugin>
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>${appengine.maven.plugin}</version>
        <configuration>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven.war.plugin}</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty.maven.plugin}</version>
      </plugin>
    </plugins>
  </build>
</project>

However, the java class fails to resolve dependencies for

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.vision.v1.Vision;
import com.google.api.services.vision.v1.VisionScopes;
import com.google.api.services.vision.v1.model.AnnotateImageRequest;
import com.google.api.services.vision.v1.model.AnnotateImageResponse;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse;
import com.google.api.services.vision.v1.model.EntityAnnotation;
import com.google.api.services.vision.v1.model.Feature;
import com.google.api.services.vision.v1.model.Image;
import com.google.common.collect.ImmutableList;

i have tried mvn -U clean install

but no luck


Solution

  • Since you're using the Flex environment. I would suggest the following

    (1) Follow the Quickstart for Flex in Java to ensure that you are setup correctly: https://cloud.google.com/appengine/docs/flexible/java/quickstart

    • test locally first (using jetty, as the quickstart shows)
    • deploy to App Engine Flex afterwards if you'd like

    (2) Then, once you know everything works (at least locally with the jetty server), you can then add to your pom.xml the following

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vision</artifactId>
        <version>0.9.4-beta</version>
    </dependency>
    

    This comes from this page: https://cloud.google.com/vision/docs/reference/libraries#client-libraries-install-java

    where you'll also find a code example.

    (3) BTW: for Auth, I would suggest setting up a Service Account locally on your machine. For that, you need to create a Service Account in the Cloud Console, you then download a .json file locally, and then setup the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to that .json file.