7

In my POM.xml file for my android project I have created six profiles. I run these from the command line like mvn clean install -P mdpi. This works fine. Now I am using jenkins for my CI. I want the user to be presented with a drop down list of all profiles and then use mvn clean install -P ${selected-profile} so the ${selected-profile} variable contains the profile for the build. How can i do this?

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
AndroidDev
  • 15,993
  • 29
  • 85
  • 119
  • I too need a way to do this.. -P ${selected-profile} did not work for me.. should we raise it in plugin issue page? – Praveen Oct 07 '13 at 07:17
  • 1
    Check out this link, http://stackoverflow.com/questions/9471460/setting-maven-params-in-jenkins – Ashay Batwal Oct 07 '13 at 07:39
  • Executing maven goal in batch or bash works with Jenkins params. For example... in this case add a post build m2 step batch command like `mvn clean install -P %selected-profile%`. But maven plugin should add support for profile value that comes from jenkins parameter. – Praveen Oct 07 '13 at 10:08

2 Answers2

16

I suggest you to install the parametrized build plugin which will allow you to display a list of options to your job user. To enable it you'll have to check 'this build is parametrized' option and then define option.

To be able to define list option you'll then have to install extended choice parameter plugin that extends the first one by adding list option (and other parameter types).

You'll then be able to define a PROFILE list option. The selected option name will be stored in the parameter name.

The maven cmdline: 'mvn clean install -P${PROFILE}' will then work as you expect.

I hope this helps!

djangofan
  • 28,471
  • 61
  • 196
  • 289
thesmash
  • 599
  • 6
  • 9
0

I have added the following to my Jenkins file:

pipeline {
agent {
    label 'common'
}
tools {
    maven 'Maven-3.5.4'
}
stages {
    stage('Build and Publish') {
        when {
            anyOf {
                buildingTag();
                branch 'master';
                branch 'develop';
                branch 'release/**';
            }
        }
        steps {
            script {
                def server = Artifactory.server 'test-artifactory'
                def rtMaven = Artifactory.newMavenBuild()
                def buildInfo
                //Set defualt profile
                def mvnProfile = 'dev'
                //Set the target mvn profile based on the current branch 
                if(env.BRANCH_NAME.equals('master')){
                    mvnProfile = 'prod'
                 }else if(env.BRANCH_NAME.startsWith('release/')){
                    mvnProfile = 'test'
                }

                rtMaven.tool = "Maven-3.5.4"
                // Set Artifactory repositories for dependencies resolution and artifacts deployment.
                rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server

                buildInfo = rtMaven.run pom: 'pom.xml', goals: 'clean install -Dmaven.test.skip=true -P' + mvnProfile
                server.publishBuildInfo buildInfo
            }
        }
    }

}

}

I have added the following conditional if within my script to set the target mvn profile:

//Set defualt profile
def mvnProfile = 'dev'
//Set the target mvn profile based on the current branch 
if(env.BRANCH_NAME.equals('master')){
    mvnProfile = 'prod'
}else if(env.BRANCH_NAME.startsWith('release/')){
    mvnProfile = 'test'
}
Ashraf Sarhan
  • 1,507
  • 16
  • 21