Jenkins multi-configuration project user-defined axis environment variable - maven

I am trying to set up a multi-configuration project in Jenkins with a single user-defined axis (call it "axis"). The value associated with each configuration would then be used to invoke top-level maven targets in ${axis}/pom.xml. The trouble is, I can't find the proper syntax for this, if indeed it does exist (${axis}, $axis, $AXIS, and ${env.axis} all fail). I would think it would be shell syntax, which it doesn't seem to be, but regardless it should be either simple or impossible. Is it possible?

Is your Jenkins installation running on Windows?
For each Custom Axis, an environment variable is created. You can refer to your custom axis using ${axis} if your server is running Linux, but on Windows you must refer to it as %axis%

${axis}/pom.xml should work in my experience.

I appreciate that OP has solved this in a different way, but for the record the following works in Hudson in Windows and Linux. I haven't tried it in Jenkins:
The syntax you need for this is simply $axis/pom.xml
I defined an axis of BuildProfile=compile unitTest integrationTest
And in the Maven 3 configuration (under Advanced properties) a POM file of $BuildProfile/pom.xml
The resulting builds gave the following output in the console (edited for brevity):
[1.7.0_25] $ C:\Users...\bin\mvn.bat clean install -V -B
-DBuildProfile=compile -f compile/pom.xml
[1.7.0_25] $ C:\Users...\bin\mvn.bat clean install -V -B
-DBuildProfile=unitTest -f unitTest/pom.xml
[1.7.0_25] $ C:\Users...\bin\mvn.bat clean install -V -B
-DBuildProfile=integrationTests -f integrationTests/pom.xml

We use a jdk axis and just use the name of the jdk so I think you could just use axis/pom.xml

Related

How do I get rid of the Executing/Progress Bar when running a Gradle application?

I am trying to build a console based application but whenver I run
./gradlew run
I get thr progress bar which looks like this:
<=========----> 75% EXECUTING [29s]
Is there anyway I can remove this bar (which hangs out until the application ends)? Or is there a better way to write console base applications with gradle?
The version is 4.3.1
You can configure the Gradle log format using console command line parameter, as described here : https://docs.gradle.org/current/userguide/command_line_interface.html#rich_console
Try with : ./gradlew run --console=plain
Another way of doing this seems to be by setting the TERM environment variable to dumb.
Try TERM=dumb ./gradlew run
To make it a default behavior, add org.gradle.console=plain to gradle.properties file (prerequisite Gradle version is more than 4.x).
For one time execution add --console=plain flag to your command.
I know your question is specific to using the gradle wrapper, but if you're using the Gradle Tooling API, you can control the color output via
setColorOutput. Combined with the --quiet argument (set via withArguments), this results in plain-text output.

Where does Jenkins store the project source

I have a Jenkins job that uses a script to build my project. On the following line, the script fails mvn -e -X -Dgit='$git' release:prepare.
Because I want to search for the cause of this, I want to go to the Jenkins server and run mvn -e -X -Dgit='$git' release:prepare from the command line, to see if it works.
Does Jenkins store the projects' source code somewhere, such that I can go to that folder and call Maven?
If yes, then where?
Yes, It Stores the project files for the job by default at
/var/lib/jenkins/workspace/{your-job-name}
This is where jenkins suppose the project files to be present or it pulls it from a source before start working/building from it.
Quote from Andrew M.:
"Hudson/Jenkins doesn't quite work that way. It stores configurations and job information in /var/lib/jenkins by default (if you're using the .deb package). If you want to setup persistence for a specific application, that's something you'll want to handle yourself - Hudson is a continuous integration server, not a test framework.
Check out the Wiki article on Continuous Integration for an overview of what to expect."
From this Question on serverfault.
This worked for me:
/var/jenkins/workspace/JobNameExample
but, if your build machine (node) is a different than the one where Jenkins is running (manager), You need specify it:
/var/jenkins/workspace/JobNameExample/label/NodeName
Where you can define label too:
jenkins stores its workspace files currently in /var/jenkins_home/workspace/project_name
I am running from docker though!

How can I make “gradle --console=rich” the default?

