77import io .github .alexeytereshchenko .guardian .task .DownloadCheckstyleFile ;
88import java .io .File ;
99import java .net .URL ;
10- import java .util .List ;
10+ import java .util .ArrayList ;
1111import java .util .Set ;
1212import net .ltgt .gradle .errorprone .CheckSeverity ;
1313import net .ltgt .gradle .errorprone .ErrorProneCompilerArgumentProvider ;
2121import org .gradle .api .plugins .quality .CheckstyleExtension ;
2222import org .gradle .api .tasks .Copy ;
2323import org .gradle .api .tasks .Delete ;
24+ import org .gradle .api .tasks .SourceSet ;
2425import org .gradle .api .tasks .SourceSetContainer ;
2526import org .gradle .api .tasks .compile .JavaCompile ;
2627import org .gradle .language .base .internal .plugins .CleanRule ;
@@ -126,29 +127,13 @@ private void configureValidateStyleTask(Project project) {
126127 }
127128
128129 private void configureCheckstyle (Project project , GuardianExtension guardianExtension ) {
129- GuardianCheckStyleExtension checkStyleExtension = guardianExtension .getCheckStyle ();
130- String fileUrl = checkStyleExtension .getFileUrl ();
131- String filePath = fileUrl == null ? getGuardianCheckStyleFilePath (project ) : getCustomCheckStyleFilePath (project );
132-
133- int errorThreshold = checkStyleExtension .getErrorThreshold ();
134- boolean showViolations = checkStyleExtension .isShowViolations ();
135- String version = checkStyleExtension .getVersion ();
130+ GuardianCheckStyleExtension guardianCheckStyleExtension = guardianExtension .getCheckStyle ();
136131
137132 CheckstyleExtension checkstyleExtension = project .getExtensions ()
138133 .findByType (CheckstyleExtension .class );
139134
140135 if (checkstyleExtension != null ) {
141- checkstyleExtension .setToolVersion (version );
142- checkstyleExtension .setMaxErrors (errorThreshold );
143- checkstyleExtension .setShowViolations (showViolations );
144- checkstyleExtension .setMaxWarnings (0 );
145- checkstyleExtension .setIgnoreFailures (false );
146- checkstyleExtension .setConfigFile (new File (filePath ));
147-
148- SourceSetContainer sourceSets = (SourceSetContainer ) project .getProperties ()
149- .get ("sourceSets" );
150- checkstyleExtension .setSourceSets (
151- List .of (sourceSets .getByName ("main" ), sourceSets .getByName ("test" )));
136+ configureCheckStyleExtension (project , checkstyleExtension , guardianCheckStyleExtension );
152137 }
153138
154139 // it's bug in a checkstyle plugin https://github.com/gradle/gradle/issues/27035#issuecomment-1814589243
@@ -165,6 +150,36 @@ private void configureCheckstyle(Project project, GuardianExtension guardianExte
165150 });
166151 }
167152
153+ private void configureCheckStyleExtension (
154+ Project project ,
155+ CheckstyleExtension checkstyleExtension ,
156+ GuardianCheckStyleExtension guardianCheckStyleExtension
157+ ) {
158+ String fileUrl = guardianCheckStyleExtension .getFileUrl ();
159+ String filePath = fileUrl == null ? getGuardianCheckStyleFilePath (project ) : getCustomCheckStyleFilePath (project );
160+
161+ int errorThreshold = guardianCheckStyleExtension .getErrorThreshold ();
162+ boolean showViolations = guardianCheckStyleExtension .isShowViolations ();
163+ String version = guardianCheckStyleExtension .getVersion ();
164+ boolean includeTest = guardianCheckStyleExtension .isIncludeTest ();
165+
166+ checkstyleExtension .setToolVersion (version );
167+ checkstyleExtension .setMaxErrors (errorThreshold );
168+ checkstyleExtension .setShowViolations (showViolations );
169+ checkstyleExtension .setMaxWarnings (0 );
170+ checkstyleExtension .setIgnoreFailures (false );
171+ checkstyleExtension .setConfigFile (new File (filePath ));
172+
173+ SourceSetContainer sourceSetContainer = (SourceSetContainer ) project .getProperties ().get ("sourceSets" );
174+ ArrayList <SourceSet > sourceSets = new ArrayList <>();
175+ sourceSets .add (sourceSetContainer .getByName ("main" ));
176+ if (includeTest ) {
177+ sourceSets .add (sourceSetContainer .getByName ("test" ));
178+ }
179+
180+ checkstyleExtension .setSourceSets (sourceSets );
181+ }
182+
168183 private String getGuardianCheckStyleFilePath (Project project ) {
169184 URL url = getClass ().getClassLoader ().getResource ("guardian-checkstyle.xml" );
170185 if (url != null ) {
0 commit comments