SJunejo
SJunejo

Reputation: 1336

Maven using $${...} for literal instead of actual property value does not work

I have a pom.xml and using ant-run plugin I am trying to replace following in my pom.xml;

From:
     <version>${current.build.version}</version>
To:
     <version>DEV.0.0-SNAPSHOT</version>

I tried the same with google replacer plugin as well as now maven-ant run plugin both resolves into actual value of the property instead of the literal.

Full POM.xml is as follows;

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>x.y.z</groupId>
        <artifactId>parent</artifactId>
        <version>${current.build.version}</version>
    </parent>
    <properties>
       <!-- Build Version for Entire Stream -->
        <current.build.version>DEV.0.0-SNAPSHOT</current.build.version>

       <!-- Replacement property while installing poms for child projects -->
        <parent.version.property.name>current.build.version</parent.version.property.name>
    </properties>

    <artifactId>child</artifactId>
    <version>${current.build.version}</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
          <!-- Replace parent version from property to actual version just before install -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <!-- Replace parent version if its a property to respective version -->
                    <execution>
                        <id>replace-parent-property-to-version</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <exportAntProperties>true</exportAntProperties>
                            <target name="replace-parent-property-to-version">
                                <!-- Get some Ant-Contrib task def -->
                                <taskdef resource="net/sf/antcontrib/antlib.xml">
                                    <classpath refid="maven.plugin.classpath" />
                                </taskdef>
                                <!-- Copy original file to temp location first -->
                                <var name="orig.project.file" value="${basedir}/pom.xml" />
                                <var name="temp.project.file.name" value="versioned-pom.xml" />
                                <var name="temp.project.file" value="${project.build.directory}/${temp.project.file.name}" />
                                <copy file="${orig.project.file}" tofile="${temp.project.file}" />

                                <!-- Replace the parent version from property to actual value -->
                                <var name="dollar" value="$${" />
                                <var name="curlBrace" value="}" />
                                <var name="var.parent.version.property.name" value="${dollar}${parent.version.property.name}${curlBrace}" />
                                <replace file="${temp.project.file}" token="&lt;version&gt;${var.parent.version.property.name}&lt;/version&gt;"
                                    value="&lt;version&gt;${project.version}&lt;/version&gt;" />

                                <!-- Now overwrite project file property with update temp file for release -->
                                <var name="project.file" unset="true" />
                                <property name="project.file" value="${temp.project.file}" />
                                <echo message="Parent version updated for release in file ${project.file}..." />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

If you see my configuration I already $$ which also resolves into property value instead of ${...}. I am unable to solve this.

Also is there a property in maven form which I can access pom.xml file location. I found in the document its called project.file but I am unable to access it in ant-run plugin. Its says not set.

Upvotes: 1

Views: 1070

Answers (1)

Yiannis Dermitzakis
Yiannis Dermitzakis

Reputation: 488

You should not do that, it is an anti-pattern. I'm sure you already see the Warning from Maven when you try to build.

It might be tempting to do it like that but it results in non-repeatable builds, so it is discouraged. You should instead always keep a literal (e.g. "DEV.1.0-SNAPSHOT") value there and update its value with the maven versions plugin instead.

If you only have one version for the whole project, and the parent doubles as a module pom (another common -but tolerated- antipattern), you can declare the parent's version in its pom, the version of the parent again in the parent section of any children and skip the child version. This way you execute

mvn versions:set -DnewVersion=DEV.1.0

on the module project when it's time to release.

You commit, do the release, then execute

mvn versions:set -DnewVersion=DEV.1.1-SNAPSHOT

to resume development..

Upvotes: 1

Related Questions