Skip to content

Commit

Permalink
Format new plugin code
Browse files Browse the repository at this point in the history
Closes gh-54
  • Loading branch information
sjohnr committed Nov 6, 2024
1 parent 65c0400 commit 908f52e
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
* @author Steve Riesenberg
*/
public class SpringProjectPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
// Apply default plugins
PluginManager pluginManager = project.getPluginManager();
if (project.getRootProject().equals(project)) {
pluginManager.apply(BasePlugin.class);
// pluginManager.apply(SpringNoHttpPlugin.class);
// pluginManager.apply(SpringNoHttpPlugin.class);
pluginManager.apply(SpringCheckProhibitedDependenciesLifecyclePlugin.class);
}
else {
Expand All @@ -45,4 +46,5 @@ public void apply(Project project) {
pluginManager.apply(SpringCheckClasspathForProhibitedDependenciesPlugin.class);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@
* @author Steve Riesenberg
*/
public class SpringJavaPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
// Apply default plugins
PluginManager pluginManager = project.getPluginManager();
if (project.file("src/main/java").exists()
|| project.file("src/test/java").exists()
if (project.file("src/main/java").exists() || project.file("src/test/java").exists()
|| project.file("src/integration-test/java").exists()) {
pluginManager.apply("org.gradle.java");
}
if (project.file("src/main/groovy").exists()
|| project.file("src/test/groovy").exists()
if (project.file("src/main/groovy").exists() || project.file("src/test/groovy").exists()
|| project.file("src/integration-test/groovy").exists()) {
pluginManager.apply("org.gradle.groovy");
}
if (project.file("src/main/kotlin").exists()
|| project.file("src/test/kotlin").exists()
if (project.file("src/main/kotlin").exists() || project.file("src/test/kotlin").exists()
|| project.file("src/integration-test/kotlin").exists()
|| project.getBuildFile().getName().endsWith(".kts")) {
pluginManager.apply("org.jetbrains.kotlin.jvm");
Expand Down Expand Up @@ -89,7 +87,8 @@ public void apply(Project project) {
// Configure jar task
project.getTasks().withType(Jar.class, (jar) -> jar.manifest((manifest) -> {
Map<String, String> attributes = new HashMap<>();
attributes.put("Created-By", String.format("%s (%s)", System.getProperty("java.version"), System.getProperty("java.specification.vendor")));
attributes.put("Created-By", String.format("%s (%s)", System.getProperty("java.version"),
System.getProperty("java.specification.vendor")));
attributes.put("Implementation-Title", project.getName());
attributes.put("Implementation-Version", project.getVersion().toString());
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
Expand All @@ -99,4 +98,5 @@ public void apply(Project project) {
// Configure JUnit 5
project.getTasks().withType(Test.class, Test::useJUnitPlatform);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@
* @author Steve Riesenberg
*/
public class SpringJavaCheckstylePlugin implements Plugin<Project> {

private static final String CHECKSTYLE_DIR = "etc/checkstyle";

private static final String SPRING_JAVAFORMAT_VERSION_PROPERTY = "springJavaformatVersion";

private static final String DEFAULT_SPRING_JAVAFORMAT_VERSION = "0.0.38";

private static final String NOHTTP_CHECKSTYLE_VERSION_PROPERTY = "nohttpCheckstyleVersion";

private static final String DEFAULT_NOHTTP_CHECKSTYLE_VERSION = "0.0.11";

private static final String CHECKSTYLE_TOOL_VERSION_PROPERTY = "checkstyleToolVersion";

private static final String DEFAULT_CHECKSTYLE_TOOL_VERSION = "8.34";

private static final String SPRING_JAVAFORMAT_EXCLUDE_PACKAGES_PROPERTY = "springJavaformatExcludePackages";

@Override
Expand All @@ -51,20 +59,27 @@ public void apply(Project project) {
if (checkstyleDir.exists() && checkstyleDir.isDirectory()) {
project.getPluginManager().apply(CheckstylePlugin.class);

// NOTE: See gradle.properties#springJavaformatVersion for actual version number
project.getDependencies().add("checkstyle", "io.spring.javaformat:spring-javaformat-checkstyle:" + getSpringJavaformatVersion(project));
// NOTE: See gradle.properties#nohttpCheckstyleVersion for actual version number
project.getDependencies().add("checkstyle", "io.spring.nohttp:nohttp-checkstyle:" + getNohttpCheckstyleVersion(project));
// NOTE: See gradle.properties#springJavaformatVersion for actual version
// number
project.getDependencies()
.add("checkstyle",
"io.spring.javaformat:spring-javaformat-checkstyle:" + getSpringJavaformatVersion(project));
// NOTE: See gradle.properties#nohttpCheckstyleVersion for actual version
// number
project.getDependencies()
.add("checkstyle", "io.spring.nohttp:nohttp-checkstyle:" + getNohttpCheckstyleVersion(project));

CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
checkstyle.getConfigDirectory().set(checkstyleDir);
// NOTE: See gradle.properties#checkstyleToolVersion for actual version number
// NOTE: See gradle.properties#checkstyleToolVersion for actual version
// number
checkstyle.setToolVersion(getCheckstyleToolVersion(project));
}

// Configure checkFormat task
project.getTasks().withType(CheckFormat.class, (checkFormat) -> {
// NOTE: See gradle.properties#springJavaformatExcludePackages for excluded packages
// NOTE: See gradle.properties#springJavaformatExcludePackages for
// excluded packages
String[] springJavaformatExcludePackages = getSpringJavaformatExcludePackages(project);
if (springJavaformatExcludePackages != null) {
checkFormat.exclude(springJavaformatExcludePackages);
Expand All @@ -76,30 +91,35 @@ public void apply(Project project) {
private static String getSpringJavaformatVersion(Project project) {
String springJavaformatVersion = DEFAULT_SPRING_JAVAFORMAT_VERSION;
if (project.hasProperty(SPRING_JAVAFORMAT_VERSION_PROPERTY)) {
springJavaformatVersion = Objects.requireNonNull(project.findProperty(SPRING_JAVAFORMAT_VERSION_PROPERTY)).toString();
springJavaformatVersion = Objects.requireNonNull(project.findProperty(SPRING_JAVAFORMAT_VERSION_PROPERTY))
.toString();
}
return springJavaformatVersion;
}

private static String getNohttpCheckstyleVersion(Project project) {
String nohttpCheckstyleVersion = DEFAULT_NOHTTP_CHECKSTYLE_VERSION;
if (project.hasProperty(NOHTTP_CHECKSTYLE_VERSION_PROPERTY)) {
nohttpCheckstyleVersion = Objects.requireNonNull(project.findProperty(NOHTTP_CHECKSTYLE_VERSION_PROPERTY)).toString();
nohttpCheckstyleVersion = Objects.requireNonNull(project.findProperty(NOHTTP_CHECKSTYLE_VERSION_PROPERTY))
.toString();
}
return nohttpCheckstyleVersion;
}

private static String getCheckstyleToolVersion(Project project) {
String checkstyleToolVersion = DEFAULT_CHECKSTYLE_TOOL_VERSION;
if (project.hasProperty(CHECKSTYLE_TOOL_VERSION_PROPERTY)) {
checkstyleToolVersion = Objects.requireNonNull(project.findProperty(CHECKSTYLE_TOOL_VERSION_PROPERTY)).toString();
checkstyleToolVersion = Objects.requireNonNull(project.findProperty(CHECKSTYLE_TOOL_VERSION_PROPERTY))
.toString();
}
return checkstyleToolVersion;
}

@Nullable
private String[] getSpringJavaformatExcludePackages(Project project) {
String springJavaformatExcludePackages = (String) project.findProperty(SPRING_JAVAFORMAT_EXCLUDE_PACKAGES_PROPERTY);
String springJavaformatExcludePackages = (String) project
.findProperty(SPRING_JAVAFORMAT_EXCLUDE_PACKAGES_PROPERTY);
return (springJavaformatExcludePackages != null) ? springJavaformatExcludePackages.split(" ") : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ public FileCollection getClasspath() {
@TaskAction
public void checkForProhibitedDependencies() throws IOException {
ResolvedConfiguration resolvedConfiguration = this.classpath.getResolvedConfiguration();
TreeSet<String> prohibited = resolvedConfiguration.getResolvedArtifacts().stream()
.map((artifact) -> artifact.getModuleVersion().getId()).filter(this::prohibited)
.map((id) -> id.getGroup() + ":" + id.getName()).collect(Collectors.toCollection(TreeSet::new));
TreeSet<String> prohibited = resolvedConfiguration.getResolvedArtifacts()
.stream()
.map((artifact) -> artifact.getModuleVersion().getId())
.filter(this::prohibited)
.map((id) -> id.getGroup() + ":" + id.getName())
.collect(Collectors.toCollection(TreeSet::new));
if (!prohibited.isEmpty()) {
StringBuilder message = new StringBuilder(String.format("Found prohibited dependencies in '%s':%n", this.classpath.getName()));
StringBuilder message = new StringBuilder(
String.format("Found prohibited dependencies in '%s':%n", this.classpath.getName()));
for (String dependency : prohibited) {
message.append(String.format(" %s%n", dependency));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class SpringCheckClasspathForProhibitedDependenciesPlugin implements Plug
@Override
public void apply(Project project) {
project.getPlugins().apply(SpringCheckProhibitedDependenciesLifecyclePlugin.class);
project.getPlugins().withType(JavaBasePlugin.class, (javaBasePlugin) ->
configureProhibitedDependencyChecks(project));
project.getPlugins()
.withType(JavaBasePlugin.class, (javaBasePlugin) -> configureProhibitedDependencyChecks(project));
}

private void configureProhibitedDependencyChecks(Project project) {
Expand All @@ -54,16 +54,19 @@ private void createProhibitedDependenciesChecks(Project project, String... confi

private void createProhibitedDependenciesCheck(Configuration classpath, Project project) {
String taskName = "check" + capitalize(classpath.getName() + "ForProhibitedDependencies");
TaskProvider<CheckClasspathForProhibitedDependencies> checkClasspathTask = project.getTasks().register(taskName,
CheckClasspathForProhibitedDependencies.class, (checkClasspath) -> {
checkClasspath.setGroup(LifecycleBasePlugin.CHECK_TASK_NAME);
checkClasspath.setDescription("Checks " + classpath.getName() + " for prohibited dependencies");
checkClasspath.setClasspath(classpath);
});
project.getTasks().named(SpringCheckProhibitedDependenciesLifecyclePlugin.CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME, (checkProhibitedTask) -> checkProhibitedTask.dependsOn(checkClasspathTask));
TaskProvider<CheckClasspathForProhibitedDependencies> checkClasspathTask = project.getTasks()
.register(taskName, CheckClasspathForProhibitedDependencies.class, (checkClasspath) -> {
checkClasspath.setGroup(LifecycleBasePlugin.CHECK_TASK_NAME);
checkClasspath.setDescription("Checks " + classpath.getName() + " for prohibited dependencies");
checkClasspath.setClasspath(classpath);
});
project.getTasks()
.named(SpringCheckProhibitedDependenciesLifecyclePlugin.CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME,
(checkProhibitedTask) -> checkProhibitedTask.dependsOn(checkClasspathTask));
}

private static String capitalize(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@
* @author Rob Winch
*/
public class SpringCheckProhibitedDependenciesLifecyclePlugin implements Plugin<Project> {

public static final String CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME = "checkForProhibitedDependencies";

@Override
public void apply(Project project) {
TaskProvider<Task> checkProhibitedDependencies = project.getTasks().register(SpringCheckProhibitedDependenciesLifecyclePlugin.CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME, (task) -> {
task.setGroup(JavaBasePlugin.VERIFICATION_GROUP);
task.setDescription("Checks both the compile/runtime classpath of every SourceSet for prohibited dependencies");
});
TaskProvider<Task> checkProhibitedDependencies = project.getTasks()
.register(SpringCheckProhibitedDependenciesLifecyclePlugin.CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME,
(task) -> {
task.setGroup(JavaBasePlugin.VERIFICATION_GROUP);
task.setDescription(
"Checks both the compile/runtime classpath of every SourceSet for prohibited dependencies");
});
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME, (checkTask) -> {
checkTask.dependsOn(checkProhibitedDependencies);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
* @author Steve Riesenberg
*/
public class SpringJavadocOptionsPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getTasks().withType(Javadoc.class, (javadoc) -> {
StandardJavadocDocletOptions options = (StandardJavadocDocletOptions) javadoc.getOptions();
options.addStringOption("Xdoclint:none", "-quiet");
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
* @author Steve Riesenberg
*/
public class SpringJacocoPlugin implements Plugin<Project> {

private static final String JACOCO_TOOL_VERSION_PROPERTY = "jacocoToolVersion";

private static final String DEFAULT_JACOCO_TOOL_VERSION = "0.8.7";

@Override
Expand All @@ -53,4 +55,5 @@ private static String getJacocoToolVersion(Project project) {
}
return jacocoToolVersion;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;

/**
* Creates a Management configuration that is appropriate for adding a platform to that is not exposed externally. If
* the JavaPlugin is applied, the compileClasspath, runtimeClasspath, testCompileClasspath, and testRuntimeClasspath
* will extend from it.
* Creates a Management configuration that is appropriate for adding a platform to that is
* not exposed externally. If the JavaPlugin is applied, the compileClasspath,
* runtimeClasspath, testCompileClasspath, and testRuntimeClasspath will extend from it.
*
* @author Rob Winch
* @author Steve Riesenberg
*/
public class SpringManagementConfigurationPlugin implements Plugin<Project> {

public static final String MANAGEMENT_CONFIGURATION_NAME = "management";

@Override
Expand All @@ -58,10 +60,11 @@ public void apply(Project project) {
});
plugins.withType(MavenPublishPlugin.class, (mavenPublish) -> {
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);
publishing.getPublications().withType(MavenPublication.class, (mavenPublication) ->
mavenPublication.versionMapping((versions) ->
versions.allVariants(VariantVersionMappingStrategy::fromResolutionResult)));
publishing.getPublications()
.withType(MavenPublication.class, (mavenPublication) -> mavenPublication.versionMapping(
(versions) -> versions.allVariants(VariantVersionMappingStrategy::fromResolutionResult)));
});
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Steve Riesenberg
*/
public class SpringNoHttpPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
// Apply nohttp plugin
Expand All @@ -38,4 +39,5 @@ public void apply(Project project) {
nohttp.setAllowlistFile(allowlistFile);
nohttp.getSource().exclude("**/build/**");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Steve Riesenberg
*/
public class SpringPropDepsEclipsePlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
PluginManager pluginManager = project.getPluginManager();
Expand All @@ -44,4 +45,5 @@ public void apply(Project project) {
classpath.getPlusConfigurations().add(project.getConfigurations().getByName("optional"));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @link https://youtrack.jetbrains.com/issue/IDEA-117668
*/
public class SpringPropDepsIdeaPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
PluginManager pluginManager = project.getPluginManager();
Expand All @@ -47,4 +48,5 @@ public void apply(Project project) {
idea.getScopes().get("PROVIDED").get("plus").add(project.getConfigurations().getByName("optional"));
});
}

}
Loading

0 comments on commit 908f52e

Please sign in to comment.