diff --git a/build.gradle b/build.gradle index 1834fe9..663a902 100644 --- a/build.gradle +++ b/build.gradle @@ -15,8 +15,7 @@ dependencies { tasks.withType(AbstractCompile).configureEach { options.compilerArgs << "-Werror" << - "-Xlint:all" << - "-Xlint:-deprecation" // do not fail on deprecations introduced very lately in Gradle 7.1 + "-Xlint:all" } tasks.withType(Test).configureEach { diff --git a/src/main/java/nu/studer/gradle/rocker/GradleUtils.java b/src/main/java/nu/studer/gradle/rocker/GradleUtils.java new file mode 100644 index 0000000..c9cb714 --- /dev/null +++ b/src/main/java/nu/studer/gradle/rocker/GradleUtils.java @@ -0,0 +1,14 @@ +package nu.studer.gradle.rocker; + +import org.gradle.util.GradleVersion; + +final class GradleUtils { + + static boolean isAtLeastGradleVersion(String version) { + return GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version(version)) >= 0; + } + + private GradleUtils() { + } + +} diff --git a/src/main/java/nu/studer/gradle/rocker/RockerCompile.java b/src/main/java/nu/studer/gradle/rocker/RockerCompile.java index b7b2db0..9cdcc7f 100644 --- a/src/main/java/nu/studer/gradle/rocker/RockerCompile.java +++ b/src/main/java/nu/studer/gradle/rocker/RockerCompile.java @@ -24,7 +24,6 @@ import org.gradle.process.ExecOperations; import org.gradle.process.ExecResult; import org.gradle.process.JavaExecSpec; -import org.gradle.util.GradleVersion; import org.gradle.work.ChangeType; import org.gradle.work.InputChanges; @@ -34,6 +33,8 @@ import java.util.HashSet; import java.util.Set; +import static nu.studer.gradle.rocker.GradleUtils.isAtLeastGradleVersion; + public class RockerCompile extends DefaultTask { private static final String ROCKER_FILE_EXTENSION_PREFIX = ".rocker"; @@ -226,12 +227,7 @@ private ExecResult executeRocker(final File templateDir) { @Override public void execute(JavaExecSpec spec) { - String mainClass = "com.fizzed.rocker.compiler.JavaGeneratorMain"; - if (GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("6.4")) >= 0) { - spec.getMainClass().set(mainClass); - } else { - spec.setMain(mainClass); - } + setMainClass("com.fizzed.rocker.compiler.JavaGeneratorMain", spec); spec.setClasspath(runtimeClasspath); spec.setWorkingDir(projectLayout.getProjectDirectory()); spec.systemProperty("rocker.option.optimize", optimize.get().toString()); @@ -248,6 +244,19 @@ public void execute(JavaExecSpec spec) { } } + private void setMainClass(String mainClass, JavaExecSpec spec) { + if (isAtLeastGradleVersion("6.4")) { + spec.getMainClass().set(mainClass); + } else { + setMainClassDeprecated(mainClass, spec); + } + } + + @SuppressWarnings("deprecation") + private void setMainClassDeprecated(String mainClass, JavaExecSpec spec) { + spec.setMain(mainClass); + } + private void systemPropertyIfNotNull(String option, String value, JavaExecSpec spec) { if (value != null) { spec.systemProperty(option, value); diff --git a/src/main/java/nu/studer/gradle/rocker/RockerPlugin.java b/src/main/java/nu/studer/gradle/rocker/RockerPlugin.java index 3fa8d4e..45fbb74 100644 --- a/src/main/java/nu/studer/gradle/rocker/RockerPlugin.java +++ b/src/main/java/nu/studer/gradle/rocker/RockerPlugin.java @@ -5,7 +5,6 @@ import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ModuleVersionSelector; import org.gradle.api.plugins.JavaBasePlugin; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskProvider; @@ -14,6 +13,7 @@ import org.slf4j.LoggerFactory; import static java.lang.String.format; +import static nu.studer.gradle.rocker.GradleUtils.isAtLeastGradleVersion; import static nu.studer.gradle.rocker.StringUtils.capitalize; @SuppressWarnings("unused") @@ -49,12 +49,7 @@ public void apply(Project project) { // add the output of the rocker task as a source directory of the source set with the matching name (which adds an implicit task dependency) // add the rocker-runtime to the compile configuration in order to be able to compile the generated sources - SourceSetContainer sourceSets; - if (GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("7.1")) >= 0) { - sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets(); - } else { - sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets(); - } + SourceSetContainer sourceSets = getSourceSets(project); sourceSets.configureEach(sourceSet -> { if (sourceSet.getName().equals(config.name)) { sourceSet.getJava().srcDir(rocker.flatMap(RockerCompile::getOutputDir)); @@ -67,6 +62,19 @@ public void apply(Project project) { enforceRockerVersion(project, rockerExtension); } + private SourceSetContainer getSourceSets(Project project) { + if (isAtLeastGradleVersion("7.1")) { + return project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets(); + } else { + return getSourceSetsDeprecated(project); + } + } + + @SuppressWarnings("deprecation") + private SourceSetContainer getSourceSetsDeprecated(Project project) { + return project.getConvention().getPlugin(org.gradle.api.plugins.JavaPluginConvention.class).getSourceSets(); + } + private static Configuration createRockerCompilerRuntimeConfiguration(Project project) { Configuration rockerCompilerRuntime = project.getConfigurations().create("rockerCompiler"); rockerCompilerRuntime.setDescription("The classpath used to invoke the Rocker template engine. Add your additional dependencies here.");