Is there a Maven plugin to create Jenkins job - maven

I have about a hundred maven projects for which I would like to create Jenkins jobs. Instead of creating the jobs by hand, with the likely attendant mistakes, is there a way to create from a maven plugin?

There is the job-dsl plugin which allows you to build jobs in a groovy DSL
This works by providing a build step in a separate job
You can loop through your jobs as explained here
def giturl = 'https://github.com/quidryan/aws-sdk-test.git'
for(i in 0..10) {
job("DSL-Tutorial-1-Test-${i}") {
scm {
git(giturl)
}
steps {
maven("test -Dtest.suite=${i}")
}
}
}

Related

Jenkins maven project violation report in pipeline project

I am trying to convert jenkins maven project to pipeline project, we have mvn clean install step and next violation plugin can someone help me How to include violation report in pipeline project (check style and findbugs)
In declarative style, using the new Warnings Next Generation plugin, you would do something like
pipeline {
agent any
stages {
... pre-conditions & other stuff previously handled by your jenkins maven job ...
stage('Build') {
steps {
withMaven {
sh 'mvn clean install'
}
}
}
... post-conditions previously handled your jenkins maven job ...
}
post {
always {
recordIssues(
enabledForFailure: true, aggregatingResults: true,
tools: [java(), checkStyle(pattern: 'checkstyle-result.xml', reportEncoding: 'UTF-8'), findBugs(pattern: 'findbugs.xml')]
)
}
}
}
See the pipeline documentation page for more details about syntax etc

Declarative Jenkisfile trigger downstream pipeline when dependency is built

Is there any way to use the trigger downstream pipeline function when a dependency is built from the Pipeline Maven Integration plugin in a declarative Jenkisfile?
There is a promising TODO on the Jenkis plugin site.
Using "withMaven" with "docker.image(...).inside{...}" to a Jenkins Declarative Pipeline
TODO provide a sample with Jenkins Declarative Pipeline
Has anybody done that yet?
In my use case, I would like to trigger all downstream pipelines when a snapshot is built without using the GUI (I would prefer an infrastructure as code approach).
I'm not entirely sure, where the exact problem should be. Theoretically it should be
pipeline {
agent {
docker {
image 'openjdk:8-jdk'
}
}
stages {
stage('Test') {
withMaven() {
sh 'mvn clean install'
}
}
}
}
Correct syntax would be to use $MVN_CMD in the withMaven(). That will load the maven spy of the Jenkins Maven plugin.
pipeline {
agent {
docker {
image 'openjdk:8-jdk'
}
}
stages {
stage('Test') {
withMaven() {
sh '$MVN_CMD clean install'
}
}
}
}

How to run two methods in Gradle in parallel mode?

I got a Gradle task which calls two methods. The methods are independent of each other so I want to start them parallel. What is the best way to achieve this?
Example:
task cucumber(dependsOn: 'testClasses') {
doLast {
// ...
// I want to call the next 2 methods in parallel
runSequentialTests(testEnvironment, tags)
runParallelTests(testEnvironment, tags)
}
}
def runSequentialTests(testEnvironment, tags) {
// execute cucumber tests via javaexec
}
def runParallelTests(testEnvironment, tags) {
// execute cucumber tests via exec sh script for each feature file (parallelism is done via GParsPool.withPool(5) {...}
}
Worth researching groovy parallel systems library gpars
GParsPool.withPool {
GParsPool.executeAsyncAndWait({runSequentialTests(testEnvironment, tags)}, {runParallelTests(testEnvironment, tags)})
}
Or maybe create a #ParallelizableTask
But I'm not sure what version of gradle you are using. And parallel is/was an incubating feature.
And needs to run build with --parallel if those have no dependencies between themselves Gradle should run them independently.
Alternatively, you can specify a property in your gradle.properties
C:\Users\<user>\.gradle\gradle.properties
org.gradle.parallel=true
I used the asynchronous invocations of GPars: http://www.gpars.org/webapp/guide/#_asynchronous_invocations
I got it to work with this. Thanks
GParsPool.withPool {
GParsPool.executeAsyncAndWait({runSequentialTests(testEnvironment, tags)}, {runParallelTests(testEnvironment, tags)})
}

parallelize code in a gradle task

I have a task which essentially performs the following:
['subproj1', 'subproj2'].each { proj ->
GradleRunner.create()
.withProjectDir(file("./examples/${proj}/"))
.withArguments('check')
.build()
}
The check is a system test and requires connecting to 3rd party services, so I would like to parallelize this.
Can this be done in gradle? If so, how?
I tried using java threading but the builds failed with errors which I can't remember what they were exactly, but they suggested that the gradle internal state had gotten corrupted.
Did you tried to use the experimental parallel task execution? On the first glance it's quite simple. You just call ./gradlew --parallel check (or if it turns out to work fine for you can also define this in your gradle.properties). This will start n threads (where n is the number cpu cores) which will execute your tasks. Each thread owns a certain project so the tasks of one project will never be executed in parallel.
You can override the number of tasks (or worker) by setting the property --max-workers at the command line or by setting org.gradle.workers.max=n at your gradle.properies.
If you are just interested in executing tests in parallel than you might try to set Test.setMaxParallelForks(int). That will cause the to execute the tests of one project (if I understood this right) in parallel (with the number of tasks you defined).
Hope that helps. Maybe the gradle documentation gives you some more insights: https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:parallel_execution
While multiproject build is the right way to go in the long term, my short term approach was to use GPars:
import groovyx.gpars.GParsPool
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.codehaus.gpars:gpars:1.1.0"
}
}
task XXXX << {
GParsPool.withPool(10) {
['subproj1', 'subproj2'].eachParallel { proj ->
GradleRunner.create()
.withProjectDir(file("./examples/${proj}/"))
.withArguments('check')
.build()
}
}
}

Gradle - Add additional task to existing task

I'm working on a project that uses EJB2s. The created EJB Jars require additional processing by the application server before they're bundled in the war/ear and deployed.
I have created a custom task that works to do the additional processing if I invoke it explicitly (gradle ejbDeploy), but am having trouble fitting it into the gradle multi-project lifecyle. I need to somehow add it to the build graph to execute automatically after the jar task.
My first attempt was to add it to jar with
jar.doLast{
ejbDeploy.execute()
}
which seems to work for arbitrary code blocks, but not for tasks
What's the recommended solution for this? I see three approaches:
Hook into the build graph and add it explicitly after the jar
task.
Set it up somehow in jar.doLast{}
Set it up as a prerequisite for the WAR task execution
Is there a recommended approach?
Thanks!
I would go for approach #3 and set it up as a dependency of the war task, e.g.:
war {
it.dependsOn ejbDeploy
...
}
I'm new to Gradle, but I would say the answer really depends on what you're trying to accomplish.
If you want to task to execute when someone runs the command gradle jar, then approach #3 won't be sufficient.
Here's what I did for something similar
classes {
doLast {
buildValdrConstraints.execute()
}
}
task buildValdrConstraints(type: JavaExec) {
main = 'com.github.valdr.cli.ValdrBeanValidation'
classpath = sourceSets.main.runtimeClasspath
args '-cf',valdrResourcePath + '/valdr-bean-validation.json'
}
Add the following, and then ejbDeploy will be executed right after jar, but before war
jar.finalizedBy ejbDeploy
See Gradle Docs. 18.11. Finalizer tasks

Resources