Skip to content

Commit 65c0400

Browse files
committed
Initial version of spring-security-project-plugin
Issue gh-54
1 parent 11d2c2c commit 65c0400

16 files changed

+1028
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
plugins {
2+
id "io.spring.convention.spring-gradle-plugin"
3+
id "com.gradle.plugin-publish" version "1.2.1"
4+
}
5+
6+
gradlePlugin {
7+
website = "https://github.com/spring-io/spring-security-release-tools/tree/main/project-plugin"
8+
vcsUrl = "https://github.com/spring-io/spring-security-release-tools.git"
9+
plugins {
10+
springSecurityProjectPlugin {
11+
id = "io.spring.security.project"
12+
displayName = "Spring Security Project Plugin"
13+
description = "Spring Security Project Plugin is a Gradle plugin that applies common configuration conventions to Java, Kotlin or Groovy modules."
14+
tags.addAll("spring", "project", "java", "kotlin", "groovy")
15+
implementationClass = "io.spring.gradle.plugin.module.SpringProjectPlugin"
16+
}
17+
}
18+
}
19+
20+
artifactory {
21+
publish {
22+
defaults {
23+
publications("pluginMaven", "springSecurityProjectPluginPluginMarkerMaven")
24+
}
25+
}
26+
}
27+
28+
repositories {
29+
gradlePluginPortal()
30+
}
31+
32+
dependencies {
33+
management platform(project(":dependencies"))
34+
implementation "io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.42"
35+
implementation "io.spring.nohttp:nohttp-gradle:0.0.11"
36+
37+
testImplementation "org.junit.jupiter:junit-jupiter"
38+
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
39+
40+
testImplementation "org.assertj:assertj-core"
41+
testImplementation "org.mockito:mockito-core"
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.gradle.plugin.module;
18+
19+
import org.gradle.api.Plugin;
20+
import org.gradle.api.Project;
21+
import org.gradle.api.plugins.BasePlugin;
22+
import org.gradle.api.plugins.JavaLibraryPlugin;
23+
import org.gradle.api.plugins.PluginManager;
24+
25+
import org.springframework.gradle.SpringJavaPlugin;
26+
import org.springframework.gradle.classpath.SpringCheckClasspathForProhibitedDependenciesPlugin;
27+
import org.springframework.gradle.classpath.SpringCheckProhibitedDependenciesLifecyclePlugin;
28+
29+
/**
30+
* @author Steve Riesenberg
31+
*/
32+
public class SpringProjectPlugin implements Plugin<Project> {
33+
@Override
34+
public void apply(Project project) {
35+
// Apply default plugins
36+
PluginManager pluginManager = project.getPluginManager();
37+
if (project.getRootProject().equals(project)) {
38+
pluginManager.apply(BasePlugin.class);
39+
// pluginManager.apply(SpringNoHttpPlugin.class);
40+
pluginManager.apply(SpringCheckProhibitedDependenciesLifecyclePlugin.class);
41+
}
42+
else {
43+
pluginManager.apply(JavaLibraryPlugin.class);
44+
pluginManager.apply(SpringJavaPlugin.class);
45+
pluginManager.apply(SpringCheckClasspathForProhibitedDependenciesPlugin.class);
46+
}
47+
}
48+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.gradle;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
23+
import org.gradle.api.JavaVersion;
24+
import org.gradle.api.Plugin;
25+
import org.gradle.api.Project;
26+
import org.gradle.api.plugins.JavaPluginExtension;
27+
import org.gradle.api.plugins.PluginManager;
28+
import org.gradle.api.tasks.compile.CompileOptions;
29+
import org.gradle.api.tasks.compile.JavaCompile;
30+
import org.gradle.api.tasks.testing.Test;
31+
import org.gradle.jvm.tasks.Jar;
32+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
33+
34+
import org.springframework.gradle.checkstyle.SpringJavaCheckstylePlugin;
35+
import org.springframework.gradle.docs.SpringJavadocOptionsPlugin;
36+
import org.springframework.gradle.jacoco.SpringJacocoPlugin;
37+
import org.springframework.gradle.management.SpringManagementConfigurationPlugin;
38+
import org.springframework.gradle.propdeps.SpringPropDepsEclipsePlugin;
39+
import org.springframework.gradle.propdeps.SpringPropDepsIdeaPlugin;
40+
import org.springframework.gradle.properties.SpringCopyPropertiesPlugin;
41+
import org.springframework.gradle.repository.SpringRepositoryPlugin;
42+
43+
/**
44+
* @author Steve Riesenberg
45+
*/
46+
public class SpringJavaPlugin implements Plugin<Project> {
47+
@Override
48+
public void apply(Project project) {
49+
// Apply default plugins
50+
PluginManager pluginManager = project.getPluginManager();
51+
if (project.file("src/main/java").exists()
52+
|| project.file("src/test/java").exists()
53+
|| project.file("src/integration-test/java").exists()) {
54+
pluginManager.apply("org.gradle.java");
55+
}
56+
if (project.file("src/main/groovy").exists()
57+
|| project.file("src/test/groovy").exists()
58+
|| project.file("src/integration-test/groovy").exists()) {
59+
pluginManager.apply("org.gradle.groovy");
60+
}
61+
if (project.file("src/main/kotlin").exists()
62+
|| project.file("src/test/kotlin").exists()
63+
|| project.file("src/integration-test/kotlin").exists()
64+
|| project.getBuildFile().getName().endsWith(".kts")) {
65+
pluginManager.apply("org.jetbrains.kotlin.jvm");
66+
}
67+
pluginManager.apply(SpringRepositoryPlugin.class);
68+
pluginManager.apply(SpringManagementConfigurationPlugin.class);
69+
pluginManager.apply(SpringPropDepsEclipsePlugin.class);
70+
pluginManager.apply(SpringPropDepsIdeaPlugin.class);
71+
pluginManager.apply(SpringJavadocOptionsPlugin.class);
72+
pluginManager.apply(SpringJavaFormatPlugin.class);
73+
pluginManager.apply(SpringJavaCheckstylePlugin.class);
74+
pluginManager.apply(SpringCopyPropertiesPlugin.class);
75+
pluginManager.apply(SpringJacocoPlugin.class);
76+
77+
// Apply Java toolchain version
78+
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
79+
java.setTargetCompatibility(JavaVersion.VERSION_17);
80+
java.toolchain((toolchain) -> toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17)));
81+
82+
// Configure java compile task
83+
project.getTasks().withType(JavaCompile.class, (javaCompile) -> {
84+
CompileOptions options = javaCompile.getOptions();
85+
options.setEncoding("UTF-8");
86+
options.getCompilerArgs().add("-parameters");
87+
});
88+
89+
// Configure jar task
90+
project.getTasks().withType(Jar.class, (jar) -> jar.manifest((manifest) -> {
91+
Map<String, String> attributes = new HashMap<>();
92+
attributes.put("Created-By", String.format("%s (%s)", System.getProperty("java.version"), System.getProperty("java.specification.vendor")));
93+
attributes.put("Implementation-Title", project.getName());
94+
attributes.put("Implementation-Version", project.getVersion().toString());
95+
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
96+
manifest.attributes(attributes);
97+
}));
98+
99+
// Configure JUnit 5
100+
project.getTasks().withType(Test.class, Test::useJUnitPlatform);
101+
}
102+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.gradle.checkstyle;
18+
19+
import java.io.File;
20+
import java.util.Objects;
21+
22+
import javax.annotation.Nullable;
23+
24+
import io.spring.javaformat.gradle.tasks.CheckFormat;
25+
import org.gradle.api.Plugin;
26+
import org.gradle.api.Project;
27+
import org.gradle.api.plugins.JavaPlugin;
28+
import org.gradle.api.plugins.quality.CheckstyleExtension;
29+
import org.gradle.api.plugins.quality.CheckstylePlugin;
30+
31+
/**
32+
* Adds and configures Checkstyle plugin.
33+
*
34+
* @author Vedran Pavic
35+
* @author Steve Riesenberg
36+
*/
37+
public class SpringJavaCheckstylePlugin implements Plugin<Project> {
38+
private static final String CHECKSTYLE_DIR = "etc/checkstyle";
39+
private static final String SPRING_JAVAFORMAT_VERSION_PROPERTY = "springJavaformatVersion";
40+
private static final String DEFAULT_SPRING_JAVAFORMAT_VERSION = "0.0.38";
41+
private static final String NOHTTP_CHECKSTYLE_VERSION_PROPERTY = "nohttpCheckstyleVersion";
42+
private static final String DEFAULT_NOHTTP_CHECKSTYLE_VERSION = "0.0.11";
43+
private static final String CHECKSTYLE_TOOL_VERSION_PROPERTY = "checkstyleToolVersion";
44+
private static final String DEFAULT_CHECKSTYLE_TOOL_VERSION = "8.34";
45+
private static final String SPRING_JAVAFORMAT_EXCLUDE_PACKAGES_PROPERTY = "springJavaformatExcludePackages";
46+
47+
@Override
48+
public void apply(Project project) {
49+
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
50+
File checkstyleDir = project.getRootProject().file(CHECKSTYLE_DIR);
51+
if (checkstyleDir.exists() && checkstyleDir.isDirectory()) {
52+
project.getPluginManager().apply(CheckstylePlugin.class);
53+
54+
// NOTE: See gradle.properties#springJavaformatVersion for actual version number
55+
project.getDependencies().add("checkstyle", "io.spring.javaformat:spring-javaformat-checkstyle:" + getSpringJavaformatVersion(project));
56+
// NOTE: See gradle.properties#nohttpCheckstyleVersion for actual version number
57+
project.getDependencies().add("checkstyle", "io.spring.nohttp:nohttp-checkstyle:" + getNohttpCheckstyleVersion(project));
58+
59+
CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
60+
checkstyle.getConfigDirectory().set(checkstyleDir);
61+
// NOTE: See gradle.properties#checkstyleToolVersion for actual version number
62+
checkstyle.setToolVersion(getCheckstyleToolVersion(project));
63+
}
64+
65+
// Configure checkFormat task
66+
project.getTasks().withType(CheckFormat.class, (checkFormat) -> {
67+
// NOTE: See gradle.properties#springJavaformatExcludePackages for excluded packages
68+
String[] springJavaformatExcludePackages = getSpringJavaformatExcludePackages(project);
69+
if (springJavaformatExcludePackages != null) {
70+
checkFormat.exclude(springJavaformatExcludePackages);
71+
}
72+
});
73+
});
74+
}
75+
76+
private static String getSpringJavaformatVersion(Project project) {
77+
String springJavaformatVersion = DEFAULT_SPRING_JAVAFORMAT_VERSION;
78+
if (project.hasProperty(SPRING_JAVAFORMAT_VERSION_PROPERTY)) {
79+
springJavaformatVersion = Objects.requireNonNull(project.findProperty(SPRING_JAVAFORMAT_VERSION_PROPERTY)).toString();
80+
}
81+
return springJavaformatVersion;
82+
}
83+
84+
private static String getNohttpCheckstyleVersion(Project project) {
85+
String nohttpCheckstyleVersion = DEFAULT_NOHTTP_CHECKSTYLE_VERSION;
86+
if (project.hasProperty(NOHTTP_CHECKSTYLE_VERSION_PROPERTY)) {
87+
nohttpCheckstyleVersion = Objects.requireNonNull(project.findProperty(NOHTTP_CHECKSTYLE_VERSION_PROPERTY)).toString();
88+
}
89+
return nohttpCheckstyleVersion;
90+
}
91+
92+
private static String getCheckstyleToolVersion(Project project) {
93+
String checkstyleToolVersion = DEFAULT_CHECKSTYLE_TOOL_VERSION;
94+
if (project.hasProperty(CHECKSTYLE_TOOL_VERSION_PROPERTY)) {
95+
checkstyleToolVersion = Objects.requireNonNull(project.findProperty(CHECKSTYLE_TOOL_VERSION_PROPERTY)).toString();
96+
}
97+
return checkstyleToolVersion;
98+
}
99+
100+
@Nullable
101+
private String[] getSpringJavaformatExcludePackages(Project project) {
102+
String springJavaformatExcludePackages = (String) project.findProperty(SPRING_JAVAFORMAT_EXCLUDE_PACKAGES_PROPERTY);
103+
return (springJavaformatExcludePackages != null) ? springJavaformatExcludePackages.split(" ") : null;
104+
}
105+
}

0 commit comments

Comments
 (0)