Hi,
I just migrated several projects from maven to gradle and it’s working fine for now So currently I am trying to improve some things and therefore I am now blocked by the following issue: I have a jar files which contains a checkstyle.xml file. I am using this strategy because we have several repos and I don’t want to copy the checkstyle config to each repo. This is working fine with the following code:
apply plugin: 'code-quality'
def checkstyleDir = 'checkstyle/config'
def checkstyleFile = 'checkStyle.xml'
configurations { checkstyle }
dependencies{
checkstyle group: 'de.xplosion', name: 'checkstyle', version: '1.0'
}
// copy checkstyle config from checkstyle.jar into checkstyle/config folder
task getCheckstyleConfig {
dependsOn configurations.checkstyle
configurations.checkstyle.files.each { details ->
zipTree(details).each{ file ->
if(!(new File(checkstyleDir + '/' + checkstyleFile).exists())){
if(file.name.contains(checkstyleFile)){
copy{
println file.toString() + " -> " + checkstyleDir
from file.toString()
into checkstyleDir
}
}
}
}
}
}
task aggregateCheckstyle(type: Checkstyle) {
dependsOn getCheckstyleConfig
source subprojects.collect { project ->
project.sourceSets.main.allJava
project.sourceSets.test.allJava
}
classpath = files(subprojects.collect { project ->
project.sourceSets.main.compileClasspath
})
configFile = new File(checkstyleDir + '/' + checkstyleFile)
println 'Running checkstyle with config ' + configFile.toString()
resultFile = new File(checkstyleDir + '/checkstyleResult.xml')
ignoreFailures = true
}
What I am trying now is to create a plugin-wrapper around the code-quality plugin. I wnt to move the above code into a plugin to push the config file directly into the code quality plugin with the result that I only have to include
apply plugin: 'myOwnCheckstylePlugin'
So I moved the code into an own project:
package de.xplosion.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.quality.CodeQualityPlugin
import org.gradle.api.plugins.quality.JavaCodeQualityPluginConvention
import org.gradle.api.plugins.quality.Checkstyle
class CheckstylePlugin implements Plugin<Project> {
def void apply(Project project) {
project.plugins.apply(CodeQualityPlugin)
def map = [type: Checkstyle]
project.task(map, 'myOwnCheckStylePlugin') << {
source project.subprojects.collect {
project.sourceSets.main.allJava
project.sourceSets.test.allJava
}
classpath = files(project.subprojects.collect {
project.sourceSets.main.compileClasspath
})
configFile = new File('checkstyle.xml')
resultFile = new File(project.buildDir.toString() + '/checkstyleResult.xml')
ignoreFailures = true
}
}
}
This compiles and I can install the jar file into the repo. I can also use
apply plugin: 'myOwnCheckstylePlugin'
into another project, but the config file won’t be loaded. The code-quality plugin is triggered but the redefinition of the config file location is not used.
Anybody an idea what I have to do? Another question would be: how do I test it correctly? How can I simulate the activation of my plugin in a test class?
I tried
def project = org.gradle.testfixtures.ProjectBuilder.builder().build()
project.plugins.apply(JavaBasePlugin)
project.plugins.apply(CheckstylePlugin)
but I cannot see that the checkstyleMain task is called. How would I do that?
Thank you in advice!
Martin