有条件地在 maven 中设置属性

Ren*_*ler 9 java maven

我想根据它是否是快照构建在 Maven 中有条件地设置一个属性。伪代码如下

if ${project.version} ends with "-SNAPSHOT"     
then
   deployFileUrl = ${project.distributionManagement.snapshotRepository.url}
else
   deployFileUrl = ${project.distributionManagement.repository.url}
Run Code Online (Sandbox Code Playgroud)

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

这种方法的问题是我无法实现 else 条件。

Ren*_*ler 9

最后我最终使用了maven-antrun-plugin,如下所示

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>validate</phase>
            <configuration>
                <exportAntProperties>true</exportAntProperties>
                <target>
                    <condition property="isSnapshot">
                        <contains string="${project.version}" substring="SNAPSHOT" />
                    </condition>
                    <condition property="deployFileUrl"
                        value="${project.distributionManagement.snapshotRepository.url}">
                        <isset property="isSnapshot" />
                    </condition>
                    <!-- Properties in ant are immutable, so the following assignments 
                        will only take place if deployFileUrl is not yet set. -->
                    <property name="deployFileUrl"
                        value="${project.distributionManagement.repository.url}" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)


小智 5

要将属性设置为两个不同的值取决于快照构建(或其他条件),您可以使用以下内容:

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

它将dockerTag属性设置为devlatest取决于构建类型。

如果你想改变的属性值是应分两个places.For例如对于更改truefalse使用 <value>${project.version} true false</value><regex>(?=.*SNAPSHOT).*(true).*|.*(false).*</regex>