|
| 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