java - 在 Maven 中有条件地设置属性

标签 java maven

我想在 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)devlatest取决于构建类型。

如果您想更改属性值,应该在两个地方进行更改。例如 truefalse使用 <value>${project.version} true false</value><regex>(?=.*SNAPSHOT).*(true).*|.*(false).*</regex> .

关于java - 在 Maven 中有条件地设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002807/

相关文章:

java - 为什么在一般情况下,冒泡排序比选择排序表现更好

java - 生成数组类型,例如 Object[].class

java - Apache Ignite 频繁缓存关闭异常

java - 通过访问 token 对 REST 服务进行用户授权

java - eclipse 中的 Maven java 项目看不到同一个包中的类

java - 在 Maven 中添加外部 JAR 会导致 NoClassDefFoundError

java - 如何消除 web.xml 中的硬编码值

spring - org.springframework.web.bind.annotation.RequestMapping 缺少什么依赖项?

java - 创建 ListView 时出现运行时错误

Maven 资源插件资源的平面副本