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