javamavenbitbucket-pipelinesbitbucket-aws-code-deploy

Get the version from pom.xml from a Java Maven project in Bitbucket pipeline


Is there a way to get the version defined in the pom.xml file so it can be used with a variable in a Bitbucket pipeline?

There are similar questions here on StackOverflow but they mostly refer to Jenkins and "readMavenPom" which doesn't seem to be available in Bitbucket pipelines.

I would like to use it as the version label for the upload to S3 in the second step.

- step:
    name: Build and Test
    caches:
      - maven
    script:
      - mvn package
    artifacts:
      - target/myapp-*.jar
- step:
    name: Upload to S3
    deployment: production
    script:
      - pipe: atlassian/aws-code-deploy:0.2.10
        variables:
          AWS_DEFAULT_REGION: $AWS_REGION
          AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
          AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
          COMMAND: 'upload'
          APPLICATION_NAME: 'myapp'
          ZIP_FILE: "target/myapp-*.jar"
          S3_BUCKET: 'myapps'
          VERSION_LABEL: "myapp-${version}-${BITBUCKET_COMMIT:0:8}"

Solution

  • Based on the tip of @joe and the similar question How to get Maven project version to the bash command line, I can confirm this works on Bitbucket pipelines:

    - step:
        name: Get the version from pom.xml
        script:
          - MVN_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
          - echo $MVN_VERSION
    

    Result

    + MVN_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
    
    + echo $MVN_VERSION
    0.0.1-SNAPSHOT