Jim Passmore
Jim Passmore

Reputation: 71

How do I add Maven dependencies to Azure Pipelines

I am attempting to build a Maven Build Pipeline in Azure Devops.

I have one project that has files shared across several others (as set of micro service projects), so the maven compile for that project creates a .jar file ( which is added to my local maven repository .m2) when I do the build on my laptop. We can call it shared.jar for now. ​

Then when I compile the micro services on my laptop, they each have a dependency on the jar file in my .m2 repo. And they pick up the dependency from there and the Maven build (mvn clean install). ​

Unfortunately, The code build pipeline does not maintain a local .m2 repo. So when I try to do a “mvn clean install” on each micro service they cannot locate shared.jar, so the build fails. ​

I have been able to successfully add the .jar file to a Feed and Artifact under Azure DevOps, but I can't seem to figure out how to pull it into the Micro Services Build.

HOW do I pick up the dependency for a local jar file in my CI/CD Maven build pipeline and get it into a maven repo that my build will find?

I have attempted to follow this link: https://learn.microsoft.com/en-us/azure/devops/artifacts/get-started-maven?view=azure-devops

Upvotes: 6

Views: 12969

Answers (3)

Roberto Morales
Roberto Morales

Reputation: 11

I just had to do this myself to create a docker image whit the final .jar and got inspired by Bruce Adams answer and the documentation about maven

you have to add to your pipeline.yml

- task: DownloadSecureFile@1
  name: mvnSettings
  displayName: 'Download Maven settings'
  inputs:
    secureFile: 'maven-azuredevops-settings.xml'
- task: MavenAuthenticate@0
  displayName: Maven Authenticate Artifacts
  inputs:
    artifactsFeeds: 'yourFeed'
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'deploy'
    options: '-s $(mvnSettings.secureFilePath)'
    mavenAuthenticateFeed: true
    publishJUnitResults: true
    testResultsFiles: '**/TEST-*.xml'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.11'
    mavenVersionOption: 'Default'

and in your pom.xml you need to add the configuration to connect to your artifacts feed

<repositories>
    <repository>
    <id>yourFeed</id>
    <url>https://yourOrganization.pkgs.visualstudio.com/yourProject/_packaging/yourFeed/maven/v1</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
    </repository>
</repositories>
<distributionManagement>
    <repository>
    <id>bithaus</id>
    <url>https://yourOrganization.pkgs.visualstudio.com/yourProject/_packaging/yourFeed/maven/v1</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
    </repository>
</distributionManagement>

and create the file setting.xml on your {user.home}/.m2/ folder, and upload it to the secure files on your pipeline. Hope it helps someone.

Upvotes: 0

Bruce Adams
Bruce Adams

Reputation: 5591

We do this by having a separate azure pipeline to build the dependency which deploys it to an Azure feed:

       variables: 
        - name: mavenRepoURL
          value: 'https://myorg.pkgs.visualstudio.com/myproject/_packaging/myfeed/maven/v1'

        - task: DownloadSecureFile@1
          name: mvnSettings
          displayName: 'Download Maven settings'
          inputs:
            secureFile: 'maven-azuredevops-settings.xml'

        - task: MavenAuthenticate@0
          displayName: Maven Authenticate Artifacts
          inputs:
            artifactsFeeds: 'myfeed'

        - task: Maven@3
          inputs:
            mavenPomFile: 'pom.xml'
            options: '-X -B -s $(mvnSettings.secureFilePath) -DWHERE="AzureDevops" -DremoteRepositories=$(mavenRepoUrl) clean deploy -U'
            mavenAuthenticateFeed: true
            publishJUnitResults: true
            testResultsFiles: '**/TEST-*.xml'
          

Note that its important that your personal access token is not stored in version control for security reasons hence the complication of having to upload it as a secure file and download it in the pipeline.

The dependency is added to the pom for the project using it as normal but with the Azure feed configured as well

        <dependency>
            <groupId>com.foobar.blah</groupId>
            <artifactId>artifactId</artifactId>
            <version>2.0.2</version>
        </dependency>

        <profile>
            <id>AzureDevops</id>
            <activation>
                <property>
                    <name>WHERE</name>
                    <value>AzureDevops</value>
                </property>
            </activation>
            <properties>
            </properties>
            <distributionManagement>
                <repository>
                    <id>myfeedname</id>
                    <url>https://myorg.pkgs.visualstudio.com/_packaging/myfeed/maven/v1</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </distributionManagement>
        </profile>

Caveat: I am not that familiar with maven or Azure so some things may not be right. For example, I have issues with project rather than organisation scoped feeds. See What are the equivalent maven commands and settings to upload and download azure artifacts?

Upvotes: 3

Meysam Karimi
Meysam Karimi

Reputation: 97

please check following itemes:

  1. Make sure that when you run "mvn clean install" command, maven read configurations from settings.xml file in ${user.home}/.m2 .
  2. Check id, username and password whitin <server></server> tags.
  3. Please before running "mvn clean install" remove all files and folder in .m2 folder.
  4. enable debugging option when run maven install command by add -X or --debug options.

Upvotes: -1

Related Questions