在maven中设置财产

时间:2017-05-16 13:25:21

标签: 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条件。

3 个答案:

答案 0 :(得分:2)

整个事情是由Maven自动处理的超级原因。如果您的版本号是1.2.3版本,则maven将使用版本库,如果版本是SNAPSHOT版本,例如&#39; 1.2.3-SNAPSHOT`,则maven使用快照库。

答案 1 :(得分:1)

最后我最终使用了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>

答案 2 :(得分:0)

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

            <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属性设置为devlatest

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