Moustafa Mahmoud
Moustafa Mahmoud

Reputation: 1590

Maven can't retrieve buildNumber while package the jar

I am using a buildnumber-maven-plugin to generate a sequence number for my jar. I will use it during my CICD process.

I followed all the examples available online. However, I can get the ${buildNumber} to print it, but while packaging the jar, I can't get the number, and I got the ${buildNumber} text. I searched for Maven LifeCycle, and I found I need to add it in the validation before any other plugin. But still, I can't solve the issue.

  <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>${group.id}</groupId>
    <artifactId>${artifact.id}</artifactId>
    <version>${model.version}</version>
    <profiles>
        <profile>
            <id>scala-2.11</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>

        ......
            </dependencies>
        </profile>
    </profiles>
    <scm>
        <connection>scm:svn:http://127.0.0.1/dummy</connection>
        <developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
        <tag>HEAD</tag>
        <url>http://127.0.0.1/dummy</url>
    </scm>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <finalName>${artifact.id}.${model.version}</finalName>
        <plugins>
            <!-- buildnumber-maven-plugin for automatic increment the version number -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <buildNumberPropertiesFileLocation>${build.number.dir}</buildNumberPropertiesFileLocation>
                    <revisionOnScmFailure>no.scm.config.in.pom</revisionOnScmFailure>
                    <doCheck>true</doCheck>
                    <doUpdate>true</doUpdate>
                    <format>{0,number}</format>
                    <items>
                        <item>buildNumber</item>
                    </items>
                </configuration>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${build.number.dir}</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo>current build number is "${buildNumber}"</echo>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- ================== ;-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.source.version}</source>
                    <target>${java.source.version}</target>
                    <skipMain>true</skipMain> <!-- skip compile -->
                    <skip>true</skip> <!-- skip testCompile -->
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven.assembly.plugin.version}</version>
                <executions>
                    <execution>
                        <id>jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>
                                    src/main/assembly/assembly-jar.xml
                                </descriptor>
                            </descriptors>
                            <finalName>${artifact.id}</finalName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>bin</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>
                                    src/main/assembly/assembly-bin.xml
                                </descriptor>
                            </descriptors>
                            <finalName>${tar.name}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>



    <properties>
        <argLine>-Dfile.encoding=UTF-8 -Dlog4j.skipJansi=false -DmodelLogLevel=${modelLogLevel}</argLine>
        <modelLogLevel>DEBUG</modelLogLevel>
            <build.number.dir>${project.basedir}/buildNumber.properties</build.number.dir>
        <java.source.version>1.8</java.source.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


        <!-- Plugin Versions -->
        <maven.compiler.plugin.version>3.6.2</maven.compiler.plugin.version>
        <maven.reports.plugin.version>2.9</maven.reports.plugin.version>
        <maven.assembly.plugin.version>3.1.0</maven.assembly.plugin.version>
        <surefire.plugin.version>2.7</surefire.plugin.version>

        <group.id>com.test.model_name</group.id>
        <artifact.id>proj_abc</artifact.id>
        <build.type>snapshot</build.type>
        <major.minor.version>0.1</major.minor.version>
            <!-- here I am trying to retrieve the actual number-->
        <build.number>${buildNumber}</build.number>
        <model.version>${major.minor.version}.${build.number}.${build.type}</model.version>
    </properties>

</project>

Note: If I run validate I could found the echo message below which means I can get the number correctly from the properties file. If I run package or run I got ${artifact.id}_source-.0.1.${buildNumber}.snapshot without the number.

main:
     [echo] current build number is "90"

I also checked this similar question here & enter link description here and other links but can't figure out the problem.

Upvotes: 1

Views: 562

Answers (1)

user944849
user944849

Reputation: 14951

I think what's happening here is that Maven resolves properties near the beginning of the build process.

<build.number>${buildNumber}</build.number>
<model.version>${major.minor.version}.${build.number}.${build.type}</model.version>

When it resolves build.number, ${buildNumber} does not have a value. So, it leaves the variable name unchanged. And I suspect you'd have the same problem if you modified model.version to this:

<model.version>${major.minor.version}.${buildNumber}.${build.type}</model.version>

for the same reason.

You can try using the build-helper-maven-plugin as described in this answer. However, this still may not work since model.version is used as the value of the project.version, which is resolved quite early as it's part of the GAV coordinates. It's worth a try though.

Upvotes: 1

Related Questions