Get started with Maven feeds and Artifacts

Azure DevOps Services | TFS 2018

This quickstart will guide you through setting up Maven to publish and consume Artifacts from your feed.

This guide assumes you've already set up Azure Artifacts. See Start using Azure Artifacts to learn how to license the extension.

Note

Azure Artifacts is an extension that comes pre-installed in 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 Visual studio marketplace.

Prerequisites

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

  2. Have Azure Artifacts installed in your organization.

Create a feed

Already have a feed? Skip to the next step.

With Azure Artifacts, you can create two types of feeds: project-scoped and organization-scoped feeds. All public feeds are project-scoped and they inherit the hosting project's visibility settings. By default, any feed that's created by using the web UI is a project-scoped feed, as follows.

  1. From within your project, navigate to Azure Artifacts.

    Go to Azure Artifacts

  2. Select Create Feed.

    New feed button

  3. Give your feed a Name and choose its visibility, upstream sources, and scope settings.

    New feed dialog box

  4. When you are done, select Create.

Note

Enabling upstream sources allows you to consume packages from public registries and gives you more protection against outages and corrupted packages.

Azure Artifacts is installed by default for TFS 2017 customers. You must upgrade to TFS 2017 in order to use Azure Artifacts. If this is the first time using your feed, you might be asked to assign a license

  1. Go to Build & Release and select Packages.

    Go to Azure Artifacts TFS

  2. Select + New feed.

    New feed button TFS

  3. Give your feed a Name, a Description, and set up who can read, who can contribute and if you want to Include external packages.

    New feed dialog box TFS

Note

Enabling upstream sources allows you to consume packages from public registries and gives you more protection against outages and corrupted packages.

Set up authentication

To talk to Azure Artifact 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 under the maven header.

    • 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.

    Tip

    To make sure you create and save your new XML file in the correct format, set File Explorer to show file name extensions:

    1. In File Explorer, select the View tab.
    2. Under Show/hide, select the File name extensions check box to see your files extensions in File Explorer.
  2. If you haven't installed Maven on your machine, you can select Get the tools to download and install it.

  3. Follow the Project setup section including generating a personal access token.

    Set up Maven authentication

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

Publish an artifact

To publish a Maven artifact to your feed, follow these steps:

  1. If you don't have a Maven package, you can create one by running the following command:

    mvn -B archetype:generate -DarchetypeGroupId="org.apache.maven.archetypes" -DgroupId="MyGroup" -DartifactId="myFirstApp"
    

    If you get the following error You must specify a valid lifecycle phase or a goal (..) after executing the previous command, follow these steps to add a goal in your run configuration:

    Right click on your project, select Run as then Maven Build. Enter package in the Goals text box, then select Run.

  2. Set up the Maven client with your feed.

  3. Navigate to the directory of your pom.xml file. By default, the pom.xml file is at root path of the project.

  4. Run the following commands to build and deploy your Maven artifact:

    • Build your package: mvn build
    • Deploy your package: mvn deploy

If you want to publish a third-party artifact, you can use the deploy:deploy-file mojo. This can be used with or without a POM file to deploy your packages.

mvn deploy:deploy-file -Dpackaging="jar" -DrepositoryId="MyFeedName" -Durl="MyFeedURL" -DgroupId="MyGroup" -DartifactId="myFirstApp" -Dversion="jarFileVersion" -Dfile="jarFileLocalPath"

Note

Maven snapshots are not supported in upstream sources.

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. In Azure Artifacts, browse to the artifact that you want to install and copy the contents of the <dependency> element.

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

  4. 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?

Check out the Azure Artifacts landing page to learn about other topics.