Let's say I have a custom property in my pom.xml set like this:
<properties>
<app>com.myProject.app</app>
</properties>
How can I access it in my jenkinsfile?
This:
def pom = readMavenPom file: 'pom.xml'
def appName = pom.app
returns
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field org.apache.maven.model.Model app
Thanks in advance!
I know two approaches:
Use properties-maven-plugin to write the properties to a file. Use readProperties in the Jenkinsfile to read the properties.
Works only if properties aren't needed until after Maven ran.
Also, with the right circumstances, the properties file may be the stale one from a previous run, which is insiduous because the property values will be right anyway 99.9% of the time.
Use pom = readMavenPom 'path/to/pom.xml'. Afterwards, access the property like this: pom.properties['com.myProject.app'].
I like approach 2 much better: No extra plugin configuration in the POM, no file written, less sequencing constraints, less fragilities.
In pipeline style, inside Jenkinsfile you can access the value as follows
pipeline {
environment {
POM_APP = readMavenPom().getProperties().getProperty('app')
}
stages{
stage('app stage'){
steps{
script{
sh """
echo ${POM_APP}
"""
}
}
}
}
Read a maven project file
try this:
IMAGE = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
jenkins pipeline add this stage.
more see
stage('info')
{
steps
{
script
{
def version = sh script: 'mvn help:evaluate -Dexpression=project.version -q -DforceStdout', returnStdout: true
def artifactId = sh script: 'mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout', returnStdout: true
}
}
}
Related
I have wrote a Jenkins Pipeline Groovy for executing multiple project maven sonar analysis. The code is working fine but the issue is that sometimes build fails for some projects which I need to track it properly. My executeMavenSonarBuild function is given as below
def executeMavenSonarBuild(projectName) {
stage ('Execute Maven Build for '+projectName)
{
sh """ {
cd ${projectName}/
mvn clean install verify sonar:sonar
} || {
echo 'Build Failed'
}
"""
}
return true;
}
If build fails it prints echo 'Build Failed' but how we can return a false Boolean as the return to the function.
You have to get the status from the mvn call itself..which should look like this:
def result = sh ( script: 'mvn ...', returnStatus: true)
i'm trying to convert my freestyle job in a scripted pipeline, i'm using gradle for the build and artifactory to resolve my dependecies and publish artifacts.
My build is parametraized with 3 params and in freestyle job when i configure Invoke gradle script I have the checkbox Pass all job parameters as System properties and in my
project.gradle file I use the params with System.getProperty() command.
Now implementing my pipeline I define the job parameters, I have these like enviromnent variables in the Jenkinsfile but can I pass this params to the gradle task?
Following the official tutorial to use Artifactory-Gradle plugin in pipeline I run my build with :
buildinfo = rtGradle.run rootDir: "workspace/project/", buildFile: 'project.gradle', tasks: "cleanJunitPlatformTest build"
Can I pass params to gradle build and use in my .gradle file?
Thank's
Yes, you can. If using sh ''' then switch that to sh """ to get the expanded values
Jenkinsfile
#!/usr/bin/env groovy
pipeline {
agent any
parameters {
string(name: 'firstParam', defaultValue: 'AAA', description: '')
string(name: 'secondParam', defaultValue: 'BBB', description: '')
string(name: 'thirdParam', defaultValue: 'CCC', description: '')
}
stages {
stage ('compile') {
steps {
sh """
gradle -PfirstParam=${params.firstParam} -PsecondParam=${params.secondParam} -PthirdParam=${params.thirdParam} clean build
sh """
}
}
}
}
and inside your build.gradle you can access them as
def firstParam = project.getProperty("firstParam")
You can also use SystemProperty with -D prefix as compared to project property with -P. In that case you can get the value inside build.gradle as
def firstParam = System.getProperty("firstParam")
I went through the following link and successfully implemented a task which calls build.gradle file from another project. i.e. solution provided by #karl worked for me.
But I need something up on that.
Can somebody help me to know how I can pass command line arguments while calling another build.gradle? Command line argument should be the variable which I have generated from my current build.gradle file.
In my case, I am defining a buildNumber and doing something like this:
def buildNumber = '10.0.0.1'
def projectToBuild = 'projectName'
def projectPath = "Path_till_my_Project_Dir"
task executeSubProj << {
def tempTask = tasks.create(name: "execute_$projectToBuild", type: GradleBuild)
// ****** I need to pass buildNumber as command line argument in "$projectPath/$projectToBuild/build.gradle" ******
tempTask.tasks = ['build']
tempTask.buildFile = "$projectPath/$projectToBuild/build.gradle"
tempTask.execute()
}
You should never call execute directly on any gradle object. The fact it's feasible doesn't mean it should be done and it's highly discouraged since you intrude internal gradle's execution graph structure.
What you need is a task of type GradleBuild which has StartParameter field that can be used to carry build options.
So:
task buildP2(type: GradleBuild) {
buildFile = '../p2/build.gradle'
startParameter.projectProperties = [lol: 'lol']
}
Full demo can be found here, navigate to p1 directory and run gradle buildP2.
You should modify your script in the following way:
def buildNumber = '10.0.0.1'
def projectToBuild = 'projectName'
def projectPath = "Path_till_my_Project_Dir"
task executeSubProj(type: GradleBuild) {
buildFile = "$projectPath/$projectToBuild/build.gradle"
tasks = ['build']
startParameter.projectProperties = [buildNumber: '10.0.0.1']
}
In the project that is executed use project.findProperty('buildNumber') to get the required value.
Within my Jenkinsfile I would like to read some metadata from a Gradle buildscript, e.g. group, baseNameand version. With Maven, I can do something like this:
def pom = readMavenPom file: 'pom.xml'
env.POM_VERSION = pom.version
env.POM_ARTIFACT_ID = pom.artifactId
env.POM_GROUP_ID = pom.groupId
Question is: how can I do the same thing with Gradle, given I have a build.gradle that looks like this:
ext {
group = 'my.group.com'
baseName = 'myapplication'
version = '0.5.2-SNAPSHOT'
}
Maybe there is a "trick" to read Gradle build information in a Jenkinsfile since both is Groovy, but I am a Groovy newb :)
I can execute a pom.xml with goals using AntBuilder like so.
def ant = new AntBuilder()
ant.sequential {
exec(executable:'mvn') {
arg(value:'clean')
arg(value:'install')
}
}
But how do I specify the execution directory to the AntBuilder? I'd like to just pass an absolute path.
For the record I've tried.
ant.project.setProperty('basedir', "${serviceRootDir}/")
and
ant.sequential {
mkdir(dir:"${serviceRootDir}/")...
You'd think this would be clear in the doc.
This works for me:
ant.exec(executable:"ls", dir:"/your/desired/directory")
It executes ls in the given directory, so mvn should work.