1313import org .owasp .dependencycheck .gradle .extension .AnalyzerExtension ;
1414import org .owasp .dependencycheck .gradle .extension .DependencyCheckExtension ;
1515
16+ import java .io .File ;
1617import java .util .Arrays ;
17- import java .util .List ;
1818import java .util .LinkedHashMap ;
19+ import java .util .List ;
1920import java .util .Map ;
20- import java .util .stream .Collectors ;
2121
2222/**
2323 * {@link Plugin} for FormKiQ Gradle Conventions.
@@ -26,9 +26,24 @@ public class JavaBasePlugin implements Plugin<Project> {
2626
2727 /** Checkstyle Version. */
2828 private static final String CHECKSTYLE_TOOL_VERSION = "10.12.4" ;
29+ /** Maximum number of parent directories to search for shared config files. */
30+ private static final int MAX_CONFIG_PARENT_DEPTH = 3 ;
2931 /** Java version. */
3032 private static final int JAVA_VERSION = 17 ;
3133
34+ private static File findConfigFile (final Project project , final String fileName ) {
35+ File directory = project .getProjectDir ();
36+ for (int depth = 0 ; depth <= MAX_CONFIG_PARENT_DEPTH && directory != null ; depth ++) {
37+ File candidate = new File (directory , fileName );
38+ if (candidate .isFile ()) {
39+ return candidate ;
40+ }
41+ directory = directory .getParentFile ();
42+ }
43+
44+ return project .file (fileName );
45+ }
46+
3247 @ Override
3348 public void apply (Project root ) {
3449 root .getRepositories ().mavenCentral ();
@@ -59,11 +74,11 @@ public void apply(Project root) {
5974 p .getExtensions ().configure (SpotlessExtension .class , (SpotlessExtension s ) -> {
6075 s .java (j -> {
6176 j .eclipse ().sortMembersEnabled (true )
62- .configFile (p . getRootProject (). file ( "spotless.eclipseformat.xml" ));
77+ .configFile (findConfigFile ( p , "spotless.eclipseformat.xml" ));
6378 j .removeUnusedImports ();
64- j .removeWildcardImports ();
79+ j .forbidWildcardImports ();
6580
66- j .licenseHeaderFile (p . getRootProject (). file ( "LICENSE" ));
81+ j .licenseHeaderFile (findConfigFile ( p , "LICENSE" ));
6782 });
6883 s .groovyGradle (g -> {
6984 g .target ("*.gradle" );
@@ -86,8 +101,8 @@ public void apply(Project root) {
86101 });
87102
88103 // SpotBugs
89- p .getExtensions ().configure (SpotBugsExtension .class , sb -> sb . getExcludeFilter ()
90- . set ( p . file ( p . getRootDir () + "/ config/gradle/spotbugs-exclude.xml" )));
104+ p .getExtensions ().configure (SpotBugsExtension .class ,
105+ sb -> sb . getExcludeFilter (). set ( findConfigFile ( p , " config/gradle/spotbugs-exclude.xml" )));
91106
92107 p .getTasks ().withType (com .github .spotbugs .snom .SpotBugsTask .class ).configureEach (t -> {
93108 if (t .getReports ().findByName ("html" ) == null ) {
@@ -103,9 +118,11 @@ public void apply(Project root) {
103118 // Checkstyle
104119 p .getExtensions ().configure (CheckstyleExtension .class , cs -> {
105120 cs .setToolVersion (CHECKSTYLE_TOOL_VERSION );
106- cs .setConfigFile (p . file ( "config/checkstyle/checkstyle.xml" ));
121+ cs .setConfigFile (findConfigFile ( p , "config/checkstyle/checkstyle.xml" ));
107122 Map <String , Object > props = new LinkedHashMap <>();
108123 props .put ("project_loc" , p .getProjectDir ());
124+ props .put ("suppressions_file" ,
125+ findConfigFile (p , "config/checkstyle/mysuppressions.xml" ).getAbsolutePath ());
109126 cs .setConfigProperties (props );
110127 cs .setMaxWarnings (0 );
111128 cs .setMaxErrors (0 );
@@ -115,14 +132,12 @@ public void apply(Project root) {
115132 p .getExtensions ().configure (DependencyCheckExtension .class , dc -> {
116133 dc .setFormats (Arrays .asList ("HTML" , "JSON" , "SARIF" ));
117134 dc .setFailBuildOnCVSS (7.0f );
118- dc .setScanConfigurations (Arrays . asList ("runtimeClasspath" ));
135+ dc .setScanConfigurations (List . of ("runtimeClasspath" ));
119136 dc .setSkipTestGroups (true );
120137 Object skipProjects = p .findProperty ("dependencyCheckSkipProjects" );
121138 if (skipProjects != null ) {
122139 List <String > projectPaths = Arrays .stream (skipProjects .toString ().split ("," ))
123- .map (String ::trim )
124- .filter (s -> !s .isEmpty ())
125- .collect (Collectors .toList ());
140+ .map (String ::trim ).filter (s -> !s .isEmpty ()).toList ();
126141 dc .setSkipProjects (projectPaths );
127142 }
128143 dc .analyzers ((AnalyzerExtension analyzers ) -> {
@@ -157,12 +172,6 @@ public void apply(Project root) {
157172 t .setMinHeapSize ("1g" );
158173 t .setMaxHeapSize ("2g" );
159174 });
160-
161- p .afterEvaluate (prj -> {
162- if (!prj .file ("config/checkstyle/checkstyle.xml" ).exists ()) {
163- prj .getLogger ().warn ("Checkstyle config not found at config/checkstyle/checkstyle.xml" );
164- }
165- });
166175 });
167176
168177 }
0 commit comments