Use PMD's Copy/Paste Detector with Gradle - ant

I'd like to use Copy/Paste Detector in my Gradle build.
This is why I've decided to translate the following Ant task (which I've found here) into Gradle syntax:
<target name="cpd">
<taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" />
<cpd minimumTokenCount="100" outputFile="/home/tom/cpd.txt">
<fileset dir="/home/tom/tmp/ant">
<include name="**/*.java"/>
</fileset>
</cpd>
</target>
This is how the translation looks currently:
check << {
ant.taskdef(name: 'cpd', classname: 'net.sourceforge.pmd.cpd.CPDTask', classpath: configurations.pmd.asPath)
ant.cpd(minimumTokenCount: '100', outputFile: file('build/reports/pmd/copyPasteDetector.txt').toURI().toString()) {
fileset(dir: 'src'){
include(name: '**.java')
}
}
}
Unfortunately calling gradle check yields an net.sourceforge.pmd.cpd.ReportException, the stacktrace is here.
How can I scan my source code with the Copy/Paste Detector using Gradle 1.9?
Thanks!

You can also use my gradle-cpd-plugin. See https://github.com/aaschmid/gradle-cpd-plugin for further informationen. Applying the cpd plugin automatically adds it the cpd as dependency of check task.
Note: I am not very happy with the name cpd for extension (see toolVersion) and task, suggestions welcome ;-)
Currently, it is version 0.1 but I am on it to switch from using CPD's ant task internally to directly call it. This will include support of all parameters etc. Here is a usage example:
apply plugin: 'cpd'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'de.aaschmid.gradle.plugins:gradle-cpd-plugin:0.1'
}
}
// optional - default is 5.1.0
cpd {
toolVersion = '5.0.5'
}
tasks.cpd {
reports {
text.enabled = true
xml.enabled = false
}
source = files('src/main/java')
}

Guys from gradle forums suggest that you use CPD in gradle like that:
task cpd(dependsOn: ':pmdSetup') {
// Combine all source sets
allSource = files {
allprojects.findAll { proj ->
proj.hasProperty("sourceSets")
}.collect { proj ->
proj.sourceSets.collect { ss ->
ss.java
}
}
}
// Declare this task's inputs and outputs.
inputs.files allSource
outDir = file("$buildDirName/cpd")
outputs.dir outDir
// outputs.files file("$outDir.path/cpd.xml")
doLast {
outDir.mkdirs()
// Keep a reference to the gradle project for use inside the
// ant closure, where "project" refers to the ant project.
gproj = project
ant {
cpd(minimumTokenCount: '100', format: 'xml',
outputFile: outDir.path + '/cpd.xml') {
fileset(dir: projectDir.getPath()) {
// Convert the gradle sourceSet to an ant
// fileset.
allSource.each { file ->
include(name: gproj.relativePath(file))
}
}
}
}
}
}
and, of course, apply plugin: 'pmd' before.

The definition of my outputFile caused the problem.
I adapted this build.gradle and I'm now happy with the following solution:
check << {
File outDir = new File('build/reports/pmd/')
// Make sure the output dir exists to prevent a ReportException
outDir.mkdirs()
ant.taskdef(name: 'cpd', classname: 'net.sourceforge.pmd.cpd.CPDTask',
classpath: configurations.pmd.asPath)
ant.cpd(minimumTokenCount: '100', format: 'text',
outputFile: new File(outDir , 'cpd.txt')) {
fileset(dir: "src/main/java") {
include(name: '**/*.java')
}
}
}
Thanks Andrey Regentov and Perryn Fowler for their input.

Related

Adding jcenter() in rootlevel repositories breaks my build

