1

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.

Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61

1 Answers1

3

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.

Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
  • Would you happen to know is there is anything special required for project scoped feeds? I get a 401 unauthorized using the method here which works just fine for org scope feeds. – Bruce Adams Jun 30 '20 at 21:55
  • Please check your token access Right, is it full access token or limited access token and then secondly check your token expiration date, it shouldn't be expired ☺️ – Dupinder Singh Jul 01 '20 at 02:07
  • I made this a full question - https://stackoverflow.com/q/62667271/1569204 – Bruce Adams Jul 01 '20 at 07:30
  • I'm not sure where the type "full access" or "limited" would be displayed other than in permissions. I gave it full permissions. The access token works for at least organisation level feeds. – Bruce Adams Jul 01 '20 at 07:31