我有一个 Google App Engine 标准 Maven 项目,我使用
appengine-standard-archetype
原型创建了该项目。
我想使用
${project.version}
变量作为部署版本,但某些字符不允许使用该值:
只能包含小写字母、数字和连字符。必须开始 并以字母或数字结尾。不得超过 63 个字符。
值
0.0.1-SNAPSHOT
需要修改。然后我使用 build-helper-maven-plugin
来获取替换品
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>version-urlsafe</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>project.version.urlsafe</name>
<value>${project.version}</value>
<regex>\.</regex>
<replacement>-</replacement>
<toLowerCase>true</toLowerCase>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
和
maven-antrun-plugin
显示值
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>regex-replace-echo</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>******** Displaying value of property ********</echo>
<echo>${project.version.urlsafe}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
最后我使用新属性作为部署版本
<app.deploy.version>${project.version.urlsafe}-urlsafe</app.deploy.version>
请注意,我在值末尾添加
-urlsafe
只是为了了解为什么不考虑该值
使用
mvn appengine:deploy
运行部署,我得到此输出
...
[INFO] Executing tasks
main:
[echo] ******** Displaying value of property ********
[echo] 0-0-1-snapshot
...
gcloud.cmd app deploy --version ${project.version.urlsafe}-urlsafe
[INFO] GCLOUD: ERROR: (gcloud.app.deploy) argument --version/-v: Bad value [${project.version.urlsafe}-urlsafe]
即使 ant-run 插件正确回显了新版本,但在构建部署命令时,变量本身丢失了。
然后我尝试在部署之前强制实现
regex-property
目标,如下所示
mvn build-helper:regex-property appengine:deploy
但是在这种情况下我遇到了丢失配置错误:
[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:3.0.0:regex-property (default-cli) on project maventest: The parameters 'regex', 'name', 'value' for goal org.codehaus.mojo:build-helper-maven-plugin:3.0.0:regex-property are missing or invalid -> [Help 1]
稍微题外话: 由于之前有类似案例的经验,我决定手动运行
build-helper:regex-property
作为额外目标:一个注入新变量的插件,该变量已正确回显,但在使用该值时缺失。参考如下:无法获取git.branch属性
与插件作者合作,我们发现在 appengine 之前添加插件目标可以解决问题
mvn git-commit-id:revision appengine:deploy
。最后这个问题的根本原因是 Maven bug:https://issues.apache.org/jira/browse/MNG-6260
因此,由于插件配置错误,即使直接调用插件的解决方法也不适合这种情况。
问题如何解决?如何获取执行 appengine 部署时正确创建的
${project.version.urlsafe}
变量?
我在使用
appengine-maven-plugin
时也遇到了同样的问题。你是对的,在部署到应用程序引擎之前,你需要首先调用目标build-helper:regex-property
。
但要使其工作,您必须将配置部分移到 executions
标签之外。
这是我当前使用的完整配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<name>project.version.urlsafe</name>
<value>${project.version}</value>
<regex>\.</regex>
<replacement>-</replacement>
<toLowerCase>true</toLowerCase>
<failIfNoMatch>false</failIfNoMatch>
<fileSet/>
<source/>
</configuration>
</plugin>
然后当调用
mvn build-helper:regex-property appengine:deploy
时,一切都应该按预期工作。
我的整个pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloudfall.mountains</groupId>
<artifactId>note</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>note</name>
<description>spring boot note project</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.custom.version}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>custom-version</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>project.custom.version</name>
<value>${project.version}</value>
<regex>\.</regex>
<replacement>-</replacement>
<toLowerCase>true</toLowerCase>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun-plugin.version}</version>
<executions>
<execution>
<id>regex-replace-echo</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>******** Displaying value of property ********</echo>
<echo>${project.custom.version}</echo>
<echo>${project.name}</echo>
<echo>${project.artifactId}</echo>
<echo>${project.version}</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
所以要求项目jar名称必须有版本,但版本中不能包含点,必须用破折号字符替换点。
<version>${build-helper-maven-plugin.version}</version>
<version>${maven-antrun-plugin.version}</version>
点击这两个会进入spring-boot-dependency-3.2.3.pom:
<build-helper-maven-plugin.version>3.4.0</build-helper-maven-plugin.version>
<maven-antrun-plugin.version>3.1.0</maven-antrun-plugin.version>
那么你可以
mvn clean package
将生成 jar 为:note-0-0-1-snapshot.jar
但是我的intellij仍然在我的pom中显示错误: 无法解析符号“project.custom.version” 它是红色的,不知道为什么 IDE 无法识别它