jenkins how to run from jenkins gradle specify testng.xml - jenkins

I'm working with jenkins and try to call a remote machine that has a java project with gradle config to run tests build.gradle
version '1.0-SNAPSHOT'
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
sourceCompatibility = 1.8
version = '1.0'
sourceSets {
jars
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(dir: "src/jars/resources", include: ['*.jar'])
compile group: 'org.testng', name: 'testng', version: '6.9.10'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.+'
compile group: 'io.appium', name: 'java-client', version: '4.1.2'
compile 'com.testdroid:testdroid-api:2.9'
compile group: 'org.json', name: 'json', version: '20141113'
}
compileJava {
options.encoding = "UTF-8"
}
test {
useTestNG {
suites 'src/main/resources/testng.xml'
}
}
but I want to run a custom testng2.xml! so I define in jenkins a parameter test and define their testng2.xml, but and it will show in the console
/Users/jenkins/Home/workspace/AutomationGradle/gradlew -Dtest=testng2.xml clean test -i
but I can't understand how to make gradle to get the dynamic parameter

Use it like that, with a default value set to testng.xml
ext.testFile = System.getProperty('Test_Plan') ?: 'testng.xml'
test {
useTestNG {
suites "src/main/resources/$testFile"
}
}
And run your command line with the test parameter
/Users/jenkins/Home/workspace/AutomationGradle/gradlew -DTest_Plan=testng2.xml clean test -i

Thank you, I'v worked a lot of time on this, so I'm writing what worked for me:
In the Jeninks I added a parameter:
TESTNGFILE
In the groovy file:
-DTestNG=$TESTNGFILE
build.gradle:
ext.testFile = System.getProperty('TestNG') ?: 'regression.xml'
test {
useTestNG {
options {
systemProperties(System.getProperties())
}
systemProperties = [
TESTNGFILE: System.getProperty('TESTNGFILE')
]
suites "/src/test/resources/testng/${testFile}"
testLogging.showStandardStreams = true
useDefaultListeners = true
}
}

Related

SonarQube not showing test Jacoco coverage for JUnit tests in a Gradle multi-project