I have a project with a Gradle buildscript and I added the bmuschko/gradle-docker-plugin to create and push image on new builds.
However I cant use the plugin if I don't specify jcenter() in root level repositories, but that breaks my build, so im kinda lost.
If I add the root level jcenter() it fails the build with:
Could not resolve all dependencies for configuration ':compileClasspath'.
Could not find com.sleepycat:je:7.0.6. Searched in the following locations:
https://jcenter.bintray.com/com/sleepycat/je/7.0.6/je-7.0.6.pom
https://jcenter.bintray.com/com/sleepycat/je/7.0.6/je-7.0.6.jar
https://repo1.maven.org/maven2/com/sleepycat/je/7.0.6/je-7.0.6.pom
https://repo1.maven.org/maven2/com/sleepycat/je/7.0.6/je-7.0.6.jar
https://nexus.company.com/content/groups/public/com/sleepycat/je/7.0.6/je-7.0.6.pom
https://nexus.company.com/content/groups/public/com/sleepycat/je/7.0.6/je-7.0.6.jar
Required by: project : > org.jpos:jpos:2.1.1
However if I don't add it i get this:
Could not resolve all dependencies for configuration ':dockerJava'.
Could not find com.aries:docker-java-shaded:3.1.0-rc-3. Searched in the following locations:
https://nexus.company.com/content/groups/public/com/aries/docker-java-shaded/3.1.0-rc-3/docker-java-shaded-3.1.0-rc-3.pom
https://nexus.company.com/content/groups/public/com/aries/docker-java-shaded/3.1.0-rc-3/docker-java-shaded-3.1.0-rc-3-cglib.jar
Required by: project :
Should I perhaps build Java shaded and include in our local Maven repo?
Here is the script:
buildscript {
repositories {
jcenter()
mavenCentral()
maven {
url = 'https://nexus.company.com/content/repositories/snapshots/'
credentials {
username 'na'
password 'na'
}
}
}
dependencies {
classpath group: 'com.company.gradle', name: 'cpmgradle', version: '0.1-SNAPSHOT'
classpath 'com.bmuschko:gradle-docker-plugin:3.4.4'
}
}
// ADDING THIS BREAKS MY BUILD BUT IS NECESSARY FOR DOCKER PLUGIN TO WORK
repositories {
jcenter()
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.company.gradle.java'
apply plugin: 'com.bmuschko.docker-remote-api'
projectinfo {
group = 'com.company.mesb'
artifact = 'MESB'
version = '1.29.DEV-SNAPSHOT'
description = 'MESB'
packaging = 'jar'
}
dependencies {
compile 'org.jpos:jpos:2.1.1'
compile 'org.jdom:jdom2:2.0.6'
compile 'org.json:json:20140107'
compile 'com.company.javasdk:JavaSDK:1.1.JDOM2-SNAPSHOT'
compile 'com.company.javasdk:Base64:1.0'
compile 'org.apache.wss4j:wss4j-ws-security-dom:2.0.3'
compile 'org.apache.santuario:xmlsec:2.0.3'
compile 'org.slf4j:slf4j-log4j12:1.7.10'
compile 'com.google.code.gson:gson:2.3.1'
compile 'org.bouncycastle:bcprov-jdk15on:1.54'
compile 'org.bouncycastle:bcpkix-jdk15on:1.54'
compile 'junit:junit:4.12'
compile 'org.jsoup:jsoup:1.9.2'
compile 'org.apache.commons:commons-lang3:3.4'
compile 'com.ibm:mq:5.3.07'
compile 'com.ibm:mq.pcf:5.3.07'
compile 'com.ibm:mq.jmqi:5.3.07'
compile 'com.ibm:mq.headers:5.3.07'
compile 'com.ibm:mq.commonservices:5.3.07'
compile 'com.ibm:connector:1.0.0'
runtime 'jaxen:jaxen:1.1.6'
runtime 'xalan:xalan:2.7.2'
runtime 'org.postgresql:postgresql:8.0-311.jdbc3'
runtime 'commons-logging:commons-logging:1.2'
}
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
options.fork = true
options.compilerArgs << '-XDignore.symbol.file'
options.forkOptions.executable = 'javac'
}
task libs(type: Copy) {
description = "Copies dependencies to the 'lib' directory of the deployment."
def libsDir = new File("$buildDir", 'libs')
libsDir.mkdirs()
def dockerDir = new File("$buildDir", 'docker')
dockerDir.mkdirs()
from project.configurations.runtime
into libsDir
}
task bundleZip(type: Zip) {
from projectDir
include 'xsl/**'
include 'conf/**'
include 'js/**'
include 'test/**'
include 'mesb.sh'
from(tasks.libs.outputs) {
include 'MESB*'
}
into project.projectinfo.version
}
tasks.bundleZip.dependsOn jar
tasks.bundleZip.dependsOn libs
def dockerUsername = project.properties['dockerUsername'] ?: ""
def dockerPassword = project.properties['dockerPassword'] ?: ""
docker {
registryCredentials {
username = dockerUsername
password = dockerPassword
}
}
task buildImage(type: com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) {
doFirst {
copy {
from "${buildDir}/libs/MESB-${projectinfo.version}.jar"
into "${buildDir}/docker"
rename { String fileName ->
fileName.replace("-${projectinfo.version}", "")
}
}
copy {
from "docker/jdk/Dockerfile"
into "${buildDir}/docker"
}
copy {
from "xsl"
into "${buildDir}/docker/xsl"
}
copy {
from "conf"
into "${buildDir}/docker/conf"
}
copy {
from "test"
into "${buildDir}/docker/test"
}
copy {
from "${buildDir}/libs"
into "${buildDir}/docker/libs"
}
}
inputDir = project.file("${buildDir}/docker")
tag = "company/mesb:${projectinfo.version}"
}
tasks.buildImage.dependsOn build
task pushImage(type: com.bmuschko.gradle.docker.tasks.image.DockerPushImage) {
imageName = "company/mesb"
tag = "${projectinfo.version}"
}
tasks.pushImage.dependsOn buildImage
artifacts {
archives bundleZip
}
/* Remove the default JAR file from the uploaded artifacts */
configurations.archives.artifacts.with { archives ->
def jarArtifact
archives.each {
if (it.file =~ 'jar') {
jarArtifact = it
}
}
remove(jarArtifact)
}
jar {
dependsOn configurations.runtime
manifest {
attributes 'Main-Class': 'main.MESB'
}
}
tasks.build.dependsOn libs
sourceSets {
main {
java {
srcDir 'src'
}
}
}
Apparently moving root repository definitions benieth the apply plugins section solved this.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.company.gradle.java'
apply plugin: 'com.bmuschko.docker-remote-api'
repositories {
jcenter()
mavenCentral()
}
Don't know whether you have solved your issue but either way...
I encountered a similar issue regarding:
Could not resolve all dependencies for configuration ':dockerJava'.
Could not find com.aries:docker-java-shaded:3.1.0-rc-3.
We solved it by adding the following
repositories {
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}

