Use Property<String>s for main class configuration in the Gradle plugin

Closes gh-23608
This commit is contained in:
Andy Wilkinson
2020-10-08 14:22:44 +01:00
parent 91acd957b2
commit 70ed7784a6
82 changed files with 197 additions and 154 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -63,7 +63,7 @@ class MainClassConventionTests {
void springBootExtensionMainClassNameIsUsed() throws Exception {
SpringBootExtension extension = this.project.getExtensions().create("springBoot", SpringBootExtension.class,
this.project);
extension.setMainClassName("com.example.MainClass");
extension.getMainClass().set("com.example.MainClass");
assertThat(this.convention.call()).isEqualTo("com.example.MainClass");
}
@@ -74,7 +74,7 @@ class MainClassConventionTests {
javaApplication.setMainClassName("com.example.JavaApplicationMainClass");
SpringBootExtension extension = this.project.getExtensions().create("springBoot", SpringBootExtension.class,
this.project);
extension.setMainClassName("com.example.SpringBootExtensionMainClass");
extension.getMainClass().set("com.example.SpringBootExtensionMainClass");
assertThat(this.convention.call()).isEqualTo("com.example.SpringBootExtensionMainClass");
}

View File

@@ -99,7 +99,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void basicArchiveCreation() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
executeTask();
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
assertThat(jarFile.getManifest().getMainAttributes().getValue("Main-Class")).isEqualTo(this.launcherClass);
@@ -113,7 +113,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void classpathJarsArePackagedBeneathLibPathAndAreStored() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(jarFile("one.jar"), jarFile("two.jar"));
executeTask();
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
@@ -126,7 +126,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void classpathDirectoriesArePackagedBeneathClassesPath() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
File classpathDirectory = new File(this.temp, "classes");
File applicationClass = new File(classpathDirectory, "com/example/Application.class");
applicationClass.getParentFile().mkdirs();
@@ -140,7 +140,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void moduleInfoClassIsPackagedInTheRootOfTheArchive() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
File classpathDirectory = new File(this.temp, "classes");
File moduleInfoClass = new File(classpathDirectory, "module-info.class");
moduleInfoClass.getParentFile().mkdirs();
@@ -160,7 +160,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void classpathCanBeSetUsingAFileCollection() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(jarFile("one.jar"));
this.task.setClasspath(this.task.getProject().files(jarFile("two.jar")));
executeTask();
@@ -172,7 +172,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void classpathCanBeSetUsingAnObject() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(jarFile("one.jar"));
this.task.setClasspath(jarFile("two.jar"));
executeTask();
@@ -184,7 +184,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void filesOnTheClasspathThatAreNotZipFilesAreSkipped() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(new File("test.pom"));
executeTask();
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
@@ -194,7 +194,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void loaderIsWrittenToTheRootOfTheJarAfterManifest() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
executeTask();
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
assertThat(jarFile.getEntry("org/springframework/boot/loader/LaunchedURLClassLoader.class")).isNotNull();
@@ -210,7 +210,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void loaderIsWrittenToTheRootOfTheJarWhenUsingThePropertiesLauncher() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
executeTask();
this.task.getManifest().getAttributes().put("Main-Class", "org.springframework.boot.loader.PropertiesLauncher");
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
@@ -221,7 +221,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void unpackCommentIsAddedToEntryIdentifiedByAPattern() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(jarFile("one.jar"), jarFile("two.jar"));
this.task.requiresUnpack("**/one.jar");
executeTask();
@@ -233,7 +233,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void unpackCommentIsAddedToEntryIdentifiedByASpec() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(jarFile("one.jar"), jarFile("two.jar"));
this.task.requiresUnpack((element) -> element.getName().endsWith("two.jar"));
executeTask();
@@ -245,7 +245,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void launchScriptCanBePrepended() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.launchScript();
executeTask();
Map<String, String> properties = new HashMap<>();
@@ -266,7 +266,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void customLaunchScriptCanBePrepended() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
File customScript = new File(this.temp, "custom.script");
Files.write(customScript.toPath(), Arrays.asList("custom script"), StandardOpenOption.CREATE);
this.task.launchScript((configuration) -> configuration.setScript(customScript));
@@ -277,7 +277,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void launchScriptInitInfoPropertiesCanBeCustomized() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.launchScript((configuration) -> {
configuration.getProperties().put("initInfoProvides", "provides");
configuration.getProperties().put("initInfoShortDescription", "short description");
@@ -292,7 +292,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void customMainClassInTheManifestIsHonored() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.getManifest().getAttributes().put("Main-Class", "com.example.CustomLauncher");
executeTask();
assertThat(this.task.getArchiveFile().get().getAsFile()).exists();
@@ -306,7 +306,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void customStartClassInTheManifestIsHonored() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.getManifest().getAttributes().put("Start-Class", "com.example.CustomMain");
executeTask();
assertThat(this.task.getArchiveFile().get().getAsFile()).exists();
@@ -319,7 +319,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void fileTimestampPreservationCanBeDisabled() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.setPreserveFileTimestamps(false);
executeTask();
assertThat(this.task.getArchiveFile().get().getAsFile()).exists();
@@ -340,7 +340,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void reproducibleOrderingCanBeEnabled() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.from(newFile("bravo.txt"), newFile("alpha.txt"), newFile("charlie.txt"));
this.task.setReproducibleFileOrder(true);
executeTask();
@@ -360,7 +360,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void devtoolsJarIsExcludedByDefault() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(newFile("spring-boot-devtools-0.1.2.jar"));
executeTask();
assertThat(this.task.getArchiveFile().get().getAsFile()).exists();
@@ -372,7 +372,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
@Deprecated
void devtoolsJarCanBeIncluded() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.classpath(jarFile("spring-boot-devtools-0.1.2.jar"));
this.task.setExcludeDevtools(false);
executeTask();
@@ -384,7 +384,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void allEntriesUseUnixPlatformAndUtf8NameEncoding() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
this.task.setMetadataCharset("UTF-8");
File classpathDirectory = new File(this.temp, "classes");
File resource = new File(classpathDirectory, "some-resource.xml");
@@ -405,7 +405,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
void loaderIsWrittenFirstThenApplicationClassesThenLibraries() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.getMainClass().set("com.example.Main");
File classpathDirectory = new File(this.temp, "classes");
File applicationClass = new File(classpathDirectory, "com/example/Application.class");
applicationClass.getParentFile().mkdirs();

View File

@@ -65,7 +65,7 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
@Test
void contentCanBeAddedToBootInfUsingCopySpecFromGetter() throws IOException {
BootJar bootJar = getTask();
bootJar.setMainClassName("com.example.Application");
bootJar.getMainClass().set("com.example.Application");
bootJar.getBootInf().into("test").from(new File("build.gradle").getAbsolutePath());
bootJar.copy();
try (JarFile jarFile = new JarFile(bootJar.getArchiveFile().get().getAsFile())) {
@@ -76,7 +76,7 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
@Test
void contentCanBeAddedToBootInfUsingCopySpecAction() throws IOException {
BootJar bootJar = getTask();
bootJar.setMainClassName("com.example.Application");
bootJar.getMainClass().set("com.example.Application");
bootJar.bootInf((copySpec) -> copySpec.into("test").from(new File("build.gradle").getAbsolutePath()));
bootJar.copy();
try (JarFile jarFile = new JarFile(bootJar.getArchiveFile().get().getAsFile())) {
@@ -276,7 +276,7 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
@SuppressWarnings("unchecked")
private void addContent() throws IOException {
BootJar bootJar = getTask();
bootJar.setMainClassName("com.example.Main");
bootJar.getMainClass().set("com.example.Main");
File classesJavaMain = new File(this.temp, "classes/java/main");
File applicationClass = new File(classesJavaMain, "com/example/Application.class");
applicationClass.getParentFile().mkdirs();

View File

@@ -37,7 +37,7 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
@Test
void providedClasspathJarsArePackagedInWebInfLibProvided() throws IOException {
getTask().setMainClassName("com.example.Main");
getTask().getMainClass().set("com.example.Main");
getTask().providedClasspath(jarFile("one.jar"), jarFile("two.jar"));
executeTask();
try (JarFile jarFile = new JarFile(getTask().getArchiveFile().get().getAsFile())) {
@@ -48,7 +48,7 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
@Test
void providedClasspathCanBeSetUsingAFileCollection() throws IOException {
getTask().setMainClassName("com.example.Main");
getTask().getMainClass().set("com.example.Main");
getTask().providedClasspath(jarFile("one.jar"));
getTask().setProvidedClasspath(getTask().getProject().files(jarFile("two.jar")));
executeTask();
@@ -60,7 +60,7 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
@Test
void providedClasspathCanBeSetUsingAnObject() throws IOException {
getTask().setMainClassName("com.example.Main");
getTask().getMainClass().set("com.example.Main");
getTask().providedClasspath(jarFile("one.jar"));
getTask().setProvidedClasspath(jarFile("two.jar"));
executeTask();
@@ -72,7 +72,7 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
@Test
void devtoolsJarIsExcludedByDefaultWhenItsOnTheProvidedClasspath() throws IOException {
getTask().setMainClassName("com.example.Main");
getTask().getMainClass().set("com.example.Main");
getTask().providedClasspath(newFile("spring-boot-devtools-0.1.2.jar"));
executeTask();
try (JarFile jarFile = new JarFile(getTask().getArchiveFile().get().getAsFile())) {
@@ -83,7 +83,7 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
@Test
@Deprecated
void devtoolsJarCanBeIncludedWhenItsOnTheProvidedClasspath() throws IOException {
getTask().setMainClassName("com.example.Main");
getTask().getMainClass().set("com.example.Main");
getTask().providedClasspath(jarFile("spring-boot-devtools-0.1.2.jar"));
getTask().setExcludeDevtools(false);
executeTask();
@@ -100,7 +100,7 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
orgDirectory.mkdir();
new File(orgDirectory, "foo.txt").createNewFile();
getTask().from(webappDirectory);
getTask().setMainClassName("com.example.Main");
getTask().getMainClass().set("com.example.Main");
executeTask();
try (JarFile jarFile = new JarFile(getTask().getArchiveFile().get().getAsFile())) {
assertThat(jarFile.getEntry("org/")).isNotNull();
@@ -110,7 +110,7 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
@Test
void libProvidedEntriesAreWrittenAfterLibEntries() throws IOException {
getTask().setMainClassName("com.example.Main");
getTask().getMainClass().set("com.example.Main");
getTask().classpath(jarFile("library.jar"));
getTask().providedClasspath(jarFile("provided-library.jar"));
executeTask();