I would like to show the test coverage of a multiple project Spring boot application build with Gradle 6.0. We currently use JUnit5.
The test coverage shows 0% in SonarQube even though a few first tests exists.
The build.gradle files in the top level project (https://github.com/OpenReqEU/eclipse-plugin-vogella/blob/master/server/build.gradle) has the following input:
plugins {
id "org.sonarqube" version "2.7"
id 'jacoco'
}
repositories {
jcenter()
}
subprojects {
apply plugin: 'jacoco'
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
jcenter()
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}
jacocoTestReport {
reports {
xml.enabled true
xml.destination file("${buildDir}/coverage-reports/coverage.xml")
//html.destination file("${buildDir}/coverage-reports")
}
}
ext {
springBootVersion = '2.1.1.RELEASE'
}
sourceCompatibility = 1.8
}
wrapper {
gradleVersion = '6.0'
}
In the Jenkins build we set the following parameters:
sonar.projectKey=eclipse-plugin-vogella
sonar.sources=server/com.vogella.prioritizer.server/src/main,server/com.vogella.prioritizer.server.bugzilla/src/main,server/com.vogella.prioritizer.server.issue.api/src/main
sonar.java.binaries=com.vogella.prioritizer.server/build/classes/java/main,com.vogella.prioritizer.server.bugzilla/build/classes/java/main,com.vogella.prioritizer.server.issue.api/build/classes/java/main
sonar.tests=server/com.vogella.prioritizer.server/src/test,server/com.vogella.prioritizer.server.bugzilla/src/test
sonar.coverage.jacoco.xmlReportsPath=server/com.vogella.prioritizer.server.bugzilla/build/jacoco/test.exec,server/com.vogella.prioritizer.server/build/jacoco/test.exec,server/com.vogella.prioritizer.server.issue.api/build/jacoco/test.exec
The result of the build shows an error:
INFO: parsing [/home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml]
ERROR: Reports path not found or is not a directory: /home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml
I see that each project has a generated ${buildDir}/coverage-reports/coverage.xml file but the root file is empty, which is expected as I did not configure anything related to this.
At some point I added a copy task which copied one of the generated xml files from one project into the root folder but the build job complained that the classes were not matching.
Does anybody know how this issue can be solved? I assume I must add a configuration to add a root coverage.xml file which is the aggregate of the individual ones but I have not found a solution for that.
I also tried to apply the jacoco to the root project but that also failed as the root project is not a Java project.
Need to generate report in xml format. Add the sonar property to the xml path as below.
jacocoTestReport {
reports {
xml.enabled true
}
}
sonarqube {
properties {
property "sonar.java.source", "1.8"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPaths", "build/reports/jacoco/test/jacocoTestReport.xml"
}
}
Run the gradle command with the jacocoTestReport
gradlew sonarqube jacocoTestReport
I managed to create the aggregated coverage.xml file by changing the top level build.gradle to:
plugins {
// id "org.sonarqube" version "2.7"
id 'jacoco'
}
repositories {
jcenter()
}
subprojects {
println name;
apply plugin: 'jacoco'
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
jcenter()
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}
jacocoTestReport {
reports {
xml.enabled true
}
}
ext {
springBootVersion = '2.1.1.RELEASE'
}
sourceCompatibility = 1.8
}
// run the build with ./gradlew clean build generateMergedReport
task generateMergedReport(type: JacocoReport) {
dependsOn = subprojects.test
additionalSourceDirs.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.setFrom files(subprojects.sourceSets.main.output)
executionData.setFrom project.fileTree(dir: '.', include: '**/build/jacoco/test.exec')
println executionData;
reports {
xml.enabled true
xml.destination file("../coverage-reports/coverage.xml")
}
}
wrapper {
gradleVersion = '6.0'
}
And changing the Jenkins to build generateMergedReport:
cd server && ./gradlew build generateMergedReport
The SonarQube properties where changed to:
sonar.projectKey=eclipse-plugin-vogella
sonar.sources=server/com.vogella.prioritizer.server/src/main,server/com.vogella.prioritizer.server.bugzilla/src/main,server/com.vogella.prioritizer.server.issue.api/src/main
sonar.java.binaries=com.vogella.prioritizer.server/build/classes/java/main,com.vogella.prioritizer.server.bugzilla/build/classes/java/main,com.vogella.prioritizer.server.issue.api/build/classes/java/main
Unfortunately SonarQube still doesnt find the coverage.xml file.
INFO: parsing [/home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml]
ERROR: Reports path not found or is not a directory: /home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml
Does someone have an idea of what is missing?

Jenkins Plugin Development Gradle Build

