Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditonally set property in maven

Tags:

java

maven

I want to set a property conditionally in maven depending on whether it is a snapshot build or not. The pseudocode looks as follows

if ${project.version} ends with "-SNAPSHOT"     
then
   deployFileUrl = ${project.distributionManagement.snapshotRepository.url}
else
   deployFileUrl = ${project.distributionManagement.repository.url}

How can I implement this in maven?

I tried it with the build-helper-maven-plugin as follows

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

The problem with this approach is that I can not implement the else-condition.

like image 480
René Winkler Avatar asked May 16 '17 13:05

René Winkler


3 Answers

To set the property to two different values depends on a snapshot build (or another condition) you can use the following:

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

It set dockerTag property to dev or latest depends on build type.

If you wants to change property values is should change it in two places.For example for true and false use <value>${project.version} true false</value> and <regex>(?=.*SNAPSHOT).*(true).*|.*(false).*</regex>.

like image 76
foal Avatar answered Oct 19 '22 22:10

foal


Finally I ended up using the maven-antrun-plugin as follows

<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>
like image 43
René Winkler Avatar answered Oct 19 '22 23:10

René Winkler


The whole thing is superflous cause that will be handled by Maven automatically. If you have a version number which is a release version like 1.2.3 maven will use the release repository and if the version is a SNAPSHOT version like '1.2.3-SNAPSHOT` maven uses the snapshot repository.

like image 1
khmarbaise Avatar answered Oct 20 '22 00:10

khmarbaise