Jenkins's maven release plugin - How to set parent project version? - jenkins

I have a multi-modules project that I want to release with Jenkins. I use Maven 3.3.1, Jenkins 1.651.3 and maven-release-plugin 0.14.0
I create one job for the parent project and one job for each sub project.
Here is the parent configuration :
pom.xml :
<groupId>parent.group.id</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<scm>
<url>http://mygitrepo/parent-project.git</url>
<connection>scm:git:git://mygitrepo/parent-project.git</connection>
<developerConnection>scm:git:http://mygitrepo/parent-project.git</developerConnection>
</scm>
Jenkins config :
When I perform maven release for the parent project, it works.
Now I do the same thing for a sub project.
pom.xml :
<parent>
<groupId>parent.group.id</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<scm>
<url>http://mygitrepo/sub-project.git</url>
<connection>scm:git:git://mygitrepo/sub-project.git</connection>
<developerConnection>scm:git:http://mygitrepo/sub-project.git</developerConnection>
</scm>
With the same Jenkins config. I got this error :
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.3.2:prepare (default-cli) on project sub-project: Can't release project due to non released dependencies :
[ERROR] parent.group.id:parent-artifact:pom:1.0.0-SNAPSHOT
The plugin doesn't seem to replace the parent version by the release version.
I read in the maven release plugin's documentation that I can use "-Dproject.dev" and "-Dproject.rel" to specify the parent version to use.
So I tried this :
-Dproject.dev.parent.group.id:parent-artifact=1.0.1-SNAPSHO
-Dproject.rel.parent.group.id:parent-artifact=1.0.0
release:clean release:prepare release:perform -X
-Dproject.dev.parent.group.id:parent-artifact:pom=1.0.1-SNAPSHOT
-Dproject.rel.parent.group.id:parent-artifact:pom=1.0.0
release:clean release:prepare release:perform -X
-Dproject.dev.parent.group.id:parent-artifact:pom:1.0.0-SNAPSHOT=1.0.1-SNAPSHOT
-Dproject.rel.parent.group.id:parent-artifact:pom:1.0.0-SNAPSHOT=1.0.0
release:clean release:prepare release:perform -X
None of this solve the problem.
How can I configure Jenkins plugin to set the parent-project version ?

As an alternative you could use the Versions Maven Plugin to set the parent version in an own build step:
mvn versions:update-parent
or to set a specific version
mvn versions:update-parent "-DparentVersion=[1.2.0]"

Related

Jenkins, Gradle : How to publish Dependency report to Sonar Dashboard

Currently we're using Jenkins free style job for Gradle project and using following commands to run Sonar and Dependencycheck
./gradlew clean build sonarqube dependencyCheckAnalyze \
and I'm getting following message
Analyzing /opt/jenkins_slave_home/workspace/AA/package-lock.json - however, the node_modules directory does not exist. Please run npm install prior to running dependency-check
Generating report for project AA_ArbitraryBuild
Found 0 vulnerabilities in project AA
and we can able to see a file inside "ws/build/reports/" but it dint scanned anything.
Following are the "build.gardle" file
buildscript {
repositories {
maven { url artifactoryRepoUrl }
mavenCentral()
}
dependencies {
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7'
classpath 'org.owasp:dependency-check-gradle:6.0.3'
}
}
apply plugin: 'org.sonarqube'
apply plugin: 'org.owasp.dependencycheck'
sonarqube {
properties {
property 'sonar.projectName', sonarProjectName
property 'sonar.projectKey', sonarProjectKey
property 'sonar.host.url', sonarHostUrl
property 'sonar.login', sonarAuthToken
property 'sonar.dependencyCheck.reportPath', sonarDependencyCheckReport
property 'sonar.dependencyCheck.htmlReportPath', sonarDependencyCheckHTMLReport
}
}
Can you plz help on what are the additional steps that I need to add.
You've got all you need to push result to sonar. Make sure that you provide right path for your owasp vulnerabilities report for sonar plugin. It's sonar.dependencyCheck.reportPath and should point to build/reports directroy, and if you produce html report file you can point it with sonar.dependencyCheck.htmlReportPath.

Set project version in Jenkins Pipeline with the Artifactory Gradle plugin

