【问题标题】:Google App Engine - parameters in pom.xml not read in appengine-web.xmlGoogle App Engine - pom.xml 中的参数未在 appengine-web.xml 中读取
【发布时间】:2018-07-28 02:47:01
【问题描述】:

尊敬的 Stackoverflow 社区,

我想做的事: 使用 Eclipse,结合使用 GoogleAppEngine 和 Google CloudSQL 连接到数据库并打印结果

我正在学习哪个教程: 这里是“使用 Cloud SQL for MySQL”教程: https://cloud.google.com/appengine/docs/standard/java/cloud-sql/using-cloud-sql-mysql

到目前为止我取得了什么成就: 当我硬编码用户、将数据库传递到 appengine-web.xml 时,我可以让它工作并从我的数据库中打印出值:

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

然后,在 HelloAppEngine.java 中,使用 System.getProperty("cloudsql") 获取此 url:

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"));
}

虽然我不明白: 根据教程,我不必在 appengine-web.xml 中硬编码用户、传递和数据库值,而是将它们放在 pom.xml 文件中,然后使用 ${INSTANCE_CONNECTION_NAME} 从那里“获取”这些值, /${数据库},用户=${用户}和密码=${密码}。

我的 pom.xml 文件包含以下信息:

 <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>

还有:

<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>

那么,最后一个问题: 我错过了什么,在我的 appengine-web.xml 中使用 ${placeholder} 不起作用,而立即在其中硬编码必要的信息确实有效?

如果有任何帮助,我将不胜感激:)

【问题讨论】:

    标签: maven google-app-engine pom.xml google-cloud-sql


    【解决方案1】:

    好的,我找到了解决方案: 当您添加新的 Google App Engine 标准应用时,pom 文件包含以下部分:

    <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 显示警告:“此应用程序引擎 mavin 插件已被弃用”,这让我很怀疑。

    查看google链接的完整pom.xml文件: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/appengine-java8/cloudsql/pom.xml

    我看到他们的插件部分看起来不同:

     <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>
    

    我没有看这部分,因为它没有在教程主页上明确提及: https://cloud.google.com/appengine/docs/standard/java/cloud-sql/using-cloud-sql-mysql

    用这个插件部分替换我的插件部分就可以了。我猜的部分在哪里

     <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>
    

    是负责的。所以,问题解决了:)

    【讨论】:

      猜你喜欢
      • 2013-09-23
      • 2011-08-11
      • 2012-11-13
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多