Get started with Maven packages in Azure DevOps Services and TFS

Azure DevOps Services | TFS 2018

Before you start

This quickstart assumes you've already set up Azure Artifacts. You can check out how to license the extension in the License Azure Artifacts guide.

Azure Artifacts is an extension that comes pre-installed on TFS 2017 or newer (Maven is only available in 2018 or newer), if it was removed from your organization, you can install it from the Marketplace page for Azure Artifacts.

Prerequisites

  1. Apache Maven installed. You can download it from the Apache Maven site.

  2. Have Azure Artifacts installed in your organization.

Create a feed

Already have a feed? Skip to the next step.

A feed is a container for packages. You consume and publish packages through a particular feed.

  1. Go to Azure Artifacts:

    Go to Azure Artifacts

  2. Select + New feed:

    New feed button

  3. In the dialog box:

    • Give the feed a name.
    • Choose who can read and contribute (or update) packages in your feed.
    • Choose the upstream sources for your feed.
    • When you're done, select Create.

    Most of the default settings work great for most feed users. Making your feed organization visible means you can share a single source of packages across your entire team. Enabling upstream sources to public sources makes it easy to use your favorite OSS packages. Enabling upstream sources can also give you more protection against outages and corrupted or compromised packages.

    Note

    There are some important things to consider when publishing packages that involve upstream sources. Check out the documentation on overriding a package from an upstream source for more information.

    New feed dialog box

  1. Go to the Packages page:

    Go to Azure Artifacts

  2. Select + New feed:

    New feed button

  3. In the dialog box:

    • Give the feed a name.
    • Choose who can read and contribute (or update) packages in your feed.
    • Choose the upstream sources for your feed.
    • When you're done, select Create.

    Most of the default settings work great for most feed users. Making your feed organization visible means you can share a single source of packages across your entire team. Enabling upstream sources to public sources makes it easy to use your favorite OSS packages. Enabling upstream sources can also give you more protection against outages and corrupted or compromised packages.

    Note

    There are some important things to consider when publishing packages that involve upstream sources. Check out the documentation on overriding a package from an upstream source for more information.

    New feed dialog box

You can change these settings later by editing the feed.

Set up authentication

To talk to Azure DevOps Services feeds, you'll need a token on your local machine that Maven can pick up and pass to Azure DevOps Services.

  1. From the Azure Artifacts page, select Connect to Feed.

    Connect to feed button on the upper right of the page

  1. From the Packages page, select Connect to Feed.

    Connect to feed button on the upper right of the page

  1. Open the Maven tab, select Generate Maven credentials, and copy the generated credentials.

    • Maven pulls credentials from your settings.xml file.

    • On Linux, the file path is usually "${user.home}/.m2/settings.xml".

    • On macOS, the file path is usually "~/.m2/settings.xml".

    • On Windows, the file path is usually "%USERPROFILE%/.m2/settings.xml".

    • If the file doesn't exist, create one now.

  2. Inside the <settings> and <servers> elements in the file, paste the credentials that you copied in Step 3.

Sample settings.xml:

<settings>
  <servers>
    <!-- Copy this section from the Maven section of the "Connect to Feed" dialog" -->
    <server>
      <id>dev.azure.com-yourFeedName</id>
      <configuration>
        <httpHeaders>
          <property>
            <name>Authorization</name>
            <!--The generated token expires on or before 7/19/2017-->
            <value>Basic Y2Fqb...</value>
          </property>
        </httpHeaders>
      </configuration>
    </server>
  </servers>
</settings>

Important

The <id> tags in the settings.xml and the pom.xml must match.

You can find more information about the settings.xml file in the settings.xml reference.

Publish an artifact

Publish Maven artifacts to a feed in Azure Artifacts to share them with your team and organization.

To publish a Maven artifact, you'll need to have a Maven artifact to publish on your local machine. If you don't have one, you can generate one by running the following command:

mvn -B archetype:generate -DarchetypeGroupId="org.apache.maven.archetypes" -DgroupId="MyGroup" -DartifactId="myFirstApp"
  1. Set up the Maven client with your feed.

  2. Navigate to the directory containing your Maven artifact's pom.xml file. If you've just created an artifact, the pom.xml file will be in the myFirstApp folder.

  3. From the Connect to feed dialog in Azure DevOps Services, copy the <repository> information. Paste it into your pom.xml file twice (see the sample file above):

    • Between the <repositories> tags.
    • Between the <distributionManagement> tags.
  4. From the directory containing your pom.xml file, run the command mvn deploy. The Maven artifact should appear in your feed.

Sample pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>MyGroup</groupId>
   <artifactId>myFirstApp</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>myFirstApp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <repositories>
    <!-- Copy this section from the Maven section of the "Connect to Feed" dialog -->
      <repository>
         <id>dev.azure.com-mseng-zcalvinmaven</id>
         <url>https://pkgs.dev.azure.com/mseng/_packaging/zCalvinMaven2/maven/v1</url>
         <releases>
            <enabled>true</enabled>
         </releases>
         <snapshots>
            <enabled>true</enabled>
         </snapshots>
      </repository>
   </repositories>
   <distributionManagement>
    <!-- Copy this section from the Maven section of the "Connect to Feed" dialog -->
      <repository>
         <id>dev.azure.com-mseng-zcalvinmaven</id>
         <url>https://pkgs.dev.azure.com/mseng/_packaging/zCalvinMaven2/maven/v1</url>
         <releases>
            <enabled>true</enabled>
         </releases>
         <snapshots>
            <enabled>true</enabled>
         </snapshots>
      </repository>
   </distributionManagement>
</project>

Important

The <id> tags in the settings.xml and the pom.xml must match.

See the Maven CLI docs for more publish options.

Install an artifact from your feed

Install Maven artifacts from your feed by using the Maven client.

The most common way to install a Maven artifact is as a dependency of another artifact.

  1. Create a Maven artifact by using the following command:

    mvn -B archetype:generate -DarchetypeGroupId="org.apache.maven.archetypes" -DgroupId="MyGroup" -DartifactId="mySecondApp"
    
  2. From the Connect to feed dialog box in Azure DevOps Services or TFS, copy the <repository> information. Paste it into your pom.xml file twice (see the preceding sample file):

    • Between the <repositories> tags
    • Between the <distributionManagement> tags
  3. In Azure Artifacts, browse to the artifact that you want to install and copy the contents of the <dependency> element.

  4. Paste the <dependency> element content inside the <dependencies> element of your pom.xml file.

  5. Run mvn install from the directory that contains your pom.xml file.

See the Maven CLI docs for more installation options.

  1. Create a Maven artifact by using the following command:

    mvn -B archetype:generate -DarchetypeGroupId="org.apache.maven.archetypes" -DgroupId="MyGroup" -DartifactId="mySecondApp"
    
  2. From the Connect to feed dialog box in TFS, copy the <repository> information. Paste it into your pom.xml file twice (see the preceding sample file):

    • Between the <repositories> tags
    • Between the <distributionManagement> tags
  3. From the Packages page, browse to the artifact that you want to install and copy the contents of the <dependency> element.

  4. Paste the <dependency> element content inside the <dependencies> element of your pom.xml file.

  5. Run mvn install from the directory that contains your pom.xml file.

See the Maven CLI docs for more installation options.

Automate the process with continuous integration

You can use continuous integration systems such as Team Build to automate the installation and publishing of your Maven artifacts. To get started with continuous integration, see the Maven in Team Build guidance.

What's next?

For more advanced topics, check out the content summary.