Mike S.
Mike S.

Reputation: 195

Getting Maven Project version within Github Actions

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>

Upvotes: 5

Views: 11092

Answers (4)

Doston
Doston

Reputation: 637

Most of the above answer worked for me only when I have used it non-reusable workflows.

But any of the above worked for me in reusable workflows. So I got it working with custom action and it very fast since it queries the single file with xml-js:

- name: Extract version from pom.xml file
    id: version-extractor
    uses: dostonhamrakulov/[email protected]
    with:
      file_path: ${{ github.workspace }}/pom.xml
- name: Get the output version
  run: echo "The found version ${{ steps.version-extractor.outputs.version }}"

Upvotes: 0

Eduardo Sanchez-Ros
Eduardo Sanchez-Ros

Reputation: 1827

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

Upvotes: 3

jonashackt
jonashackt

Reputation: 14429

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

Upvotes: 6

Mike S.
Mike S.

Reputation: 195

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

Upvotes: 4

Related Questions