How to use specific maven and jdk on Jenkins - maven

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"
}

Related

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.

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

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"
}

Maven build in Jenkins writes the output of execution to a file

I have a Maven build job in Jenkins and in the "Build" section, I have given Maven Version and Root POM and in the "Goals and Options" filed, I am executing the pom with customized goals. I need to write the output of the execution to a file, I tried below
clean install -Dmaven.test.skip=true -nsu -l output.log
clean install -Dmaven.test.skip=true -nsu -DoutputFile=output.log
clean install -Dmaven.test.skip=true -nsu -Doutput=output.log
Nothing works for me. Could anyone please help in either the above way or any other option available to direct the output log to a file?
According to mvn -help, the following works for me:
clean install -l output.log
The file is stored to the job workspace, if you want to publish it as an artifact, you need to add a Publish artifact or Publish document post-step (or Publish document post-build option in Maven project).

Gradle equivalent for mvn clean verify

I have a project which is based on gradle .I have to run the command which is equivalent for mvn clean verify .As I am new to both gradle and maven ,and have been exposed to only 3 command of both .I want to run a gradle equivalent for mvn clean verify .I searched on websites but still have not got the answer .Can some please help me to know what will be the gradle equivalent for "mvn clean verify"
gradle clean verify
or if you are using gradle wrapper:
./gradlew clean verify

Use maven even if gradle is present in travis

I've added some gradle task to my project. However, I still want to continuous build to run maven. My first attempt was to add an explicit script
script:
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
- mvn test -B
This doesn't work. I tried to rename the build.gradle file. This doesn't work either. I probably need to remove gradlew but before that, is there a better solution?

Resources