Get started with Maven packages in Azure Artifacts

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

Azure Artifacts enables developers to manage their dependencies from a single feed while providing control over package sharing. With Azure Artifacts feeds, you can publish and restore Maven packages, and set up upstream sources to consume packages from public registries like Maven Central, Google Maven Repository, Gradle Plugins, and JitPack. This article will guide you through setting up your Maven project, connecting to an Azure Artifacts feed, and publishing and restoring your Maven packages.

Prerequisites

Create a feed

  1. Sign in to your Azure DevOps organization and navigate to your project.

  2. Select Artifacts, and then select Create Feed.

  3. Provide a Name for your feed, choose the Visibility option that defines who can view your packages, check Include packages from common public sources if you want to include packages from sources like nuget.org or npmjs.com, and for Scope, decide whether the feed should be scoped to your project or the entire organization.

  4. Select Create when you're done.

    Screenshot that shows selections for creating a new feed in Azure DevOps Services.

  1. Sign in to your Azure DevOps server, and then go to your project.

  2. Select Artifacts, and then select Create Feed.

  3. Provide a Name for your feed, choose the Visibility option that defines who can view your packages, check Include packages from common public sources if you want to include packages from sources like nuget.org or npmjs.com, and for Scope, decide whether the feed should be scoped to your project or the entire organization.

  1. Select Create when you're done.

    Screenshot that shows selections for creating a new feed in Azure DevOps 2022.

  1. Select Create when you're done.

    Screenshot that shows selections for creating a new feed in Azure DevOps 2020.

  1. Sign in to your Azure DevOps server, and then go to your project.

  2. Select Artifacts, and then select New feed.

  3. For Name, enter a descriptive name for your feed.

    For Visibility, select an option to indicate who can view packages within the feed.

    If you want to include packages from public sources, select the Use packages from public sources through this feed option.

  4. Select Create when you're done.

    Screenshot that shows selections for creating a new feed in Azure DevOps 2019.

Note

By default, newly created feeds have their project's Build Service value set to Feed and Upstream Reader (Collaborator).

Connect to a feed

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Artifacts, and then select your feed from the dropdown menu.

  3. Select Connect to feed, and then select Maven from the left navigation area.

  4. If this is your first time using Azure Artifacts with Maven, make sure you've installed the prerequisites. Otherwise select Get the tools in the top-right corner to install them.

  5. Follow the instructions in the Project setup section to set up your pom.xml and settings.xml files. Your files should resemble the following examples:

    • pom.xml:

        <repositories>
          <repository>
            <id>MavenDemo</id>
            <url>https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1</url>
            <releases>
              <enabled>true</enabled>
            </releases>
            <snapshots>
              <enabled>true</enabled>
            </snapshots>
          </repository>
        </repositories>
        <distributionManagement>
          <repository>
            <id>MavenDemo</id>
            <url>https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1</url>
            <releases>
              <enabled>true</enabled>
            </releases>
            <snapshots>
              <enabled>true</enabled>
            </snapshots>
          </repository>
        </distributionManagement>
      
    • settings.xml:

        <servers>
          <server>
            <id>MavenDemo</id>
            <username><ORGANIZATION_NAME></username>
            <password>[PERSONAL_ACCESS_TOKEN]</password>
          </server>
        </servers>
      
  6. Generate a Personal Access Token with Packaging > Read & write scope, and then paste it into the <password> tag.

  1. Sign in to your Azure DevOps collection, and then navigate to your project.

  2. Select Artifacts, and then select your feed from the dropdown menu.

  3. Select Connect to Feed, and then select Maven from the left navigation pane.

  4. If this is your first time using Azure Artifacts with Maven, make sure you've installed the prerequisites, otherwise select Get the tools in the top-right corner to install them.

  5. Follow the instructions in the Project setup section to set up your pom.xml and settings.xml files. Your files should resemble the following examples:

    • pom.xml:

        <repositories>
          <repository>
            <id>MavenDemo</id>
            <url>http://<SERVER_NAME>/<COLLECTION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1</url>
            <releases>
              <enabled>true</enabled>
            </releases>
            <snapshots>
              <enabled>true</enabled>
            </snapshots>
          </repository>
        </repositories>
        <distributionManagement>
          <repository>
            <id>MavenDemo</id>
            <url>http://<SERVER_NAME>/<COLLECTION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1</url>
            <releases>
              <enabled>true</enabled>
            </releases>
            <snapshots>
              <enabled>true</enabled>
            </snapshots>
          </repository>
        </distributionManagement>
      
    • settings.xml:

        <servers>
          <server>
            <id>MavenDemo</id>
            <username><COLLECTION_NAME></username>
            <password>[PERSONAL_ACCESS_TOKEN]</password>
          </server>
        </servers>
      
  6. Generate a Personal Access Token with Packaging > Read & write scope, and then paste it into the <password> tag.

  1. Sign in to your Azure DevOps collection, and then navigate to your project.

  2. Select Artifacts, and then select your feed.

  3. Select Connect to Feed, and then select Maven from the left navigation pane.

  4. Select Generate Maven Credentials and add the credentials to your user settings.xml file inside the <servers> tag. Your file should look like the following example:

      <servers>
        <server>
          <id>server-2019-defaultcollection-demo</id>
          <username><FEED_NAME></username>
          <password>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</password>
        </server>
      </servers>
    
  5. Add the second snippet to your project's pom.xml inside both the <repositories> and <distributionManagement> tags. Your file should look like the following example:

      <repositories>
        <repository>
          <id>server-2019-defaultcollection-demo</id>
          <url>http://<SERVER_NAME>/<COLLECTION_NAME>/_packaging/<FEED_NAME>/maven/v1</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <distributionManagement>
        <repository>
          <id>server-2019-defaultcollection-demo</id>
          <url>http://<SERVER_NAME>/<COLLECTION_NAME>/_packaging/<FEED_NAME>/maven/v1</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </distributionManagement>
    

Publish packages

  1. Open a command prompt window and navigate to your project directory where your pom.xml file is located, and then run the following command to package your project. This command compiles your code, runs tests included, and packages your project into a distributable format (such as a JAR file).

    mvn package
    
  2. Run the following command from your project directory to publish your package to your feed:

    mvn deploy
    
  3. After successfully deploying your package, it will shortly be available in your feed.

    A screenshot that shows a Maven package deployed to a feed.

Note

If your organization is using a firewall or a proxy server, make sure you allow Azure Artifacts Domain URLs and IP addresses.

Restore packages

  1. Navigate to Artifacts in your Azure DevOps project, select your feed from the dropdown menu, and then select the package you wish to install.

  2. Navigate to Overview > Maven, then copy the dependency snippet.

  3. Open your pom.xml file and paste the snippet inside the <dependencies> tag.

  4. Run the following command from the directory where your pom.xml file is located to restore your packages:

    mvn install