Gradle Jacoco and JUnit5

We just ported our unit tests to JUnit5. Realizing that this is still rather early adoption with little hints on google.
The most challenging was to get jacoco code coverage for the Junit5 tests which we use on jenkins. Since this took me almost a day to figure out, I thought I share. Nevertheless, if you know of a better solution I would be interested to know!
buildscript {
dependencies {
// dependency needed to run junit 5 tests
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
}
}
// include the jacoco plugin
plugins {
id 'jacoco'
}
dependencies {
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
runtime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2"
runtime "org.junit.vintage:junit-vintage-engine:4.12.0-M2"
}
apply plugin: 'org.junit.platform.gradle.plugin'
Then the problem seems to be that junitPlatformTest as defined in the org.junit.platform.gradle.plugin is defined too
late in the gradle lifecycle phase and hence is unknown when the script is parsed.
The following hack is needed in order to still be able to define a jacoco task which observes the junitPlatformTest task.
tasks.whenTaskAdded { task ->
if (task.name.equals('junitPlatformTest')) {
System.out.println("ADDING TASK " + task.getName() + " to the project!")
// configure jacoco to analyze the junitPlatformTest task
jacoco {
// this tool version is compatible with
toolVersion = "0.7.6.201602180812"
applyTo task
}
// create junit platform jacoco task
project.task(type: JacocoReport, "junitPlatformJacocoReport",
{
sourceDirectories = files("./src/main")
classDirectories = files("$buildDir/classes/main")
executionData task
})
}
}
Finally it is necessary to configure the junitPlatform plugin. The following code allows command line configuration of which junit 5 tags shall be run:
You can run all tests with 'unit' tag by running:
gradle clean junitPlatformTest -PincludeTags=unit
You can run all tests which are missing both unit and integ tag using
gradle clean junitPlatformTest -PexcludeTags=unit,integ
If no tags are provided all tests will be run (default).
junitPlatform {
engines {
include 'junit-jupiter'
include 'junit-vintage'
}
reportsDir = file("$buildDir/test-results")
tags {
if (project.hasProperty('includeTags')) {
for (String t : includeTags.split(',')) {
include t
}
}
if (project.hasProperty('excludeTags')) {
for (String t : excludeTags.split(',')) {
exclude t
}
}
}
enableStandardTestTask false
}
Thank you, so the hack now looks like this:
project.afterEvaluate {
def junitPlatformTestTask = project.tasks.getByName('junitPlatformTest')
// configure jacoco to analyze the junitPlatformTest task
jacoco {
// this tool version is compatible with
toolVersion = "0.7.6.201602180812"
applyTo junitPlatformTestTask
}
// create junit platform jacoco task
project.task(type: JacocoReport, "junitPlatformJacocoReport",
{
sourceDirectories = files("./src/main")
classDirectories = files("$buildDir/classes/main")
executionData junitPlatformTestTask
})
}
Can be also resolved with direct agent injection:
subprojects {
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.9"
}
configurations {
testAgent {
transitive = false
}
}
dependencies {
testAgent("org.jacoco:org.jacoco.agent:0.7.9:runtime")
}
tasks.withType(JavaExec) {
if (it.name == 'junitPlatformTest') {
doFirst {
jvmArgs "-javaagent:${configurations.testAgent.singleFile}=destfile=${project.buildDir.name}/jacoco/test.exec"
}
}
}
}
then report will be available with jacocoTestReport task
To get a reference to the junitPlatformTest task, another option is to implement an afterEvaluate block in the project like this:
afterEvaluate {
def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
// do something with the junitPlatformTestTask
}
See my comments on GitHub for JUnit 5 for further examples.
You just need to add #RunWith(JUnitPlatform.class) to your package
#RunWith(JUnitPlatform.class)
public class ClassTest {
}

