How to specify relative path to checkstyle configuration in root project in gradle 2.2+

I have the following project structure:

project checkstyle checkstyle.xml subproject1 ... subproject2 ... build.gradle 

My configuration for checkstyle in build.gradle as follows:

 allprojects { apply plugin: 'checkstyle' checkstyle { configFile = 'checkstyle/checkstyle.xml' as File } } 

When I used gradle 2.1, the build worked fine. After upgrading to 2.2 or 2.2.1, an error occurs:

 :subproject1:checkstyleMain FAILED FAILURE: Build failed with an exception. What went wrong: Execution failed for task ':subproject1:checkstyleMain'. > Unable to create a Checker: unable to find /home/my/project/subproject1/checkstyle/checkstyle.xml 

Gradle now looks for "checkstyle / checkstyle.xml" in the subproject directory, which I did not expect.

I found that they mentioned this change in the release notes for 2.2 . But they did not say how to maintain previous behavior in resolving this path. How to do it?

I tried using project.file , but it does not work either.

+6
source share
3 answers

in your fragment

 allprojects { apply plugin: 'checkstyle' checkstyle { configFile = 'checkstyle/checkstyle.xml' as File } } 

you configure the configuration file for each project as relative from the subproject that you are currently setting up. you can fix it by replacing

 checkstyle { configFile = 'checkstyle/checkstyle.xml' as File } 

 checkstyle { configFile = rootProject.file('checkstyle/checkstyle.xml') } 
+12
source

Could you try replacing:

 configFile = 'checkstyle/checkstyle.xml' as File 

with:

 configFile = ('checkstyle/checkstyle.xml' as File).absolutePath as File 

and try if it works?

As mentioned here, configFile is replaced with config . I can’t try, but it’s possible:

 config = 'checkstyle/checkstyle.xml' as File 

will work?

+2
source

There is a much simpler way to do this. Try gradle-estilo-plugin . He will take care of creating all the configuration files (checkstyle.xml, import-control.xml, suppressions.xml and header.txt). All you have to do is use the plugin in the gradle build file. If you need any custom rules, you can add them directly to your gradle task.

Here is what you need to get started:

 buildscript { repositories { mavenCentral() } dependencies { classpath 'net.anshulverma.gradle:gradle-estilo-plugin:0.4.8' } } apply plugin: 'net.anshulverma.gradle.estilo' estilo { source 'sun' } 

After that, you can change the checks, add suppressions, edit the import control and add the header file directly from the estilo { } block. Detailed information on how to do this, as well as detailed information, including tips, can be found in README .

+1
source

Source: https://habr.com/ru/post/981785/


All Articles