pom.xml 中的子串变量
substring a variable in pom.xml
是否可以(以及如何)在 pom.xml 或使用该变量的属性中对变量进行子字符串化?
我的场景:
我有一个 swing 应用程序,它在其页脚中显示了一种版本。
该版本是从属性文件中读取的。
属性文件只有一个 maven 变量的引用,如:
version=${git.branch}
在我的 pom.xml 中,有一个插件可以查找分支名称,并将其写入 "git.branch" 变量。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/version.properties</exclude>
</excludes>
</resource>
</resources>
</build>
但是现在我们为分支名称使用前缀,这个前缀不应该与应用程序版本一起部署。
我有像这样的分支:
v123
v124
v125
现在我有这样的分支:
b_04_v123
b_04_v124
而且我希望 maven 只获取字符串 "b_04_v123" 的值 "v124",前缀是固定的,就像 "b_NN_".
注意:不,不能更改分支名称,因为其他部门通过脚本和自动化使用它们。
你可以使用org.codehaus.groovy.maven
插件:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.setProperty("version2","${version}".replace('b_04_', ''))
</source>
</configuration>
</execution>
</executions>
</plugin>
现在您可以使用版本 2 了:
version=${version2}
我在 java 代码中解决了它,我已经有一个读取属性文件的代码,只是在那里添加了子字符串。
这不是一个漂亮的 Maven 解决方案,但有效。我本可以从一开始就做到这一点。
但是,如果有人能告诉我如何在我的 pom.xml 中对变量进行子字符串化,那就太好了,尽管我不再需要它(或同样紧迫)我仍然很好奇。
我自己偶然发现了这个问题,其他提供的答案对我不起作用。所以我创建了自己的 Maven 插件。
它可以search and replace other variables or normal text using regular expressions and so create a new variable。
对于您的情况,它将是这样的:
<plugin>
<groupId>io.github.1tchy</groupId>
<artifactId>variable-search-replace-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<text>${git.branch}</text>
<search>^b_\d\d_</search>
<variableName>old.version</variableName>
</configuration>
</execution>
</executions>
</plugin>
然后你可以简单地把它当作普通的属性:version=${old.version}
该插件还可以定义替换文本:查看它的 documentation!
是否可以(以及如何)在 pom.xml 或使用该变量的属性中对变量进行子字符串化?
我的场景:
我有一个 swing 应用程序,它在其页脚中显示了一种版本。 该版本是从属性文件中读取的。 属性文件只有一个 maven 变量的引用,如:
version=${git.branch}
在我的 pom.xml 中,有一个插件可以查找分支名称,并将其写入 "git.branch" 变量。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/version.properties</exclude>
</excludes>
</resource>
</resources>
</build>
但是现在我们为分支名称使用前缀,这个前缀不应该与应用程序版本一起部署。
我有像这样的分支:
v123
v124
v125
现在我有这样的分支:
b_04_v123
b_04_v124
而且我希望 maven 只获取字符串 "b_04_v123" 的值 "v124",前缀是固定的,就像 "b_NN_".
注意:不,不能更改分支名称,因为其他部门通过脚本和自动化使用它们。
你可以使用org.codehaus.groovy.maven
插件:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.setProperty("version2","${version}".replace('b_04_', ''))
</source>
</configuration>
</execution>
</executions>
</plugin>
现在您可以使用版本 2 了:
version=${version2}
我在 java 代码中解决了它,我已经有一个读取属性文件的代码,只是在那里添加了子字符串。 这不是一个漂亮的 Maven 解决方案,但有效。我本可以从一开始就做到这一点。 但是,如果有人能告诉我如何在我的 pom.xml 中对变量进行子字符串化,那就太好了,尽管我不再需要它(或同样紧迫)我仍然很好奇。
我自己偶然发现了这个问题,其他提供的答案对我不起作用。所以我创建了自己的 Maven 插件。 它可以search and replace other variables or normal text using regular expressions and so create a new variable。 对于您的情况,它将是这样的:
<plugin>
<groupId>io.github.1tchy</groupId>
<artifactId>variable-search-replace-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<text>${git.branch}</text>
<search>^b_\d\d_</search>
<variableName>old.version</variableName>
</configuration>
</execution>
</executions>
</plugin>
然后你可以简单地把它当作普通的属性:version=${old.version}
该插件还可以定义替换文本:查看它的 documentation!