Skip to content

Commit

Permalink
Initial version of spring-security-project-plugin
Browse files Browse the repository at this point in the history
Issue gh-54
  • Loading branch information
sjohnr committed Nov 6, 2024
1 parent 11d2c2c commit 65c0400
Show file tree
Hide file tree
Showing 16 changed files with 1,028 additions and 0 deletions.
42 changes: 42 additions & 0 deletions project-plugin/spring-security-project-plugin.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
plugins {
id "io.spring.convention.spring-gradle-plugin"
id "com.gradle.plugin-publish" version "1.2.1"
}

gradlePlugin {
website = "https://github.com/spring-io/spring-security-release-tools/tree/main/project-plugin"
vcsUrl = "https://github.com/spring-io/spring-security-release-tools.git"
plugins {
springSecurityProjectPlugin {
id = "io.spring.security.project"
displayName = "Spring Security Project Plugin"
description = "Spring Security Project Plugin is a Gradle plugin that applies common configuration conventions to Java, Kotlin or Groovy modules."
tags.addAll("spring", "project", "java", "kotlin", "groovy")
implementationClass = "io.spring.gradle.plugin.module.SpringProjectPlugin"
}
}
}

artifactory {
publish {
defaults {
publications("pluginMaven", "springSecurityProjectPluginPluginMarkerMaven")
}
}
}

repositories {
gradlePluginPortal()
}

dependencies {
management platform(project(":dependencies"))
implementation "io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.42"
implementation "io.spring.nohttp:nohttp-gradle:0.0.11"

testImplementation "org.junit.jupiter:junit-jupiter"
testRuntimeOnly "org.junit.platform:junit-platform-launcher"

testImplementation "org.assertj:assertj-core"
testImplementation "org.mockito:mockito-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.spring.gradle.plugin.module;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.PluginManager;

import org.springframework.gradle.SpringJavaPlugin;
import org.springframework.gradle.classpath.SpringCheckClasspathForProhibitedDependenciesPlugin;
import org.springframework.gradle.classpath.SpringCheckProhibitedDependenciesLifecyclePlugin;

/**
* @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(SpringCheckProhibitedDependenciesLifecyclePlugin.class);
}
else {
pluginManager.apply(JavaLibraryPlugin.class);
pluginManager.apply(SpringJavaPlugin.class);
pluginManager.apply(SpringCheckClasspathForProhibitedDependenciesPlugin.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.gradle;

import java.util.HashMap;
import java.util.Map;

import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.plugins.PluginManager;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.testing.Test;
import org.gradle.jvm.tasks.Jar;
import org.gradle.jvm.toolchain.JavaLanguageVersion;

import org.springframework.gradle.checkstyle.SpringJavaCheckstylePlugin;
import org.springframework.gradle.docs.SpringJavadocOptionsPlugin;
import org.springframework.gradle.jacoco.SpringJacocoPlugin;
import org.springframework.gradle.management.SpringManagementConfigurationPlugin;
import org.springframework.gradle.propdeps.SpringPropDepsEclipsePlugin;
import org.springframework.gradle.propdeps.SpringPropDepsIdeaPlugin;
import org.springframework.gradle.properties.SpringCopyPropertiesPlugin;
import org.springframework.gradle.repository.SpringRepositoryPlugin;

/**
* @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()
|| 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()
|| 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()
|| project.file("src/integration-test/kotlin").exists()
|| project.getBuildFile().getName().endsWith(".kts")) {
pluginManager.apply("org.jetbrains.kotlin.jvm");
}
pluginManager.apply(SpringRepositoryPlugin.class);
pluginManager.apply(SpringManagementConfigurationPlugin.class);
pluginManager.apply(SpringPropDepsEclipsePlugin.class);
pluginManager.apply(SpringPropDepsIdeaPlugin.class);
pluginManager.apply(SpringJavadocOptionsPlugin.class);
pluginManager.apply(SpringJavaFormatPlugin.class);
pluginManager.apply(SpringJavaCheckstylePlugin.class);
pluginManager.apply(SpringCopyPropertiesPlugin.class);
pluginManager.apply(SpringJacocoPlugin.class);

// Apply Java toolchain version
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
java.setTargetCompatibility(JavaVersion.VERSION_17);
java.toolchain((toolchain) -> toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17)));

// Configure java compile task
project.getTasks().withType(JavaCompile.class, (javaCompile) -> {
CompileOptions options = javaCompile.getOptions();
options.setEncoding("UTF-8");
options.getCompilerArgs().add("-parameters");
});

// 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("Implementation-Title", project.getName());
attributes.put("Implementation-Version", project.getVersion().toString());
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
manifest.attributes(attributes);
}));

// Configure JUnit 5
project.getTasks().withType(Test.class, Test::useJUnitPlatform);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.gradle.checkstyle;

import java.io.File;
import java.util.Objects;

import javax.annotation.Nullable;

import io.spring.javaformat.gradle.tasks.CheckFormat;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.quality.CheckstyleExtension;
import org.gradle.api.plugins.quality.CheckstylePlugin;

/**
* Adds and configures Checkstyle plugin.
*
* @author Vedran Pavic
* @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
public void apply(Project project) {
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
File checkstyleDir = project.getRootProject().file(CHECKSTYLE_DIR);
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));

CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
checkstyle.getConfigDirectory().set(checkstyleDir);
// 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
String[] springJavaformatExcludePackages = getSpringJavaformatExcludePackages(project);
if (springJavaformatExcludePackages != null) {
checkFormat.exclude(springJavaformatExcludePackages);
}
});
});
}

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();
}
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();
}
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();
}
return checkstyleToolVersion;
}

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

0 comments on commit 65c0400

Please sign in to comment.