One checkstyle config file for many projects


(Klaus Christiansen) #1


I have some projects that all applies the same plugins like; java, bugfinder and checksyle.
I’m creating a project (genericGradleStuffProject) that has a build file with all the generic plugin/settings that my projects are using.

I would like to have only one checkstyle config file for all projects, that exists in genericGradleStuffProject.

My problem is that gradle seams to look for the checkstyle config file relative to the project that executes the gradle script.

How do I get gradle to look in the right place for the checkstyle config file?

build.gradle snippet

apply plugin: 'checkstyle'

checkstyle {
	toolVersion = '6.17'
	showViolations = true
	configFile = new File('config/checkstyle/style.xml')

console output

What went wrong:
Execution failed for task ':checkstyleMain'.
> Unable to create a Checker: configLocation {C:\Users\klaus\workspace_hello_gradle\ImUsing\config\checkstyle\style.xml}, classpath {C:\Users\klaus\workspace_hello_gradle\ImUsing\build\classes\main;C:\Users\klaus\workspace_hello_gradle\

(Sterling Greene) #2

You should never use new File(String) in a Gradle script since relative paths will be relative to the current working directory of the daemon (which could be something other than your project directory).

I’m not quite sure I understand how you’re using genericGradleStuffProject with ImUsing?

Here’s another idea for sharing checkstyle configs between projects:

(Klaus Christiansen) #3

Thanks for the link. It is spot on. The only problem is that Maven and Ivy is not an options for me.

The idea with genericGradleStuffProject is to collect all the configuration that all the other projects always uses.
The build.gradle file in the ImUsing project then contains
apply from: "../genericGradleStuffProject/build.gradle".

(Schalk Cronjé) #4

Just a thought: Have you tried?

configFIle = file("${rootProject.projectDir}/path/to/style.xml")

Then it is always resovled relative to the root project.

(Klaus Christiansen) #5

I have the project layout like this:

If I execute gradle from ImUsing than $rootProject is ImUsing.

I have come up with a solution but I’m not sure that I’m happy about it.
In ImUsing I have create settings.gradle witch include GenericGradleStuffProject.
build.gradle in ImUsing contains this as first line:

apply from: project(':GenericGradleStuffProject').projectDir.path + '/build.gradle'

The build.gradle file in GenericGradleStuffProject has this:

   checkstyle {
       toolVersion = '6.17'
       config = project(':GenericGradleStuffProject').resources.text.fromFile("/config/checkstyle/checkstyle.xml")