We want to define the project version for our Gradle project during the build of our project in the Jenkins pipeline, which will include a timestamp and a git-commit-id. (20180625180158-b8ad8df0dc0356a91707eaa241de7d62df6a29f2)
void defineVersion() {
sh "git rev-parse HEAD > .git/commit-id"
commitId = readFile('.git/commit-id')
timestamp = getCurrentTimestamp()
version = timestamp+'-'+commitId
}
This function will determine the version I want to publish our artifact with.
Next I use the Artifactory Gradle plugin to publish, but I can't find a way to set/override the project version. I want the jar to be published with version 20180625180158-b8ad8df0dc0356a91707eaa241de7d62df6a29f2
version = defineVersion() // how can we incorperate this version in our gradle build/publish?
gradleBuild = Artifactory.newGradleBuild()
gradleBuild.useWrapper = true
gradleBuild.deployer(
repo: env.BRANCH_NAME == 'master' ? 'libs-releases-local' : 'libs-snapshots-local',
server: Artifactory.server('artifactory-global'))
gradleBuild.run tasks: 'clean build artifactoryPublish'
How can we achieve this? Also I would like to pass other parameters like -x test to the run command to skip tests in this stage.
Apparently you can add parameters throug the switches parameter: https://jenkins.io/doc/pipeline/steps/artifactory/
With this you add the necessary parameters like '-x test -Pversion=' + version
For my use case I added a version property to my build.gradle: version = "${version}" so it can be overridden with the command above.

How to inject Jenkins environmental variables into pom.xml

I'm developing plugin for Atlassian Jira and trying to get the version number of my plugin to be equal Jenkins BUILD_NUMBER environment variable.
Plugin is built inside docker container using command:
docker run --rm --volume $PWD/src/jira_plugin/:/opt/atlas/ codeclou/docker-atlassian-sdk:latest atlas-package
POM.xml:
<project>
<version>${jenkins.buildNumber}</version>
...
<properties>
<jenkins.buildNumber>${env.BUILD_NUMBER}</jenkins.buildNumber>
</properties>
</project>
Result:
[INFO] Building jar: /opt/atlas/target/test-null.jar ..[ERROR] Failed
to execute goal
com.atlassian.maven.plugins:maven-jira-plugin:6.3.15:generate-obr-artifact
(default-generate-obr-artifact) on project test: Source
'/opt/atlas/target/test${env.BUILD_NUMBER}.jar' does not exist
Nevertheless command
echo `printenv`
in docker correctly displays BUILD_NUMBER var.
Question:
What should I add in pom.xml to inject BUILD_NUMBER var in pom.xml and to display version correctly?
Any help would be greatly appreciated.
I figured it out:
docker run -e BUILD_NUMBER="${BUILD_NUMBER}" ...
So the variable will be injected and can be used in pom.xml.

Generate pom file for non-Maven artifact in Jenkins

I have a tar.gz artifact that gets uploaded to Artifactory using Jenkins. Since this artifact is not based on a Maven project, there is no pom file and thus no maven-metadata.xml being generated in Artifactory. The latter being very useful for versioning, etc.
Is there any way to have Jenkins generate a pom file for a non-Maven project?
With a manual deploy on artifactory you can generate a pom.xml ,unfortunately this option is not available on the jenkins artifactory's plugin, but you can maybe create you own shell script , like :
#!/bin/bash
groupId=com.mycompany
artifactId=myproject
version=1.0.0
echo '<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>'$groupId'</groupId>
<artifactId>'$artifactId'</artifactId>
<version>'$version'</version>
<description>Artifactory auto generated POM</description>
</project>' >> pom.xml
Anyway depending on your build tool and how you create the tar.gz package you can may be find more solutions ..
Regards

Call Ant jar target from Gradle build file

How can I call the Ant target 'jar' from the Gradle build file? I've tried a number of things to no avail. Renaming the 'jar' target in the Ant build file is not an option. I'm in the process of converting an Ant based build system to Gradle and the first required step is to call all the Ant targets from Gradle.
The 'jar' task is a default Gradle task so I'm overwriting/overriding it but I need to call my similarly named Ant target.
Gradle build.gradle file:
// Prevents error "Cannot add task {taskname} as a task with that name already exists"
ant.project.addTarget('clean', new org.apache.tools.ant.Target())
ant.project.addTarget('jar', new org.apache.tools.ant.Target())
ant.project.addTarget('test', new org.apache.tools.ant.Target())
ant.project.addTarget('javadoc', new org.apache.tools.ant.Target())
ant.importBuild 'build.xml'
task jar(overwrite: true) {
println 'jar'
}
task clean(overwrite: true) {
println 'clean'
}
task test(overwrite: true) {
println 'test'
}
and my Ant build.xml:
<target name='jar' description='jar'>
<echo>Called jar task in ant build</echo>
</target>
Running Gradle v1.2
------------------------------------------------------------
Gradle 1.2
------------------------------------------------------------
Gradle build time: Wednesday, September 12, 2012 10:46:02 AM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Ivy: 2.2.0
JVM: 1.6.0_25 (Sun Microsystems Inc. 20.0-b11)
OS: Linux 2.6.37.6 amd64
Gradle only adds a jar task when you apply the Java plugin, which you shouldn't do for the project into which you import the Ant build. I don't think you should ever call ant.project.addTarget from a Gradle build script.

Resources