Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I use my java program's Maven POM app version in my bitbucket pipelines build?

Anthony Carella
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 19, 2024

 

I have a Spring Boot Maven Java project. My POM file has a version number property in it of 1.2.3, as seen below:

<project ...>
  <modelVersion>#.#.#</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>myapp</artifactId>
  <version>1.2.3</version>
  <packaging>jar</packaging>
...
</project>

I am trying to use Bitbucket pipelines to deploy my project to Elastic Beanstalk. This is part of what my bitbucket-pipelines.yml file in my project looks like:

pipelines:
  custom:
    build and deploy develop branch:
      - step:
          name: Build and Test
          caches:
              - maven
          script:
              - mvn --version
              - mvn -B verify --file pom.xml
          artifacts:
              - target/my-app-1.2.3.zip
      - step:
          name: Get the version from pom.xml
          script:
            - APP_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
            - echo $APP_VERSION
      - step:
          name: Deploy to QA
          script:
            - echo "Deploying develop branch to QA environment"
            - echo 'testing-$APP_VERSION'
            - pipe: atlassian/aws-elasticbeanstalk-deploy:#.#.#
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                AWS_DEFAULT_REGION: 'us-east-1'
                APPLICATION_NAME: 'my-app'
                ENVIRONMENT_NAME: 'my-app-qa'
                ZIP_FILE: 'target/my-app-1.2.3.zip'
                S3_BUCKET: 'elasticbeanstalk-us-east-1-123456789012'
                VERSION_LABEL: '1.2.3'

I want to replace the hard-coded 1.2.3 with the version number in the POM so that when I change the version number in the pom.xml, it will be reflected in my deployment. I tried doing this by setting the APP_VERSION variable above. However, when I run the manual build, that variable does not seem to be working when I try to echo it or if I try to replace 1.2.3 with $APP_VERSION in the "Deploy to QA" step.

Running APP_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec) then running echo $APP_VERSION in my terminal successfully displays 1.2.3. It just doesn't work in my Bitbucket pipelines build script.

How can I get the version number in my project's POM and use it when deploying with Bitbucket pipelines to Elastic Beanstalk?

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 23, 2024

Hi Anthony and welcome to the community!

Do you see the value of the variable when you run echo $APP_VERSION during the second step?

Every step in a Pipelines build runs in a separate Docker container. For the first step of your Pipelines build, a Docker container starts, the repo gets cloned in this container, and then the commands of that step's script get executed. When the step is finished, the container gets destroyed. For the second step of your build, the same process is repeated.

Variables you define in one step will not be available in the next steps because the Docker container where the variable was defined is destroyed when the step is finished. The only way to share data between steps is via artifacts files:

So, you could save the variable definition in a file and then source this file in the third step.

However, a simpler solution is to remove the second step completely and define the variable in the step that needs it. Right now you have a second step that spins up a new Docker container, clones the repo, and then does nothing else other than defining a variable.

Please also be mindful that the command

- echo 'testing-$APP_VERSION'

is not going to show the variable value even if you define the variable in that third step, because you are using single quotes in the echo command. You will need to use double quotes for the variable to get substituted.

Single quotes are fine in the pipe's variables if you include $APP_VERSION in the VERSION_LABEL variable, but the echo command needs double quotes.

Please feel free to let me know how it goes and if you have any questions.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events