在Maven中有条件地设置属性

ux6nzvsh  于 2023-03-29  发布在  Maven
关注(0)|答案(3)|浏览(155)

我想在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-condition。

avwztpqn

avwztpqn1#

要根据快照生成(或其他条件)将该属性设置为两个不同的值,可以使用以下命令:

<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>

gkl3eglg

gkl3eglg2#

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

huwehgph3#

如果你的版本号是一个发布版本,如1.2.3,maven将使用发布仓库,如果版本是一个SNAPSHOT版本,如'1.2.3-SNAPSHOT`,maven将使用快照仓库。

相关问题