Jenkins Job DSL: how to build particular node in Configure block?

The particular thing I want is to prevent some steps from execution in Flexible Publish section. I use a condition of strings (not) matching and I don't want something to be executed after check fails.
Being configured manually, expected step looks like this:
Expected postbuild step
As I have not found an appropriate method in Jenkins Jobs DSL API I've tried to reproduce it using the Configure block. The reference says that I can use 'project' for root element of the job and 'node' for particular node to append a child to them. It says also that new nodes won't be created again if pointed nodes exist. So here's my config:
job("flexible_condition") {
publishers {
flexiblePublish {
configure { node ->
node / publishers /
'org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher' << 'runner'(class:
'org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun')
}
condition {
not { stringsMatch('string_placeholder', '', false) }
}
publisher {
debianPackage('common') {
commitMessage('new feature')
}
}
publisher {
git {
pushOnlyIfSuccess(true)
branch('origin', 'master')
}
}
}
}
}
In spite of the reference desirable xml aren't generated in my jenkins neither in the Playground. I've got nodes duplicated instead and it seems that 'node' is interpreted as 'project' and is always put to the root.
<!-- 1. flexible_condition -->
<project>
<actions></actions>
<description></description>
<keepDependencies>false</keepDependencies>
<properties></properties>
<scm class='hudson.scm.NullSCM'></scm>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers class='vector'></triggers>
<concurrentBuild>false</concurrentBuild>
<builders></builders>
<publishers>
<org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
<runner class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun'></runner>
</org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
<org.jenkins__ci.plugins.flexible__publish.FlexiblePublisher>
<publishers>
<org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
<condition class='org.jenkins_ci.plugins.run_condition.logic.Not'>
<condition class='org.jenkins_ci.plugins.run_condition.core.StringsMatchCondition'>
<arg1>string_placeholder</arg1>
<arg2></arg2>
<ignoreCase>false</ignoreCase>
</condition>
</condition>
<publisherList>
<ru.yandex.jenkins.plugins.debuilder.DebianPackagePublisher>
<repoId>common</repoId>
<commitMessage>new feature</commitMessage>
<commitChanges>true</commitChanges>
</ru.yandex.jenkins.plugins.debuilder.DebianPackagePublisher>
<hudson.plugins.git.GitPublisher>
<configVersion>2</configVersion>
<pushMerge>false</pushMerge>
<pushOnlyIfSuccess>true</pushOnlyIfSuccess>
<forcePush>false</forcePush>
<tagsToPush></tagsToPush>
<branchesToPush>
<hudson.plugins.git.GitPublisher_-BranchToPush>
<targetRepoName>origin</targetRepoName>
<branchName>master</branchName>
</hudson.plugins.git.GitPublisher_-BranchToPush>
</branchesToPush>
</hudson.plugins.git.GitPublisher>
</publisherList>
<runner class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$Fail'></runner>
</org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
</publishers>
</org.jenkins__ci.plugins.flexible__publish.FlexiblePublisher>
</publishers>
<buildWrappers></buildWrappers>
</project>
I've been checking this example in the Playground via Job DSL version 1.40 but 1.39 in my jenkins gives the same result.
What am doing wrong?
Thank you.
UPD I've put the configure block below as SevenEleven suggested and it almost helped. The runner node is on the right place now but is still duplicated.
<publishers>
<org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
<condition .../>
<publisherList>
...
</publisherList>
<runner class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$Fail'></runner>
<runner class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun'></runner>
</org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
</publishers>
UPD 2 Although there are two different lines in the xml, the newer runner replaces the default one in generated job. So I've got the expected result. Thanks.
UPD 3. Yay! Found out that now one can simply use JobDSL syntax and it works.
publishers {
flexiblePublish {
conditionalAction {
condition {
not { stringsMatch('string_placeholder', '', false) }
}
publishers {
debianPackage('common') {
commitMessage('Automatic Commit')
}
git {
pushOnlyIfSuccess(true)
branch('origin', '$GIT_BRANCH')
}
}
runner('DontRun')
}
}
}
To edit the configuration, you have to place the configure-block below, not inside the publishers configuration:
job("flexible_condition") {
publishers {
flexiblePublish {
//...
}
}
configure {
it / publishers / 'org.jenkins__ci.plugins.flexible__publish.FlexiblePublisher' / publishers / 'org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher' / 'runner'(class:'org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun') {
}
}
}

