dna
dna

Reputation: 519

How to specify the maven local repository path with artifactory plugin?

I'm using the artifactory plugin in a Jenkinsfile to drive my maven build:

def goals = "clean install"
def artifactory = Artifactory.server 'artifactory'

configFileProvider([configFile(fileId: 'simple-maven-settings', variable: 'MAVEN_USER_SETTINGS')]) {
    def mavenRuntime = Artifactory.newMavenBuild()
    mavenRuntime.tool = 'maven350'
    mavenRuntime.resolver server: artifactory, releaseRepo: '…', snapshotRepo: '…'
    mavenRuntime.deployer server: artifactory, releaseRepo: '…', snapshotRepo: '…'

    def buildInfo = mavenRuntime.run pom: 'pom.xml', goals: "-B -s ${MAVEN_USER_SETTINGS} ${goals}".toString()

This works fine but maven uses the default, shared location ~/.m2/repository

I want to reproduce the behavior I had before moving to pipeline and give each job its own local repository.

I do not find any setting for that… How should I proceed?

Upvotes: 2

Views: 6573

Answers (2)

khmarbaise
khmarbaise

Reputation: 97527

You can do it like this in a Jenkins Pipeline with Maven:

  withMaven(
        // Maven installation declared in the Jenkins "Global Tool Configuration"
        maven: 'M3',
        // Maven settings.xml file defined with the Jenkins Config File Provider Plugin
        // Maven settings and global settings can also be defined in Jenkins Global Tools Configuration
        mavenSettingsConfig: 'my-maven-settings',
        mavenLocalRepo: '.repository') {

      // Run the maven build
      sh "mvn clean install"

    } // withMaven

There you can simply define the local cache location.

Upvotes: 2

Eyal Ben Moshe
Eyal Ben Moshe

Reputation: 2435

By default, maven uses the local maven repository inside the .m2 directory under the user home. In case you'd like maven to create the local repository, for example, in your job's workspace, add the -Dmaven.repo.local=.m2 system property to the goals value as shown here:

def buildInfo = rtMaven.run pom: 'pom.xml', goals: 'clean install -Dmaven.repo.local=.m2'

The value of -Dmaven.repo.local points to the local maven repository used for this maven build. You can read more about it here`

Upvotes: 4

Related Questions