4

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}"
Frank
  • 5,741
  • 4
  • 28
  • 49
  • 1
    Does this answer your question? [Extract version ID from POM in a Jenkins pipeline](https://stackoverflow.com/questions/37603619/extract-version-id-from-pom-in-a-jenkins-pipeline) – Sambit Feb 23 '21 at 16:29
  • As far as I can find, readMavenPom is Jenkins specific – Frank Feb 23 '21 at 20:36
  • 1
    What about this one: [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/26514030#26514030) – Joe Feb 23 '21 at 22:24
  • thanks Joe, indeed that could be used on Bitbucket, will post the answer – Frank Feb 24 '21 at 15:05

1 Answers1

8

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
Frank
  • 5,741
  • 4
  • 28
  • 49