Skip to content

Commit 4435d7f

Browse files
committed
Converted to Java Plugin
1 parent 6e4a5f0 commit 4435d7f

3 files changed

Lines changed: 146 additions & 102 deletions

File tree

build.gradle

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
plugins {
2-
id 'groovy-gradle-plugin'
2+
id 'java-gradle-plugin'
3+
id "com.gradle.plugin-publish" version "2.0.0"
34
id 'com.diffplug.spotless' version '7.2.1'
45
}
56

7+
group 'com.formkiq.gradle'
8+
version '1.0.0'
9+
610
allprojects {
711
apply plugin: 'com.diffplug.spotless'
812
}
913

1014
repositories {
11-
gradlePluginPortal() // so that external plugins can be resolved in dependencies section
15+
gradlePluginPortal()
1216
}
1317

1418
dependencies {
@@ -35,3 +39,32 @@ spotless {
3539
}
3640

3741
spotlessCheck.dependsOn spotlessApply
42+
43+
gradlePlugin {
44+
website = 'https://github.com/formkiq/java-base-plugin'
45+
vcsUrl = 'https://github.com/formkiq/java-base-plugin'
46+
plugins {
47+
javaBase {
48+
id = "com.formkiq.gradle.java-base"
49+
displayName = "FormKiQ Java Base Conventions"
50+
description = "Applies Java 17 toolchain, Spotless, SpotBugs, Checkstyle, Versions plugin, and standard repos."
51+
implementationClass = "com.formkiq.gradle.JavaBasePlugin"
52+
tags = [
53+
"java",
54+
"conventions",
55+
"spotless",
56+
"spotbugs",
57+
"checkstyle"
58+
]
59+
}
60+
}
61+
}
62+
63+
publishing {
64+
publications {
65+
pluginMaven(MavenPublication) {
66+
}
67+
}
68+
}
69+
70+
check.dependsOn(tasks.publishToMavenLocal)

src/main/groovy/com.formkiq.gradle.java-base.gradle

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.formkiq.gradle;
2+
3+
import com.diffplug.gradle.spotless.SpotlessExtension;
4+
import com.github.spotbugs.snom.SpotBugsExtension;
5+
import org.gradle.api.Plugin;
6+
import org.gradle.api.Project;
7+
import org.gradle.api.plugins.JavaPluginExtension;
8+
import org.gradle.api.plugins.quality.CheckstyleExtension;
9+
import org.gradle.api.tasks.compile.JavaCompile;
10+
import org.gradle.api.tasks.testing.Test;
11+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
12+
13+
import java.util.LinkedHashMap;
14+
import java.util.Map;
15+
16+
/**
17+
* {@link Plugin} for FormKiQ Gradle Conventions.
18+
*/
19+
public class JavaBasePlugin implements Plugin<Project> {
20+
21+
/** Checkstyle Version. */
22+
private static final String CHECKSTYLE_TOOL_VERSION = "10.12.4";
23+
24+
@Override
25+
public void apply(Project root) {
26+
root.getRepositories().mavenCentral();
27+
28+
// configure root + all subprojects
29+
root.getAllprojects().forEach(p -> {
30+
// Apply plugins
31+
p.getPluginManager().apply("com.diffplug.spotless");
32+
p.getPluginManager().apply("java-library");
33+
p.getPluginManager().apply("checkstyle");
34+
p.getPluginManager().apply("com.github.spotbugs");
35+
p.getPluginManager().apply("com.github.ben-manes.versions");
36+
p.getPluginManager().apply("com.formkiq.gradle.graalvm-native-plugin");
37+
p.getPluginManager().apply("distribution");
38+
39+
// Repositories
40+
p.getRepositories().mavenLocal();
41+
p.getRepositories().mavenCentral();
42+
p.getRepositories().maven(repo -> repo.setUrl("https://central.sonatype.com/repository/maven-snapshots/"));
43+
44+
// Java toolchain 17
45+
JavaPluginExtension java = p.getExtensions().getByType(JavaPluginExtension.class);
46+
java.getToolchain().getLanguageVersion().set(JavaLanguageVersion.of(17));
47+
48+
// Spotless
49+
p.getExtensions().configure(SpotlessExtension.class, (SpotlessExtension s) -> {
50+
s.java(j -> {
51+
j.eclipse().sortMembersEnabled(true).configFile(p.getRootProject().file("spotless.eclipseformat.xml"));
52+
j.removeUnusedImports();
53+
j.removeWildcardImports();
54+
j.licenseHeaderFile(p.getRootProject().file("LICENSE"));
55+
});
56+
s.groovyGradle(g -> {
57+
g.target("*.gradle", "**/*.gradle");
58+
g.greclipse();
59+
g.leadingTabsToSpaces(2);
60+
g.trimTrailingWhitespace();
61+
g.endWithNewline();
62+
});
63+
});
64+
65+
// SpotBugs
66+
p.getExtensions().configure(SpotBugsExtension.class, sb ->
67+
sb.getExcludeFilter().set(p.file(p.getRootDir() + "/config/gradle/spotbugs-exclude.xml"))
68+
);
69+
70+
p.getTasks().withType(com.github.spotbugs.snom.SpotBugsTask.class).configureEach(t -> {
71+
if (t.getReports().findByName("html") == null) {
72+
t.getReports().create("html");
73+
}
74+
75+
t.getReports().getByName("html").getRequired().set(true);
76+
if (t.getReports().findByName("xml") != null) {
77+
t.getReports().getByName("xml").getRequired().set(false);
78+
}
79+
});
80+
81+
// Checkstyle
82+
p.getExtensions().configure(CheckstyleExtension.class, cs -> {
83+
cs.setToolVersion(CHECKSTYLE_TOOL_VERSION);
84+
cs.setConfigFile(p.file("config/checkstyle/checkstyle.xml"));
85+
Map<String, Object> props = new LinkedHashMap<>();
86+
props.put("project_loc", p.getProjectDir());
87+
cs.setConfigProperties(props);
88+
cs.setMaxWarnings(0);
89+
cs.setMaxErrors(0);
90+
});
91+
92+
// Compiler flags
93+
p.getTasks().withType(JavaCompile.class).configureEach(jc ->
94+
jc.getOptions().getCompilerArgs().add("-Xlint:deprecation")
95+
);
96+
97+
// Tests
98+
p.getTasks().withType(Test.class).configureEach(t -> {
99+
t.useJUnitPlatform();
100+
t.setFailFast(true);
101+
});
102+
103+
p.afterEvaluate(prj -> {
104+
if (!prj.file("config/checkstyle/checkstyle.xml").exists()) {
105+
prj.getLogger().warn("Checkstyle config not found at config/checkstyle/checkstyle.xml");
106+
}
107+
});
108+
});
109+
110+
}
111+
}

0 commit comments

Comments
 (0)