Search code examples
javaazuremavenauthenticationartifact

Maven project is not able to download artifacts from Azure Artifacts


Another way to ask this question is:

How to authenticate maven project with Azure personal access token so that Maven build can download artifacts published on Azure Artifacts

I have Personal access token, to authenticate. But the problem is what would be the pom.xml which help maven project to authenticate.

Or more simple words

How to configure the maven project to download artifacts from maven central repo and some private artifact publisher also. In my case private artifact publisher is Azure Artifacts.


Solution

  • Fist of all set your Azure Personal Access Token in system environment variable. like environment variable name key is AZURE_PAT then access this in settings.xml

    You just need to add a settings.xml under C:\Users\<UserName>\.m2

    settings.xml will be like

    <settings>
      <servers>
        <server>
          <id>AzureRepo</id>
          <username>AZURE_ARTIFACTS</username>
          
            <password>${env.AZURE_PAT}</password>
            
        </server>
    
      </servers>
    
    </settings>
    

    Then Open pom.xml and add the following repository attribute under repositories attribute. Your azure artifactory URL will be provided by Azure artifactory.

        <repository>
            <id>AzureRepo</id>
            <url>Artifactory URL provided by Azure</url>
        </repository>
    

    So your pom.xml will look like this. BTW this pom.xml have spring dependencies.

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.basic</groupId>
        <artifactId>basic</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>basic</name>
        <description>basic maven project for Spring Boot</description>
    
        <properties>
            <java.version>11</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
        <repositories>
            <repository>
              <id>Central Maven repository</id>
              <name>Central Maven repository https</name>
              <url>https://repo.maven.apache.org/maven2</url>
              <layout>default</layout>
            </repository>
            <repository>
                <id>AzureRepo</id>
                <url>Artifactory URL provided by Azure</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
              <id>Central Maven repository</id>
              <name>Central Maven repository https</name>
              <url>https://repo.maven.apache.org/maven2</url>
              <layout>default</layout>
            </pluginRepository>
        </pluginRepositories>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    This can help you to download azure artifact in maven repository.