4

I'm trying to fetch the version of my maven project as part of of the deployment process, but I seem to be getting an error on the command's output. Any ideas?

I have Maven help in my pom.xml plugins.

Here's the step I'm running:

 - name: Get version
        run: |
          VERSION=$( mvn help:evaluate -Dexpression=project.version -q -DforceStdout )
          echo "::set-output name=version::$VERSION"
        id: get_version

Here's the output (note: I've removed -q here so I can see the output). Note that the project that is defaulted is what I'm looking for. I'm trying to get the v0.1 as my output!

[INFO] No artifact parameter specified, using 'com.xyz:abc-123:war:v0.1' as project.
[INFO] 
null object or invalid expression
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  13.569 s
[INFO] Finished at: 2020-08-29T13:52:22Z
[INFO] ------------------------------------------------------------------------

I am using the help 3.2.0

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-help-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
Mike S.
  • 185
  • 2
  • 9
  • Which version of the maven-help-plugin have you configured to use? Apart from that why using such a strange version `v1.0` instead of `1.0` ? – khmarbaise Aug 29 '20 at 14:36
  • 1
    Maybe try using something like this instead https://stackoverflow.com/a/26514030 of the plugin – Edward Romero Aug 29 '20 at 15:51
  • 1
    Does this answer your question? [How to get Maven project version to the bash command line](https://stackoverflow.com/questions/3545292/how-to-get-maven-project-version-to-the-bash-command-line) – Edward Romero Aug 30 '20 at 16:32
  • Ed -- Your first link ended up working as well as the command I originally had. Ultimately it came down to incorrect ordering in the script. – Mike S. Aug 31 '20 at 13:03

3 Answers3

4

Turns out the issue wasn't the command, but more in my ordering. I didn't originally paste this in the first comment, but I have a step that requires manual installation of jars (yes, these should be posted to some sort of internal package manager at some point...).

Once I put the manual library step BEFORE the version step, everything executed smoothly.

      - name: Install manual libraries
        run: |
          mvn install:install-file -Dfile=lib/xyz.jar -DgroupId=com.xyz-DartifactId=xyz -Dversion=8.3.0 -Dpackaging=jar

      - name: Get version
        run: |
          VERSION=$( mvn help:evaluate -Dexpression=project.version -q -DforceStdout )
          echo "::set-output name=version::$VERSION"
        id: get_version
Mike S.
  • 185
  • 2
  • 9
4

There's also this approach using the mvn exec goal and the set-output variable definition:

  - name: Extract Maven project version
    run: echo ::set-output name=version::$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
    id: project

  - name: Show extracted Maven project version
    run: echo ${{ steps.project.outputs.version }}
jonashackt
  • 12,022
  • 5
  • 67
  • 124
2

If you only need to use the version on the next steps of the job, then you can set it as an environment variable on GITHUB_ENV and access it via ${{ env.var_name }}. For example:

  - name: Set Release version env variable
    run: |
      echo "RELEASE_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_ENV

  - name: Create Release
    id: create_release
    uses: actions/create-release@v1
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    with:
      tag_name: ${{ env.RELEASE_VERSION }}
      release_name: Release ${{ env.RELEASE_VERSION }}
      draft: false
      prerelease: false
Eduardo Sanchez-Ros
  • 1,777
  • 2
  • 18
  • 30