Jenkins pipeline script example

ARTIFACTORY: How to run Maven builds from pipeline scripts with Artifactory example

AuthorFullName__c
DaYoun Kang
articleNumber
000005351
ft:sourceType
Salesforce
FirstPublishedDate
2022-07-28T07:45:51Z
lastModifiedDate
2022-07-28
VersionNumber
2
node {
    def server
    def rtMaven = Artifactory.newMavenBuild()
    def buildInfo

    stage ('Clone') {
        git url: 'https://github.com/jfrog/project-examples.git'
    }

    stage ('Artifactory configuration') {
        // Obtain an Artifactory server instance, defined in Jenkins --> Manage Jenkins --> Configure System:
        server = Artifactory.server 'my-artifactory'

        // Tool name from Jenkins configuration, defined in Jenkins --> Manage Jenkins --> Global Tool Configuration  --> Maven Installations --> Name
        rtMaven.tool = 'mvn'
        rtMaven.deployer releaseRepo: 'my-libs-release-local', snapshotRepo: 'my-libs-snapshot-local', server: server
        rtMaven.resolver releaseRepo: 'my-libs-release', snapshotRepo: 'my-libs-snapshot', server: server
        buildInfo = Artifactory.newBuildInfo()
    }

    stage ('Exec Maven') {
        rtMaven.run pom: 'maven-examples/maven-example/pom.xml', goals: '-DmavenLocalRepo="" -DmavenSettingsFilePath="" clean install -U', buildInfo: buildInfo 
    }

    stage ('Publish build info') {
        server.publishBuildInfo buildInfo
    }
}

In the above example, I created an Artifactory Maven Build instance ‘rtMaven’.

Under the ‘Artifactory configuration’ stage, I defined my Artifactory server as ‘my-artifactory’. This information can be found in Jenkins --> Manage Jenkins --> Configure System.

Then, I specified my maven tool name as ‘mvn’, which can be found in Jenkins --> Manage Jenkins --> Global Tool Configuration --> Maven Installations --> Name.

After that, I define my deployer and resolver repositories. Please note that the repositories should be defined accordingly as below:
rtMaven.deployer releaseRepo: ARTIFACTORY_LOCAL_RELEASE_REPO, snapshotRepo: ARTIFACTORY_LOCAL_SNAPSHOT_REPO, server: server


rtMaven.resolver releaseRepo: ARTIFACTORY_VIRTUAL_RELEASE_REPO, snapshotRepo: ARTIFACTORY_VIRTUAL_SNAPSHOT_REPO, server: server

Lastly, I define my ‘buildInfo’.

The ‘Exec Maven’ and ‘Publish build info’ stages will execute maven goals and publish build info to Artifactory accordingly.