在pom.xml中将变量子字符串化

res*_*res 3 build version pom.xml maven

是否有可能(以及如何)在pom.xml或使用此变量的属性中为变量添加子字符串?

我的情况:

我有一个swing应用程序,在其页脚中显示了一种版本。此版本是从属性文件中读取的。该属性文件仅具有maven变量的引用,例如:

version=${git.branch}
Run Code Online (Sandbox Code Playgroud)

在我的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>
Run Code Online (Sandbox Code Playgroud)

但是现在我们在分支名称中使用前缀,并且该前缀不应与应用程序版本一起部署。

我有类似的分支:

v123
v124
v125
Run Code Online (Sandbox Code Playgroud)

现在我有类似的分支:

b_04_v123
b_04_v124
Run Code Online (Sandbox Code Playgroud)

而且我希望Maven仅获取字符串“ b_04_v123”的值“ v124”,前缀是固定的,就像“ b_NN_”一样。

注意:否,无法更改分支名称,因为其他部门将它们与脚本和自动化一起使用。

Azz*_*hem 5

您可以使用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>
Run Code Online (Sandbox Code Playgroud)

现在您可以使用version2:

version=${version2}
Run Code Online (Sandbox Code Playgroud)

这是GitHub中的一个例子


res*_*res 3

我在java代码中解决了这个问题,我已经有一个读取属性文件的代码,只是在那里添加了子字符串。这不是一个漂亮的 Maven 解决方案,但有效。我从一开始就可以这样做。但是,如果有人能告诉我如何在 pom.xml 中对变量进行子串化,那就太好了,尽管我不再需要它(或具有同样的紧迫性),但我仍然很好奇。