No such property: clean for class groovy.lang.Binding - maven

I created a Groovy script for Jenkins, where I am building my project with Maven:
node {
mvn clean install
}
However, I am getting:
groovy.lang.MissingPropertyException: No such property: clean for class: groovy.lang.Binding
So what's the correct syntax for cleaning and installing with Maven?

From https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Maven+Plugin:
node{
...
withMaven(
maven: 'M3',
mavenSettingsConfig: 'maven-settings-for-gameoflife',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
}
}
E.g. You need to use the withMaven step and then do a native batch or bat command to build. The withMaven step makes sure that mvn is on the PATH when calling sh or bat.
Note that you need to set up maven in your Jenkins installation first.
Or, if you already have mvn on path on the computer where Jenkins is running, then simply run:
node {
sh "mvn clean install"
}
or if you're on windows:
node {
bat "mvn clean install"
}

Related

How to use specific maven and jdk on Jenkins

I have corporate Jenkins where I don't have access to Manage Jenkins option. I want to make a build of my java app using maven.
When I try to run mvn clean install:
dir("test/test2/project") {
sh "mvn clean install -Dmaven.test.skip=true"
}
I get the following error:
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/var/jenkins/workspace/test/test2/project). Please verify you invoked Maven from the correct directory.
I was trying to add mvn -f /var/jenkins/workspace/test/test2/project/pom.xml (I have pom file in the folder) but it did not work.
I also tried
withEnv(["PATH+MAVEN=${tool 'maven-3.5.0'}/bin:${env.JAVA_HOME}/bin"]) {
sh "mvn --batch-mode -V -U -e clean install -Dmaven.test.skip=true"
which also did not work.
I would like to point to maven and java which are installed on the agent but can't seem to sucseed.
Any idea?
Can you try something like below?
dir("test/test2/project") {
sh "mvn clean install"
}

Is there a way to programmatically specify pom.xml path in jenkinsfile

In a declarative pipeline, I manually specify pom.xml path in Jenkinsfile and Jenkins is able to locate it as expected at build time.
pipeline {
agent any
options {
timestamps()
}
stages {
stage('Compile') {
steps {
withMaven(maven: 'MAVEN_HOME') {
sh 'mvn -f /Users/jo/.jenkins/workspace/DeclarativePipelineDemo/Demo/pom.xml clean install' //filepath
}
}
}
}
Now, is there a more elegant way to tell Jenkins to dynamically capture the workspace/pom.xml classpath directly from my project so I don't need to manually specify it?
If your Jenkinsfile in same repo of pom.xml, you can use relative path.
When Jenkins run you pipeline, it will automatically clone the repo that holds the Jenkinsfile to Jenkins slave.
If pom.xml in base dir of project, you can try
sh mvn -f pom.xml ...

Jenkins Pipeline and SonarQube setup

I'm having some trouble setting up SonarQube using Maven on a Jenkins Pipeline.
My pipeline pulls the git repo into the directory created for it and it goes through the rest of the steps successfully but I don't see the test results on SonarQube nor any output that tests are being ran.
Here is my code set up on the pipeline:
node('master'){
stage('Git Clone') {
dir('my-git-dir'){
git branch: '$GIT_BRANCH'
git url: '$GIT_REPO'
credentialsId: '11111111-111-1111-1111-111111111111'
}
}
stage('build & SonarQube Scan') {
MVN="/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.3.9/bin/mvn"
echo "running clean verify sonar"
"$MVN clean verify sonar:sonar -Dsonar.host.url=http://111.11.1.111:9000 -Dsonar.java.binaries=/etc/sonarqube"
echo "running clean install"
"$MVN clean install deploy -DskipTests"
}
}
The command runs just fine on a free style project:
#!/usr/bin/bash
MVN="/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.3.9/bin/mvn"
$MVN clean verify sonar:sonar -Dsonar.host.url=http://111.11.1.111:9000 -Dsonar.java.binaries=/etc/sonarqube
$MVN clean install deploy -DskipTests
It also has an "Invoke top-level Maven targets"
Maven Version:
Maven 3.3.9
Goals:
test
-fn
Edit: Working Script
stage('SonarQube analysis') {
dir("$gitRepo"){
withSonarQubeEnv('SonarQube') {
sh "pwd"
MVN="/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.3.9/bin/mvn"
echo "Running JaCoCO stuff"
sh "$MVN clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false"
echo "running clean verify"
sh "$MVN clean verify sonar:sonar -Dsonar.host.url=http://111.11.1.111:9000" //-Dsonar.java.binaries=/etc/sonarqube"
echo "running clean install deploy"
sh "$MVN clean install deploy -DskipTests"
}
P.S I am trying to create a job that you can select a repo and branch to pull from, create/use a file in that workspace to run SonarQube on
Your question demonstrates several misunderstandings.
First, SonarQube analysis will not execute your tests for you; you need to fire that off yourself. As noted in the docs the command for that would be something like
$MVN clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false
followed by
$MVN sonar:sonar -sonar.host.url=http://111.11.1.111:9000
Note that I've left off the -Dsonar.java.binaries=/etc/sonarqube parameter you used. That's because
This is the path to the compiled classes of your project. They should not be in /etc/sonarqube. (Move them if they are.)
Maven analysis already knows where your binaries are so you don't need to provide the path unless your build is configured to put them somewhere strange.

sonar qube on jenkins pipeline

How can I make that sonnar qube server analyze a mvn project on jenkins?. I have a pipeline project from CSM with a jenkinfile which point to a groovy file where all steps of the job are executed. All steps are working ok (mvn test, mvn package, mvn compile, etc), but donĀ“t know how to execute the mvn sonar:sonar. It gives following error.Image show how do I have sonar configured in jenkins and the job step where it fails.
And this is how I have the step described in groovy file of pipeline:
stage ('SonarQube analysis') {
withSonarQubeEnv('https://sonarqube.xxxxx.com') {
sh 'mvn sonar:sonar'
}
}
Try using the server installation name in withSonarQubeEnv, right now you are using URL i.e. withSonarQubeEnv('Grey Azure Sonarqube').
Documentation

Configure settings.xml in jenkins slave created on the fly in AWS

I am creating Jenkins Slave on the fly configuring it on AWS with Spot Instances.
In the global tool configuration, I have set to use my own "settings.xml" for the master is working perfectly.
But when the server start slaves (without maven installed) it auto install maven (set in the Jenkins file to install this tool) but without putting any settings.xml
*I know I can copy the setting.xml directly from the server but for me looks like it is not the appropriate way to do it.
* I already did mvn -X in order to see find the folder for the settings but this is not used.
Added one small slice of the jenkinsfile
pipeline {
tools{
maven 'maven default'
}
agent any
stages {
stage('Maven build') {
steps {
sh 'mvn clean install'
}
}
}
}
You have to use withMaven() in the Pipeline code..which has to look like this:
withMaven(jdk: jdkName, maven: MavenInGlobalToolsName, mavenSettingsConfig: 'IdInConfigFileProvided', mavenLocalRepo:".repository") {
sh "mvn clean verify"
}
The IdInConfigFileProvided is the important part which makes the reference to the config file provider plugin...
The other solution could be to use the config file provider directly in Jenkins file:
configFileProvider(
[configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS clean package'
}
But this solution will not handle the support global tools support for Maven itself. So I would suggest to prefer the withMaven() solution.

Resources