How to compile multiple xsd files in Gradle JAXB/XJC?

I'm fairly new to Gradle and I am facing some issues trying to compile multiple schema files using Gradle Ant XJC.
Using the below code, I'm able to compile 1 schema successfully. However, I'm not sure how to do the same for multiple schema files.
Any advice please?
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task : XJC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
final def packageName = "org.test.webservice.domain"
final def schemaFile = "misc/resources/schemas/employee-v3/wadl/employee.xsd"
configurations { provided }
project.ext.generatedSrcDir = file("$buildDir/generated-src")
dependencies {
provided 'com.sun.xml.bind:jaxb-impl:2.2.6'
provided 'com.sun.xml.bind:jaxb-xjc:2.2.6'
}
task jaxb {
println 'Starting JAXB XJC...'
ext {
packagePath = packageName.replaceAll("\\.", "/")
srcFile = file(schemaFile)
destDir = new File(project.ext.generatedSrcDir, packagePath)
}
inputs.file srcFile
outputs.dir destDir
project.ext.generatedSrcDir.mkdirs()
ant.taskdef(name: 'xjc',
classname: 'com.sun.tools.xjc.XJCTask',
classpath: configurations.provided.asPath)
doLast {
project.ext.generatedSrcDir.mkdirs()
ant.xjc(schema: srcFile, package: packageName,
destdir: project.ext.generatedSrcDir)
}
task generateSources() {}
sourceSets.main.java.srcDirs += project.ext.generatedSrcDir
generateSources.dependsOn jaxb
compileJava.dependsOn generateSources
eclipseClasspath.dependsOn generateSources
// ideaClasspath.dependsOn generateSources
}
I solved it myself. Here is the working solution.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task : XJC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
final def packageName = "org.test.webservice.domain"
final def schemaDir = "misc/resources/schemas/employee-sda-v3/wadl"
configurations { provided }
project.ext.generatedSrcDir = file("$buildDir/generated-src")
dependencies {
provided 'com.sun.xml.bind:jaxb-impl:2.2.6'
provided 'com.sun.xml.bind:jaxb-xjc:2.2.6'
}
task jaxb {
println 'Starting JAXB XJC...'
ext {
packagePath = packageName.replaceAll("\\.", "/")
destDir = new File(project.ext.generatedSrcDir, packagePath)
}
outputs.dir destDir
project.ext.generatedSrcDir.mkdirs()
ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.provided.asPath)
doLast {
project.ext.generatedSrcDir.mkdirs()
ant.xjc(package: packageName, destdir: project.ext.generatedSrcDir){
schema(dir: ${schemaDir}, includes: "**/*.xsd")
}
}
task generateSources() {}
sourceSets.main.java.srcDirs += project.ext.generatedSrcDir
generateSources.dependsOn jaxb
compileJava.dependsOn generateSources
eclipseClasspath.dependsOn generateSources
// ideaClasspath.dependsOn generateSources
}
jacobono's gradle-jaxb-plugin compiles all schemas in the input directory.
I find this plugin more stable than Ant XJC. In my project, on some machines Ant XJC does not generate classes for no reason, although the results is successful. Maybe this is matter of configuration but IMHO even setup is easier with the plugin.
Here is one more working example. I put complete version of my build.gradle file.
You can setup directory and include filter on schema inside of xjc.
if you have 'nested "$" element is not supported' error like above, then below my example will help
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
task genJaxb {
ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
ext.classesDir = "${buildDir}/classes/jaxb"
ext.schemaDir = "${projectDir}/src/main/resources"
outputs.dir classesDir
doLast() {
project.ant {
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
classpath: configurations.jaxb.asPath
mkdir(dir: sourcesDir)
mkdir(dir: classesDir)
xjc(destdir: sourcesDir) {
schema(dir: schemaDir, includes: "**/*.xsd")
arg(value: "-wsdl")
produces(dir: sourcesDir, includes: "**/*.java")
}
javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
debugLevel: "lines,vars,source",
classpath: configurations.jaxb.asPath,
includeantruntime: "false") {
src(path: sourcesDir)
include(name: "**/*.java")
include(name: "*.java")
}
copy(todir: classesDir) {
fileset(dir: sourcesDir, erroronmissingdir: false) {
exclude(name: "**/*.java")
}
}
}
}
}
jar {
baseName = 'myProject'
version = '0.0.1'
from genJaxb.classesDir
}
repositories {
mavenLocal()
mavenCentral()
}
configurations {
jaxb
}
dependencies {
compile("com.google.code.gson:gson:2.3",
"commons-lang:commons-lang:2.6",
"commons-collections:commons-collections:3.2.1",
"commons-codec:commons-codec:1.10",
"commons-io:commons-io:2.4",
"org.springframework.ws:spring-ws-core:2.2.1.RELEASE"
)
compile("wsdl4j:wsdl4j:1.6.1")
jaxb("com.sun.xml.bind:jaxb-xjc:2.2.4-1")
compile(files(genJaxb.classesDir).builtBy(genJaxb))
testCompile("junit:junit",
"org.powermock:powermock-api-mockito:1.6.2",
"org.powermock:powermock-module-junit4:1.6.2",
)
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
This could be the easiest solution without any external plugin.
Does it have any other impact?
task prepareXSD2Java{
def xsdSourceDir = xsdPackageName.replace(".","/")
FileTree xsdFilesTree = fileTree(dir: "$projectResourceDir/$xsdSourceDir")
xsdFilesTree.include '*.xsd'
doLast{
xsdFilesTree.getFiles().each {
File xsdFile ->
def xsdPackageExt = xsdFile.name.replace("-","_")
xsdPackageExt = xsdPackageExt.take(xsdPackageExt.lastIndexOf("."))
String xsdFileName = xsdFile.getAbsolutePath();
exec {
executable = "xjc"
args = [ "-d", "$projectSourceDir", "-p",
"$xsdPackageName.$xsdPackageExt", "$xsdFileName" ]
}
}
}
}

Gradle Geb saucelabs plugin

I'm following the example from http://www.gebish.org/manual/0.9.2/sauce-labs.html#gradle_geb_saucelabs_plugin but am unable to get it working. My build.gradle script is as follows:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.grails:grails-gradle-plugin:2.0.0"
classpath 'org.gebish:geb-gradle:0.9.2'
}
}
version "0.1"
group "example"
apply plugin: "grails"
apply plugin: "geb-saucelabs"
repositories {
grails.central() //creates a maven repo for the Grails Central repository (Core libraries and plugins)
maven { url "http://repository-saucelabs.forge.cloudbees.com/release" }
}
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
sauceConnect "com.saucelabs:sauce-connect:3.0.28"
}
sauceLabs {
browsers { //5
firefox_linux_19 //Could not find property 'reporting' on root project 'gradleGrailsError'.
chrome_mac
internetExplorer_vista_9
}
task { //6
testClassesDir = test.testClassesDir
testSrcDirs = test.testSrcDirs
classpath = test.classpath
}
account { //7
username = System.getenv("SAUCE_ONDEMAND_USERNAME")
accessKey = System.getenv("SAUCE_ONDEMAND_ACCESS_KEY")
}
}
When I run $gradle test, I get the following error: Could not find property 'reporting' on root project... This error occurs on the line which specifies firefox_linux_19 as the browser. Can someone please advise how I can get the geb-saucelabs plugin working correctly? Thanks.
After a lot of trial and error, I got the following to work:
sauceLabs {
tasks.withType(Test) {
reports.junitXml.destination = reporting.file("test-results/$name")
reports.html.destination = reporting.file("test-reports/$name")
}
browsers { //5
firefox_linux_19
chrome_mac
internetExplorer_vista_9
}
account { //7
username = System.getenv("SAUCE_ONDEMAND_USERNAME")
accessKey = System.getenv("SAUCE_ONDEMAND_ACCESS_KEY")
}
}
The addition of the tasks.withType(Test) was the key, and I also removed the task closure which was listed in the sample code.

Resources