Google App Engine - parameters in pom.xml not read

2019-07-22 19:10发布

问题:

Dear Stackoverflow community,

what I'd like to do: working with Eclipse, use GoogleAppEngine in combination with Google CloudSQL to connect to a database and print out results

which tutorial am I following: It's the "using Cloud SQL for MySQL" tutorial right here: https://cloud.google.com/appengine/docs/standard/java/cloud-sql/using-cloud-sql-mysql

what have I achieved so far: I can make it work and print out values from my database when I hardcode user, pass and database into appengine-web.xml:

<system-properties>
    <property name="cloudsql" value="jdbc:google:mysql://XXXYYYZZZ:europe-west3:XXXYYYZZZ/Database?user=root&amp;password=12345" />
  </system-properties>

and then, in HelloAppEngine.java, get this url with System.getProperty("cloudsql"):

final String selectSql = "SELECT favID from favs";

PrintWriter out = resp.getWriter();
resp.setContentType("text/plain");


String url = System.getProperty("cloudsql");
try (

    Connection conn = DriverManager.getConnection(url); 
    ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {

    out.print("Last 10 visitsss:\n");
    while (rs.next()) {
      out.println(rs.getString("favID"));
    }

} catch (SQLException e) {
    out.println("fehlerddddd: " + e);
    out.println(System.getProperty("cloudsql"));
}

What I dont understand though: According to the tutorial, I dont have to hardcode user, pass and database values in the appengine-web.xml, but rather have them in a pom.xml file and then "fetch" those values from there by using ${INSTANCE_CONNECTION_NAME}, /${database}, user=${user} and password=${password}.

My pom.xml file contains these information:

 <properties>
<appengine.maven.plugin.version>1.3.2</appengine.maven.plugin.version>
<appengine.api.sdk.version>1.9.62</appengine.api.sdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
<INSTANCE_CONNECTION_NAME>XXXYYYZZZ:europe-west3:faverdb</INSTANCE_CONNECTION_NAME>
<user>root</user>
<password>12345</password>
<database>XXYYZZ</database>

and also:

<dependency> <!-- Only used locally -->
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.42</version>  <!-- v5.x.x is for production, v6.x.x EAP X DevAPI -->
</dependency>

<dependency>
  <groupId>com.google.cloud.sql</groupId>
  <!-- If using MySQL 6.x driver, use mysql-socket-factory-connector-j-6 instead -->
  <artifactId>mysql-socket-factory</artifactId>
  <version>1.0.5</version>
</dependency>

So, the final question: What am I missing that using ${placeholder} in my appengine-web.xml does not work while hardcoding the necessary information in there right away does work?

I'd appreciate any help :)

回答1:

Ok, I found the solution: When you add a new Google App Engine standard app, the pom file contains the following part:

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>versions-maven-plugin</artifactId>
    <version>2.3</version>
    <executions>
      <execution>
        <phase>compile</phase>
        <goals>
          <goal>display-dependency-updates</goal>
          <goal>display-plugin-updates</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>appengine-maven-plugin</artifactId>
    <version>${appengine.maven.plugin.version}</version>
  </plugin>

  <plugin>
       <groupId>com.google.appengine</groupId>
       <artifactId>appengine-maven-plugin</artifactId>
       <version>1.9.62</version>
    </plugin>


</plugins>

Eclipse shows a warning: "This app engine mavin plugin has been deprecated" which made me suspicious.

Looking at the complete pom.xml file linked by google: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/appengine-java8/cloudsql/pom.xml

I saw that their plugin section looks different:

 <plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
      <webResources>
        <!-- in order to interpolate version from pom into appengine-web.xml -->
        <resource>
          <directory>${basedir}/src/main/webapp/WEB-INF</directory>
          <filtering>true</filtering>
          <targetPath>WEB-INF</targetPath>
        </resource>
      </webResources>
    </configuration>
  </plugin>

  <plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>appengine-maven-plugin</artifactId>
    <version>1.3.1</version>
    <configuration>
      <deploy.promote>true</deploy.promote>
      <deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
    </configuration>
  </plugin>

I did not look at this part since its not explicitely mentioned on the tutorials main page: https://cloud.google.com/appengine/docs/standard/java/cloud-sql/using-cloud-sql-mysql

Replacing my plugin section with this plugin section did the trick. I guess the part where

 <webResources>
    <!-- in order to interpolate version from pom into appengine-web.xml -->
    <resource>
      <directory>${basedir}/src/main/webapp/WEB-INF</directory>
      <filtering>true</filtering>
      <targetPath>WEB-INF</targetPath>
    </resource>
  </webResources>

is placed is responsible. So, problem solved :)