Building a Jenkins Plugin that creates a SimpleBuildStep.
It works with maven hpi:run but I need to switch it to gradle
My problem is that when I run gradle server I can see my custom plugin is installed but it is not in the build step.
I thought it was my versioning and I changed it several times. I'm wondering if my configuration is wrong.
I have a work directory that shows up and my plugin is shown in work/plugins/ with a .hpi and a .hpl file but it still doesn't work. It only works in maven it also doesn't show when I do a docker instance of jenkins (which is always at jenkins version 2)
I'm still assuming it is my build.gradle
plugins {
id "org.jenkins-ci.jpi" version "0.16.0"
}
jenkinsPlugin {
coreVersion = "2.0" // Version of Jenkins core this plugin depends on.
displayName = "Test Jenkins Plugin" // Human-readable name of plugin.
url = "http://wiki.jenkins-ci.org/display/JENKINS/SomePluginPage" // URL for plugin on Jenkins wiki or elsewhere.
shortName = "jetson" // Plugin ID, defaults to the project name without trailing '-plugin'
}
group 'test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jenkins-ci.main', name: 'ui-samples-plugin', version: '1.424.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
EDIT: Have it working. I can actually use my plugin in my instance now.
Changes:
After examining the hpl file and reading it. I realized that my Jenkins plugin wasn't even registering my classes. I realized cause my build.gradle was in a folder in the root project. So obviously I moved build.gradle into the root.
From there I noticed it actually built those classes. Still couldn't get my plugin to actually show up as a build step even though it showed up under installed (same old problem). I took another build.gradle from a different plugin and edited for my own use. It works, however I have no idea why.
*I also had to add a missing dependency I was having, now that it was actually building my project.
new build.gradle:
buildscript {
repositories {
// The plugin is currently only available via the Jenkins
// Maven repository, but has dependencies in Maven Central.
mavenCentral()
maven {
url 'http://repo.jenkins-ci.org/releases/'
}
}
dependencies {
classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.14.1'
}
}
apply plugin: 'java'
apply plugin: 'org.jenkins-ci.jpi'
repositories {
mavenCentral()
maven {
url "http://repo.jenkins-ci.org/releases/"
}
}
group = 'workday'
version = '0.1.0-SNAPSHOT'
description = 'Test AS A Service Plugin'
jenkinsPlugin {
// version of Jenkins core this plugin depends on, must be 1.420 or later
coreVersion = '1.654'
// ID of the plugin, defaults to the project name without trailing '-plugin'
shortName = 'jetson'
// human-readable name of plugin
displayName = 'Jetson Test Plugin'
// use the plugin class loader before the core class loader, defaults to false
pluginFirstClassLoader = true
// optional list of package prefixes that your plugin doesn't want to see from core
maskClasses = 'groovy.grape org.apache.commons.codec'
// optional version number from which this plugin release is configuration-compatible
compatibleSinceVersion = '1.1.0'
// enable injection of additional tests for checking the syntax of Jelly and other things
disabledTestInjection = false
// the output directory for the localizer task relative to the project root, defaults to the value shown
localizerOutputDir = "${project.buildDir}/generated-src/localizer"
// plugin file extension, either 'jpi' or 'hpi', defaults to 'hpi'
fileExtension = 'hpi'
}
dependencies {
compile group: 'org.jenkins-ci.main', name: 'ui-samples-plugin', version: '1.424.2'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.14'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
I suspect it actually has to do with the new buildscripts blocks for some reason
You probably need to set your group to
group = 'org.jenkins-ci.plugins'
and you can delete the
apply plugin 'java'
as this is done internally (I think)
I'm not sure you need to include the ui-samples-plugin either but if you do it needs to be something like
dependencies {
jenkinsPlugins( group: 'org.jenkins-ci.main',
name: 'ui-samples-plugin',
version: '1.424.2',
ext: 'jar')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
(untested)
Try its wiki page for more info

Implement rpm task in gradle

I am trying to migrate from ant to gradle to build my rpm-package. I found one plugin gradle-ospackage-plugin, but I am not able to understand how to use any of commands which I have in ant task: specFile, topDir, command, cleanbuildDir, failonError
Is it not possible to have them in gradle?
Update: Basically I am trying to replicate the following in gradle
<target name="myrpm">
<rpm specFile = "topdir"
topDir = "topdir" />
</target>
I was also looking into running ant tasks from gradle such as ant.echo(message: "hello).
But ant.rpm is not resolving.
A simple example that creates two RPMs. If you want a single RPM then use one task. Your build.gradle file should be like this.
plugins {
id 'java'
id "nebula.ospackage" version "3.2.0"
}
task one(type: Rpm) {
packageName = 'one-pack'
version = '1.0.0'
release = '1'
arch = I386
os = LINUX
from('./src/main/resources') {
into 'apps/lib/'
}
}
task two(type: Rpm) {
packageName = 'two-pack'
version = '2.2.0'
release = '2'
arch = I386
os = LINUX
}
apply plugin: 'nebula.ospackage'
group 'com.avishek'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.assertj:assertj-core:3.2.0'
}
Run the project with the following command:
./gradlew clean two one

Grails gradle: How to set System Property

May I know why I'm unable to set the System Property with the systemProperty method when using the grails-gradle-plugin?
My build.gradle as follows:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.grails:grails-gradle-plugin:2.0.0"
}
}
version "0.1"
group "example"
apply plugin: "grails"
repositories {
grails.central() //creates a maven repo for the Grails Central repository (Core libraries and plugins)
}
grails {
grailsVersion = '2.3.5'
groovyVersion = '2.1.9'
springLoadedVersion '1.1.3'
}
dependencies {
bootstrap "org.grails.plugins:tomcat:7.0.50" // No container is deployed by default, so add this
compile 'org.grails.plugins:resources:1.2' // Just an example of adding a Grails plugin
}
test {
println "I'm in the test"
//Could not find method systemProperty() for arguments [geb.env, sauce] on root project
systemProperty 'geb.env', 'sauce'//Fails
}
In the test task, I get the following error when i run $gradle grails-test:
Could not find method systemProperty() for arguments [geb.env, sauce] on root project..."
Is this a problem with the grails-gradle plugin since other plugins like "java" allows me to use setProperty? Thanks.
The error message is correct: there's no systemProperty() method.
As Joshua Moore comments, this should work:
test {
println "I'm in the test"
System.setProperty 'geb.env', 'sauce'
}

