title | layout | sidebar | permalink | folder | comments |
---|---|---|---|---|---|
Maven Package Management with Visual Studio Team Services |
page |
java |
/labs/java/mavenpmvsts/ |
/labs/java/mavenpmvsts/ |
true |
In this exercise, you are going to set up package management with Maven in VSTS. This repo contains a class library (MyShuttleCalc) that is used by the MyShuttle2 application. You will configure a VSTS build to publish the MyShuttleCalc package to a VSTS Maven Package feed so that it can be consumed by MyShuttle2 and any other applications that require the calculation code.
This exercise assumes you have completed the exercises to create a Team Project, have set up the Docker private VSTS agent, and imported the MyShuttleCalc and MyShuttle2 GitHub repos into your VSTS team project. This exercise also assumes that you have cloned the repos in either IntelliJ or Eclipse. This exercise uses a team project named jdev-labs, though your team project name may differ.
{% include note.html content="This exercise shows how to do a Maven build using VSTS. To see how to perform a Maven build using Jenkins that still integrates into the VSTS Package Feed, please refer to the Maven Package Management with Jenkins lab" %}
In this task you will create a Maven package feed. You will publish packages to this feed as well as consume packages from this feed.
-
In VSTS, click on Build & Release and then Packages to navigate to the Package Hub. Click + New Feed to create a new feed.
-
Enter Maven for the feed name and click Create.
You now have a feed that you can publish package to.
In this task you will create credentials for the Maven feed. You will then create a settings.xml file containing the credentials.
-
In the Packages Hub, make sure you have selected the Maven feed and click Connect to Feed.
-
In the left menu, click on Maven.
{% include note.html content= "At the time of writing, Maven package feeds was a preview feature. You will need to enable the Maven package feed feature in order to connect to Package Management to/from Maven." %}
-
Click Generate Maven Credentials.
-
VSTS creates a credentials snippet. Click Copy to Clipboard to copy the snippet to the clipboard.
-
In your editor, open the
MyShuttleCalc\maven\settings.xml
. -
Delete the comment
<!-- paste maven package feed credentials section here !-->
and replace it with the snippet text between the tags<servers>
and</servers>
so that the final result looks like this: -
Press Ctrl-S (or File -> Save) to save the file.
-
In VSTS, go back to the Connect to Feed dialog on your Maven feed. Click copy in the section labeled
Add this feed to your project pom.xml inside the <repositories> tag
. -
In your editor, open the
pom.xml
file. Update the<repositories>
tag as well as the<distributionManagement>
tag so that they now point to your feed. -
Commit your changes to the repo.
{% include note.html content= "If this is your first commit to VSTS, you will be prompted to update your display name and email address for the repo. These are only for display purposes but are usually matched to your VSTS profile." %}
- Click VCS -> Commit Changes to commit your changes to the repo.
-
Enter Adding maven credentials for the commit message.
-
Click the drop-down next to Commit and select Commit and Push.
{% include note.html content= "Use the personal access token (PAT)generated from the Set up a Docker Build lab that should be located at: home/vmadmin/pat.txt
. Otherwise, follow the instructions from that lab again to generate a new PAT." %}
-
In Eclipse, right click on the project or the pom.xml file and select Team -> Commit.
-
Enter Adding maven credentials for the commit message and click Commit and Push.
-
Verify that the remote location is accurate and click Next.
-
If a window prompts for credentials, use the following values:
Name Value User _VSTS_Code_Access_Token
Password {PAT that you copied earlier}
-
Important: Copy the maven settings file to the .m2 directory so that local Maven operations will succeed by running the following command in a terminal:
cp ~/MyShuttleCalc/maven/settings.xml ~/.m2/
-
Important: If you have the MyShuttle2 project already open in IntelliJ or Eclipse, close the instance of the IDE and reopen it.
In this task you will create a build that will publish the MyShuttleCalc library to the Maven feed.
-
Open VSTS and connect to your team project. Click on the Build & Release Hub and then click Builds. Click on +New to create a new build definition.
-
In the templates window, type maven into the search box. Click apply on the Maven template.
-
In the Process page, change the name of the build to MyShuttleCalc.
-
Click on Options.
-
Update the Build number format to
0.0$(rev:.r)
and update the build queue todefault
(this is the queue that the VSTS agent in the container is joined to). -
Click on Tasks. Click on the Maven task. Edit the following settings:
Field Value Notes Options --settings ./maven/settings.xml
Required to authenticate when pushing the Maven package to the feed. Goal(s) deploy -Dbuildversion=$(Build.BuildNumber)
Tell Maven to publish the package, passing in the build number Code Coverage Tool JaCoCo
Change the code coverage format Source Files Directory src/main
These files must be included in the coverage results -
Click Save and Queue. Accept the defaults to queue the build.
-
A green status bar indicates that the build has been queued. Click on the build number to view the logs in real time.
-
When the build completes successfully, you can click on the build number to view the summary. Note the test results and code coverage.
{% include note.html content= "Each time you run the build, the patch number (the last of the 3-digit version numbers) will increment. In the image above, the build has run 6 times so the latest build number is 0.0.6. The package version matches the build number because we supplied the build version number to Maven in the Maven task." %}
-
Navigate back to the Maven package feed. There you will see the MyShuttleCalc package.