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.
Related
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"
}
We use Jenkins (2.150.1) together with M2 Release Plugin (0.14.0). After checkout from git we specified "Maven release build -> Release goals and options"
-Dresume=false release:prepare release:perform -DautoVersionSubmodules -Dgoals=deploy
and in the Build step we configured the maven "Goals and Options"
clean verify -U
When we start to perform a Maven release, I get asked e.g. a Release Version 1.0 and a new Version 1.1-SNAPSHOT.
Generally, everything works fine like the following way:
git clone
mvn changes versions to 1.0
mvn clean verify -U
mvn release perform
mvn changes version to 1.1-SNAPSHOT
mvn clean verify -U
mvn release perform
Question: Do you know how I can skip the second part? So maven does update to Version 1.1-SNAPSHOT but does NOT perform the following two steps?
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
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"
}
I have a Maven project that generates a zip file with maven-assembly-plugin on mvn package
MyProy
|--pom
|--src/...
|--target
|---MyProy-something-1.0.0-SNAPSHOT-zip (contains /1.0.0-SNAPSHOT/...)
I created a Jenkins job and configured a shell script execution as a post step. This script copies the zip to a folder (/something) and unzips it
|-/something
|---1.0.0-SNAPSHOT
|---...
This works fine for builds. However I want to do the same when I launch a release. I installed Jenkins release plugin and in "configure release build" I added this tasks:
Before release build -> Maven task mvn release:prepare
After successful release build -> Maven task mvn release:perform
After successful release build -> Execute shell script - Copy zip and unzip.
I see the mvn release:perform execute correctly, for instance with version 1.0.0 but the problem is that when the script (3.) is launched it applies to the next development version (1.0.1-SNAPSHOT) instead of the release version (1.0.0).
This is how I managed to work around the problem:
Before release build
mvn release:prepare
After successful release build
mvn release:perform
mvc scm:checkout -DscmVersion=${releaseVersion} -DscmVersionType=tag
-f ${WORKSPACE}/target/checkout clean install
Execute shell script with copy and unzip
To sum up, after the release:perform I checkout the brand new tag, execute a clean install in the location of the downloaded tag and finally I execute the script that copies the zip and unzipes it.