Gradle / Grails application

I have been trying to configure Gradle to manage a Grails project for a couple of hours with no success. The suggestions I've found here on Stack Overflow and elsewhere on the Internet didn't work for me.
Could you please give me up-to-date directions on how to configure a Gradle+Grails project? Ideally it should relate to the current versions of Grails (2.1.0) and Gradle (1.1).
BuildScript
import org.grails.gradle.plugin.GrailsTask
buildscript {
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
dependencies {
classpath "org.grails:grails-gradle-plugin:1.1.1-SNAPSHOT"
}
}
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
version = "1.0"
grailsVersion = "2.1.0"
apply plugin: "grails"
dependencies {
['dependencies', 'resources', 'core', 'hibernate', 'plugin-datasource', 'plugin-domain-class', 'plugin-tomcat', 'plugin-services'].each { plugin ->
compile "org.grails:grails-$plugin:2.1.0"
}
bootstrap "org.codehaus.groovy:groovy-all:1.8.6"
}
GRAILS_TASK_PREFIX = "grails-"
if (name.startsWith(GRAILS_TASK_PREFIX)) {
project.task(name, type: GrailsTask) {
command "${name - GRAILS_TASK_PREFIX}"
}
}
Initialize
Then you can do gradle init to initialize the project structure
Commands
Use gradle grails-[grails script] to execute your grails commands. For example: gradle grails-run-app is equivalent to grails run-app
Hope this helps!
Update
This seems to work for Grails 2.3.2:
buildscript {
repositories {
mavenCentral()
maven { url 'http://repo.grails.org/grails/repo' }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:2.0.0-SNAPSHOT"
}
}
repositories {
mavenCentral()
maven { url 'http://repo.grails.org/grails/repo' }
}
version = "1.0"
apply plugin: "grails"
apply plugin: 'idea'
grails {
grailsVersion = "2.3.2"
}
dependencies {
['dependencies', 'core', 'spring', 'web', 'plugin-datasource', 'plugin-domain-class', 'plugin-controllers', 'plugin-services'].each { plugin ->
compile "org.grails:grails-$plugin:2.3.2"
}
compile 'org.grails.plugins:tomcat:7.0.42'
compile 'org.grails.plugins:hibernate:3.6.10.3'
compile 'com.h2database:h2:1.3.173'
bootstrap "org.codehaus.groovy:groovy-all:2.1.9"
}
A note, throws Exception when generating Grails wrapper, but gradle init seems to initialize the project structure properly otherwise. And, gradle grails-run-app seems to work fine too.

Resources