Along the lines of this answer (which works for me, BTW) and the javadocs, I tried
gradle.startParameter.consoleOutput = org.gradle.api.logging.configuration.ConsoleOutput.Rich
in my ~/.gradle/init.gradle. However, I still need --console=rich to get color output. Why?
Tested with Gradle 2.14.1 and 3.2.1.
Terminal is cygwin urxvt with TERM variable set to rxvt-unicode-256color.
Since Gradle 4.3 you can use org.gradle.console property in gradle.properties:
org.gradle.console=rich
A new console verbose mode will print outcomes of all tasks (like UP-TO-DATE) like Gradle 3.5 and earlier did. You can set this via --console=verbose or by a new Gradle property org.gradle.console=(plain rich verbose).
I am not sure if you can force the rich console from a gradle script, as the detection happens likely before the script is interpreted.
NativeServices class provides the integration with the console. If you look at the source code, there are two messages possibly printed in log:
Native-platform terminal integration is not available. Continuing with fallback.
Unable to load from native-platform backed ConsoleDetector. Continuing with fallback.
The latter might give you more information why. Try running the gradle script with --debug. You will likely find out that you are missing a native library that is either not available in cygwin or it is, but is not on library path.
I believe it works when you specify the rich console from the command line, because gradle forces the colours even though the console doesn't indicate it supports them.
Does it work if you don't use the cygwin console in Windows native command line or maybe GitBash?
There is a workaround how you can make this work. You can create an alias in cygwin that will always add the --console=rich.
If you are using gradle wrapper, you can edit the gradlew script and add the command line parameter. To make it automated, you can change the wrapper task to alter your script in the doLast part.
Create a file called gradle.properties inside your ~/.gradle/ folder.
Inside gradle.properties, add the line org.gradle.console=rich.
Each builds will run under --console=rich automatically because the new gradle.properties will be merged with the gradle.properties of your project.
If your project's gradle.properties contains the same tag as the local file, your project's will be used overriding the local file's
If you are on Linux/Mac set
alias gradle='gradle --console rich'
in your ~/.bashrc.
In Gradle Wrapper, add the following line:
org.gradle.console=rich
to ./gradle.properties in the root folder, where the gradlew script is located.

Jenkins: How to use a variable from a pre-build shell in the Maven "Goals and options"

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.

Maven ignores settings.xml file

I have added proxy configuration in settigns.xml file, but it is not used by Maven, i confirmed this by making the settings.xml file invalid. I ran the maven install command to update settings and global-settings to point to the correct file, still no luck. I am using maven3.0.4.
Try running Maven with the -X option. It should print as part of the debug output which settings file is being used.
Since you already tried it with an invalid file, I bet that something is wrong with the location of your file.
It's almost to stupid to tell, but it might save some time for somebody else: If you're using a new computer, make sure file extensions are displayed. Otherwise your "settings.xml" file probably is a "settings.xml.txt" file in fact...
Make sure it is in the right directory (HOME/.m2/settings.xml)
You can find the relevant paths and a proxy example here: Maven proxy settings not working
And of course the reference is always useful: https://maven.apache.org/settings.html
have you tried with these options: from the command line to specify the settings file?
mvn -o –Dmaven.repo.local=$HOME/.my_m2path/repository clean install --settings $HOME/.my_m2path/settings.xml Dcheckstyle.skip=true –DskipTests
Some options that might not be necessary
-o is for offline (unless you have all your repos in your m2, its suggested to skip this option)
skip tests is for skipping tests while building
–Dmaven.repo.local - repo path - if you are having own repo path, then use this option
--settings $HOME/.my_m2path/settings.xml (remember there is space between settings and the path)
Since there is no accepted answer, and I encountered that problem today and other answers proved unhlepful as the file path was correct:
The solution is to restart your computer. No, seriously. After restart maven is guaranteed to read settings.xml file again and use whatever changes you made.
For those using Linux
In the Ubuntu package repository (and probably other disto's too), there are two maven packages: maven and maven2. For some reason, I had maven2 installed, which seems to ignore settings.xml in ~/.m2.
As a solution, I removed it using
sudo apt-get purge maven2
and installed the other one with
sudo apt-get install maven
What's going on?
I couldn't find a reliable source, but apparently, maven2 is an older version (2.x), as the latest maven has version 3.x and is served with maven.
Please make sure you are using the settings.xml which you modified.
for example: your using IDE's embedded maven which using different settings.xml in you operation system.
You can set the path of settings file in Eclipse* as :
In the menubar goto Window -> Preferences
In Preferences Dialog, Goto Maven Section(On the left) and Expand it.
Click on UserSettings.
Add the path of settings file using Browse. The default for most users will be (C:\Users\.m2\settings.xml). It will be shown in grey but you need to actually enter the location.
Click on Update Settings !
You can also enable debug logs and stack traces for debugging by clicking on the Maven Section in the Preferences dialog and Checking the box against the label "Debug Output".
P.S. I am currently using Eclipse(Neon) on Windows 10 x64.
I have also been facing the same issue. I removed the file and folder, but still maven was still picking the settings.
For me, restarting the system solved the issue.
What I found was that even if I set my own maven setting file using the --settings or -s command such as:
mvn -s $MAVEN_HOME/libexec/conf/my-settings.xml clean
Maven was still reading the default global setting.xml
Two ways that one can slove this:
Rename the setting.xml to something like setting.xml.bak in the $MAVEN_HOME/conf dicrectory.
The preferred approach add global to the command -gs hence the final command becomes:
mvn -gs $MAVEN_HOME/libexec/conf/my-settings.xml clean

Resources