Skip to content

Commit 10433df

Browse files
authored
Fixed deprecated methods being used, added search for config files in a project instead of hard coded directory (#3)
1 parent 010eb17 commit 10433df

2 files changed

Lines changed: 41 additions & 19 deletions

File tree

build.gradle

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,24 @@ plugins {
66
id "com.github.ben-manes.versions" version "0.53.0"
77
}
88

9+
def isNonStable = { String version ->
10+
def normalizedVersion = version.toUpperCase()
11+
def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { normalizedVersion.contains(it) }
12+
def stableVersion = version ==~ /^[0-9,.v-]+(-r)?$/
13+
return !stableKeyword && !stableVersion
14+
}
15+
916
group 'com.formkiq.gradle'
10-
version '1.0.9'
17+
version '1.0.10'
1118

1219
allprojects {
1320
apply plugin: 'com.diffplug.spotless'
21+
22+
tasks.matching { it.name == 'dependencyUpdates' }.configureEach {
23+
rejectVersionIf {
24+
isNonStable(it.candidate.version)
25+
}
26+
}
1427
}
1528

1629
repositories {

src/main/java/com/formkiq/gradle/JavaBasePlugin.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
import org.owasp.dependencycheck.gradle.extension.AnalyzerExtension;
1414
import org.owasp.dependencycheck.gradle.extension.DependencyCheckExtension;
1515

16+
import java.io.File;
1617
import java.util.Arrays;
17-
import java.util.List;
1818
import java.util.LinkedHashMap;
19+
import java.util.List;
1920
import 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

Comments
 (0)