How do I externally set a version for my jar without a parent pom that produces a usable pom.xml for dependencies

892 views Asked by At

How do I externally define the version tag in pom.xml for a jar project, with no parent pom?

I've used properties-maven-plugin and flatten-maven-plugin to get real close. The resulting pom.xml that gets deployed has an unusable version because of the value substitutions being used by properties-maven-plugin to set version. flatten-maven-plugin resolves issues with dependency versions, but does not seem to resolve the version of the primary artifact. The code does appear to build and name the jar correctly.

I looked into https://maven.apache.org/maven-ci-friendly.html and learned about ${revision}, but the examples there seem to include parent poms.

I'm hoping someone has come across a solution without use of a parent pom. If there's no solution, I'd like to consider this a feature request for one of the plugins, but I'd like to try here first before I submit such a request.

This is the simplest configuration I can provide to demonstrate my issue.

pom.xml

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

    <groupId>testgroup</groupId>
    <artifactId>test</artifactId>
    <version>${revision}</version>
    <packaging>jar</packaging>

    <name>test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <lang>3.9</lang>
        <revision>${major}.${minor}</revision>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${lang}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <id>pre</id>
                        <phase>pre-clean</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>test.properties</file>
                            </files>
                        </configuration>
                    </execution>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>test.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.1.0</version>
                <configuration>
                    <flattenMode>defaults</flattenMode>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

test.properties

major=1
minor=2

src/main/java/testgroup/test/App.java

package testgroup.test;

import org.apache.commons.lang3.StringUtils;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println(StringUtils.chop("boo"));

    }
}

Resulting .flattened-pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>testgroup</groupId>
  <artifactId>test</artifactId>
  <version>${major}.${minor}</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.9</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

I expect the version in .flattened-pom.xml to be 1.2, not ${major}.${minor}

So, I'm not sure if this is something I'm doing wrong, or some kind of bug in one of the plugins. For example, even Maven displays this weirdly:

C:\Users\Robert\eclipse-workspace\test>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< testgroup:test >---------------------------
[INFO] Building test ${major}.${minor}
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (pre) @ test ---

< snip >

[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test ---
[INFO] Building jar: C:\Users\Robert\eclipse-workspace\test\target\test-1.2.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
2

There are 2 answers

0
kostia On

I don't know if you managed to find a way to resolve this, but from what I see, you are correctly setting the $revision property to the version element.

But do you run the tests? In order to build/test the application you need to run it like this:

mvn clean flatten:flatten test

or

mvn clean flatten:flatten package

What I find a bit off is that you are using $major and $minor without having them initialized in your pom.xml as properties and not using properties instead.

I understand that you would like to use the properties file which I assume you inject it somehow in your test environment. Instead you could use properties <major> and <minor> to initialize them and when you want to change them you can use

mvn clean -Dmajor=1 -Dminor=3 flatten:flatten test

If you have found some other way to resolve this, please share it with the rest of us :)

0
conderls On
  1. upgrade maven >= 3.5.0
  2. change goal <phase>process-resources</phase> to <phase>package</phase>

it will solve the issue.

see also: Maven CI Friendly Versions