我想在 Maven 中有条件地设置一个属性,具体取决于它是否是快照构建。伪代码如下所示
if ${project.version} ends with "-SNAPSHOT"
then
deployFileUrl = ${project.distributionManagement.snapshotRepository.url}
else
deployFileUrl = ${project.distributionManagement.repository.url}
我如何在 maven 中实现它?
我用 build-helper-maven-plugin 试了一下,如下所示
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>regex-properties</id>
<goals>
<goal>regex-properties</goal>
</goals>
<configuration>
<regexPropertySettings>
<regexPropertySetting>
<name>deployFileUrl</name>
<value>${project.version}</value>
<regex>.*-SNAPSHOT</regex>
<replacement>${project.distributionManagement.snapshotRepository.url}</replacement>
<failIfNoMatch>false</failIfNoMatch>
</regexPropertySetting>
</regexPropertySettings>
</configuration>
</execution>
</executions>
</plugin>
这种方法的问题是我无法实现 else 条件。
最佳答案
要根据快照构建(或其他条件)将属性设置为两个不同的值,您可以使用以下方法:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<!-- sets the only.when.is.snapshot.used property to true
if SNAPSHOT was used, to the project version otherwise -->
<id>build-helper-regex-is-snapshot-used</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>dockerTag</name>
<value>${project.version} dev latest</value>
<regex>(?=.*SNAPSHOT).*(dev).*|.*(latest).*</regex>
<replacement>$1$2</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
它设置了dockerTag
属性(property)dev
或 latest
取决于构建类型。
如果您想更改属性值,应该在两个地方进行更改。例如 true
和 false
使用
<value>${project.version} true false</value>
和 <regex>(?=.*SNAPSHOT).*(true).*|.*(false).*</regex>
.
关于java - 在 Maven 中有条件地设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002807/