I am using groovy script in jenkins pipeline job and I need maven to execute my stage.
I do not have control of .profile as it is running on slave. I need to set the Maven in my groovy script so that I can run man clean compile.
maven is located at /opt/runtime/apache-maven-3.3.3/bin/mvn.
I am very new to groovy and want to know how to use this.
You can do something like this:
// Maven location
def mvnHome = tool name: 'maven 3_3_9', type: 'hudson.tasks.Maven$MavenInstallation'
env.MAVEN_HOME = mvnHome
// Begin Compile
stage concurrency: 1, name: 'compile'
sh '''${MAVEN_HOME}/bin/mvn clean compile'''
Please note that the tool name comes from the Maven configuration you have defined in the Global Tool Configuration page.
Related
I am migrating all my jobs from Jenkins v1.651.3 to Jenkins v2.263.1.
Currently I am passing a file stored in my Linux server as a property under Invoke top-level maven targets build step in Jenkins.
e.g. property.a=/home/user1/props/a.properties and property.b=/home/user1/props/b.properties
In Jenkins v2.263.1, I am running all my jobs in a docker container which is created dynamically.
Where and how can I store this file so that I can pass this as a property to maven build like -Dproperty.a=/home/user1/props/a.properties and -Dproperty.b=/home/user1/props/b.properties
I tried adding these files in Managed Files section under Manage Jenkins option and passing these as -Dproperty.a=a.properties to maven build through Jenkinsfile, however, it did not work. Not sure if it's the right way.
Please let me now if there's a way to handle this.
Thanks in advance!
I have a maven Jenkins job, that is part of a multijob. Τhis used to run always on windows nodes, but currently we have created some linux nodes, too, that we want to utilize.
The thing is that inside the job,at build step you have to choose:
This maven version applies when building the job. For example the maven version that applied here,, is linked to the path were maven is installed for windows. So this path, is applicable only for windows nodes, therefore, the job cannot be run by a linux node. Is there a workaround for this?
thanks!
You can achieve it using Jenkins pipeline. Just set two maven config in Jenkins > global tool configuration. One for windows maven and other for linux.
node {
def mvnHome
stage('Build') {
// Run the maven build
if (isUnix()) {
mvnHome = tool 'Maven-linux'
sh "'${mvnHome}/bin/mvn' clean package "
} else {
mvnHome = tool 'Maven-windows'
bat(/"${mvnHome}\bin\mvn" clean package /)
}
}
Another way to do it is to use Jenkins Conditional build step plugin.
Create freestyle job
In Build Step select conditional step single or multiple.
In the Run? you can select execute shell
In Builder select invoke-top-level-maven-target and select maven-linux in maven version. Then you can configure maven goals and other steps
In Build Step on again select conditional step single or multiple.
In the Run? you can select execute windows batch command
In Builder select invoke-top-level-maven-target and select maven-window in maven version. Then you can configure maven goals and other steps
Configure your linux slaves:
Manage jenkins > manage nodes > %node > configure > Node properties
Then specify mvn and java located on your slave and fill in Tool ocations
I have 2 groovy class files under one rootProject. I want to run just one of them without running the other one. How can I do that using intellij command line and command prompt. Right now when I run the rootProject both files run one after the other. Is there any setting required build.gradle file then please explain hoow can I do that as I am very new to gradle and don't know much about it. Thanks!
If you have only those two groovy scripts (and there is nothing to compile etc.), you do not need Gradle to run it at all. From command line call
groovy <filename>.groovy
To run the script directly from Gradle Script, use
new GroovyShell().run(file('somePath'))
Yes you can create intellij config to run your script.
Follow these steps:
1) Edit configuration
2) Add new Groovy configuration
3) Browse you script
4) Run your script
I have a Workflow job with the following Groovy code segment:
def mvnHome = tool 'M3'
sh "${mvnHome}/bin/mvn install"
When I run it I get the error:
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
I have installed JDK in "Jenkins > Manage Jenkins > Configure System" and gave the name "JAVA_HOME". When a run a "Freestyle project" job to test the installation:
echo "JDK: $JAVA_HOME"
The output is good:
JDK: /var/lib/jenkins/tools/hudson.model.JDK/JAVA_HOME
I also tried restarting Jenkins but it didn't help. Please advise.
FYI, I started to use the following fix by changing the Workflow code like this:
def mvnHome = tool 'M3'
def javaHome = tool 'JAVA_HOME'
sh "JAVA_HOME=$javaHome $mvnHome/bin/mvn install"
I don't know if this is a correct solution or a hack.
Your final script looks correct. Maven generally requires a full JDK to run, not just a JRE. If your build slave does not already have one in its $PATH then you can use the tool step to modify $PATH and/or $JAVA_HOME to point to it.
This question is old but Jenkins recommends doing it this way:
withEnv(["JAVA_HOME=${ tool 'JAVA_HOME' }", "PATH+MAVEN=${tool 'M3'}/bin"]) {
sh "mvn install"
}
You can see it here: Jenkins Pipeline Examples. It works for me
I have a Maven job in Jenkins. Before the actual build step I have an "Execute shell" pre-build step. In that shell I set a variable:
REVISION=$(cat .build_revision)
I would like to use that variable in the Maven build job in "Goals and options":
clean install -Drevision=${REVISION}
But that does not work! The "Drevision" is set to "${REVISION}" not the actual value of ${REVISION}. Output:
Executing Maven: -B -f /home/gerrit/.jenkins/jobs/<job_name>/workspace/pom.xml clean install -Drevision=${REVISION}
It works with Jenkins environment variables:
clean install -Dbuild=${BUILD_NUMBER}
It sets "Dbuild" to the actual build number. Output:
Executing Maven: -B -f /home/gerrit/.jenkins/jobs/<job_name>/workspace/pom.xml clean install -Dbuild=54
My question: How to use a shell variable in Maven "Goals and options"??
EDIT:
I tried using Jenkins EnvInject Plugin to "Inject environment variables" after the pre-build shell, and my variable is now accessible by e.g. post-build shells, but it is still not available in Maven "Goals and options".
Then it is possible to set "Inject environment variables to the build process" using the EnvInject Plugin, which actually makes those variables available in Maven "Goals and options", but those are set right after SCM checkout, i.e. before pre-build steps, and do not support expression evaluations.
You're on the right track here, but missed a third feature of the EnvInject-Plugin: The "Inject environment variables" build step that can inject variables into following build steps based on the result of a script or properties.
We're using the EnvInject plugin just like that; A script sets up a resource and communicates its parameters using properties that are then propagated by the plugin as environment variables.
i.e. setting up a temporary database for the build:
I had a very similar problem, trying to compute a build version and inject it into the build. After running into all the same issues (not expanding, etc), I used the "Generate environment variables from script" option, which interprets the output as tag=value pairs into Jenkins variables. The script :
#generate a version code that is high enough to surpass previously published clients
val=`expr 150000 + $BUILD_NUMBER`
echo VERSION_CODE=$val
After this, I was able to inject $VERSION_CODE into maven as follows :
-Dbuild.vercode=${VERSION_CODE}
Hope that works for you.
This issue is caused by a bug in the Jenkins Maven Project Plugin as detailed in this bug report opened on 2012-06-22. The plugin has not yet been fixed as of version 2.1.
A fix has been proposed for the Maven Project Plugin, but has not yet been integrated. Here is the link to the pull request: https://github.com/jenkinsci/maven-plugin/pull/14
If you build the plugin yourself with the pull request patch applied, the variables are injected and made available to the "goals and options" field as expected.
I see there is an accepted answer, but for a newbie in Jenkins I found it hard to grasp it all. That's why I would add a bit more detail in this answer and show how I did it.
As #jjungnickel suggested you need to have EnvInject Plugin installed for Jenkins. Then in the Build section > Add build step you'll get option "Inject environment variables".
Basically the idea is:
Add variables you want to access later to a file (might be added by a shell script or it could be file from the file system).
Inject the file with the variables.
Use the variables.
Here a sample setup:
Since I want to use them in maven goal I need to check the Inject Build Variables checkbox.
Then at the end of the build I remove the file just because I want to keep the environment as it was before the build.
I think your best shot is to try the EnvInject plugin for this along with your initial pre-scm step.
You run the pre-scm as you already do.
You use the env inject to load the file for the main job's build steps
Consider loading your file's content (properties format) or execute a script which will load the file as you want and make a variable available for the rest of the job with the "Prepare an environment for the run" option.
I hope this helps.
I needed to resolve the variables before the injection was done so I put this in script content:
Example: (note it doesn't seem possible to simply export variables here so I wrote to files and the help section in jenkins seems to indicate this is expected)
git ls-tree --name-only -r ${sha1} | grep -v -c "*\.md" > diff.bak
git diff origin/master --shortstat | grep "1 files changed" && echo 1 > count.bak || echo 0 > count.bak
I then added this in the groovy script, using the output files I can create a map:
def procDiff = "cat $WORKSPACE/diff.bak".execute()
def procCount = "cat $WORKSPACE/count.bak".execute()
def diff = procDiff.text
def count = procCount.text
print "string val = $diff and count = $count "
if ("0".equals(diff) || !"1".equals(count)){
def map = ["GOAL": "clean verify"]
return map
} else {
def map = ["GOAL": "clean"]
return map
}
Then I could reference $GOAL in my maven build to conditionally trigger a "clean" or a "clean verify" based on the type of PR raised.