commit
b50f8359b5
|
@ -20,8 +20,10 @@ import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import io.spring.javaformat.gradle.FormatTask;
|
import io.spring.javaformat.gradle.FormatTask;
|
||||||
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
|
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
|
||||||
|
@ -35,6 +37,8 @@ import org.gradle.api.plugins.JavaBasePlugin;
|
||||||
import org.gradle.api.plugins.JavaPlugin;
|
import org.gradle.api.plugins.JavaPlugin;
|
||||||
import org.gradle.api.plugins.quality.CheckstyleExtension;
|
import org.gradle.api.plugins.quality.CheckstyleExtension;
|
||||||
import org.gradle.api.plugins.quality.CheckstylePlugin;
|
import org.gradle.api.plugins.quality.CheckstylePlugin;
|
||||||
|
import org.gradle.api.tasks.SourceSet;
|
||||||
|
import org.gradle.api.tasks.SourceSetContainer;
|
||||||
import org.gradle.api.tasks.bundling.Jar;
|
import org.gradle.api.tasks.bundling.Jar;
|
||||||
import org.gradle.api.tasks.compile.JavaCompile;
|
import org.gradle.api.tasks.compile.JavaCompile;
|
||||||
import org.gradle.api.tasks.javadoc.Javadoc;
|
import org.gradle.api.tasks.javadoc.Javadoc;
|
||||||
|
@ -104,6 +108,11 @@ class JavaConventions {
|
||||||
extractLegalResources.getDestinationDirectory().set(project.getLayout().getBuildDirectory().dir("legal"));
|
extractLegalResources.getDestinationDirectory().set(project.getLayout().getBuildDirectory().dir("legal"));
|
||||||
extractLegalResources.setResourcesNames(Arrays.asList("LICENSE.txt", "NOTICE.txt"));
|
extractLegalResources.setResourcesNames(Arrays.asList("LICENSE.txt", "NOTICE.txt"));
|
||||||
extractLegalResources.property("version", project.getVersion().toString());
|
extractLegalResources.property("version", project.getVersion().toString());
|
||||||
|
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
|
||||||
|
Set<String> sourceJarTaskNames = sourceSets.stream().map(SourceSet::getSourcesJarTaskName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
Set<String> javadocJarTaskNames = sourceSets.stream().map(SourceSet::getJavadocJarTaskName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
project.getTasks().withType(Jar.class, (jar) -> project.afterEvaluate((evaluated) -> {
|
project.getTasks().withType(Jar.class, (jar) -> project.afterEvaluate((evaluated) -> {
|
||||||
jar.metaInf((metaInf) -> metaInf.from(extractLegalResources));
|
jar.metaInf((metaInf) -> metaInf.from(extractLegalResources));
|
||||||
jar.manifest((manifest) -> {
|
jar.manifest((manifest) -> {
|
||||||
|
@ -111,13 +120,25 @@ class JavaConventions {
|
||||||
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
|
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
|
||||||
attributes.put("Build-Jdk-Spec", project.property("sourceCompatibility"));
|
attributes.put("Build-Jdk-Spec", project.property("sourceCompatibility"));
|
||||||
attributes.put("Built-By", "Spring");
|
attributes.put("Built-By", "Spring");
|
||||||
attributes.put("Implementation-Title", project.getDescription());
|
attributes.put("Implementation-Title",
|
||||||
|
determineImplementationTitle(project, sourceJarTaskNames, javadocJarTaskNames, jar));
|
||||||
attributes.put("Implementation-Version", project.getVersion());
|
attributes.put("Implementation-Version", project.getVersion());
|
||||||
manifest.attributes(attributes);
|
manifest.attributes(attributes);
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String determineImplementationTitle(Project project, Set<String> sourceJarTaskNames,
|
||||||
|
Set<String> javadocJarTaskNames, Jar jar) {
|
||||||
|
if (sourceJarTaskNames.contains(jar.getName())) {
|
||||||
|
return "Source for " + project.getName();
|
||||||
|
}
|
||||||
|
if (javadocJarTaskNames.contains(jar.getName())) {
|
||||||
|
return "Javadoc for " + project.getName();
|
||||||
|
}
|
||||||
|
return project.getDescription();
|
||||||
|
}
|
||||||
|
|
||||||
private void configureTestConventions(Project project) {
|
private void configureTestConventions(Project project) {
|
||||||
project.getTasks().withType(Test.class, (test) -> {
|
project.getTasks().withType(Test.class, (test) -> {
|
||||||
withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java"));
|
withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java"));
|
||||||
|
|
|
@ -23,6 +23,7 @@ import java.io.InputStreamReader;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.jar.Attributes;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
|
@ -71,23 +72,99 @@ class ConventionsPluginTests {
|
||||||
out.println(" id 'java'");
|
out.println(" id 'java'");
|
||||||
out.println(" id 'org.springframework.boot.conventions'");
|
out.println(" id 'org.springframework.boot.conventions'");
|
||||||
out.println("}");
|
out.println("}");
|
||||||
out.println("description 'Test'");
|
out.println("version = '1.2.3'");
|
||||||
|
out.println("sourceCompatibility = '1.8'");
|
||||||
|
out.println("description 'Test project for manifest customization'");
|
||||||
out.println("jar.archiveFileName = 'test.jar'");
|
out.println("jar.archiveFileName = 'test.jar'");
|
||||||
}
|
}
|
||||||
runGradle("jar");
|
runGradle("jar");
|
||||||
File file = new File(this.projectDir, "/build/libs/test.jar");
|
File file = new File(this.projectDir, "/build/libs/test.jar");
|
||||||
assertThat(file).exists();
|
assertThat(file).exists();
|
||||||
try (JarFile jar = new JarFile(file)) {
|
try (JarFile jar = new JarFile(file)) {
|
||||||
JarEntry license = jar.getJarEntry("META-INF/LICENSE.txt");
|
assertThatLicenseIsPresent(jar);
|
||||||
assertThat(license).isNotNull();
|
assertThatNoticeIsPresent(jar);
|
||||||
JarEntry notice = jar.getJarEntry("META-INF/NOTICE.txt");
|
Attributes mainAttributes = jar.getManifest().getMainAttributes();
|
||||||
assertThat(notice).isNotNull();
|
assertThat(mainAttributes.getValue("Implementation-Title"))
|
||||||
String noticeContent = FileCopyUtils.copyToString(new InputStreamReader(jar.getInputStream(notice)));
|
.isEqualTo("Test project for manifest customization");
|
||||||
// Test that variables were replaced
|
assertThat(mainAttributes.getValue("Automatic-Module-Name"))
|
||||||
assertThat(noticeContent).doesNotContain("${");
|
.isEqualTo(this.projectDir.getName().replace("-", "."));
|
||||||
|
assertThat(mainAttributes.getValue("Implementation-Version")).isEqualTo("1.2.3");
|
||||||
|
assertThat(mainAttributes.getValue("Built-By")).isEqualTo("Spring");
|
||||||
|
assertThat(mainAttributes.getValue("Build-Jdk-Spec")).isEqualTo("1.8");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sourceJarIsBuilt() throws IOException {
|
||||||
|
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
|
||||||
|
out.println("plugins {");
|
||||||
|
out.println(" id 'java'");
|
||||||
|
out.println(" id 'maven-publish'");
|
||||||
|
out.println(" id 'org.springframework.boot.conventions'");
|
||||||
|
out.println("}");
|
||||||
|
out.println("version = '1.2.3'");
|
||||||
|
out.println("sourceCompatibility = '1.8'");
|
||||||
|
out.println("description 'Test'");
|
||||||
|
}
|
||||||
|
runGradle("build");
|
||||||
|
File file = new File(this.projectDir, "/build/libs/" + this.projectDir.getName() + "-1.2.3-sources.jar");
|
||||||
|
assertThat(file).exists();
|
||||||
|
try (JarFile jar = new JarFile(file)) {
|
||||||
|
assertThatLicenseIsPresent(jar);
|
||||||
|
assertThatNoticeIsPresent(jar);
|
||||||
|
Attributes mainAttributes = jar.getManifest().getMainAttributes();
|
||||||
|
assertThat(mainAttributes.getValue("Implementation-Title"))
|
||||||
|
.isEqualTo("Source for " + this.projectDir.getName());
|
||||||
|
assertThat(mainAttributes.getValue("Automatic-Module-Name"))
|
||||||
|
.isEqualTo(this.projectDir.getName().replace("-", "."));
|
||||||
|
assertThat(mainAttributes.getValue("Implementation-Version")).isEqualTo("1.2.3");
|
||||||
|
assertThat(mainAttributes.getValue("Built-By")).isEqualTo("Spring");
|
||||||
|
assertThat(mainAttributes.getValue("Build-Jdk-Spec")).isEqualTo("1.8");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void javadocJarIsBuilt() throws IOException {
|
||||||
|
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
|
||||||
|
out.println("plugins {");
|
||||||
|
out.println(" id 'java'");
|
||||||
|
out.println(" id 'maven-publish'");
|
||||||
|
out.println(" id 'org.springframework.boot.conventions'");
|
||||||
|
out.println("}");
|
||||||
|
out.println("version = '1.2.3'");
|
||||||
|
out.println("sourceCompatibility = '1.8'");
|
||||||
|
out.println("description 'Test'");
|
||||||
|
}
|
||||||
|
runGradle("build");
|
||||||
|
File file = new File(this.projectDir, "/build/libs/" + this.projectDir.getName() + "-1.2.3-javadoc.jar");
|
||||||
|
assertThat(file).exists();
|
||||||
|
try (JarFile jar = new JarFile(file)) {
|
||||||
|
assertThatLicenseIsPresent(jar);
|
||||||
|
assertThatNoticeIsPresent(jar);
|
||||||
|
Attributes mainAttributes = jar.getManifest().getMainAttributes();
|
||||||
|
assertThat(mainAttributes.getValue("Implementation-Title"))
|
||||||
|
.isEqualTo("Javadoc for " + this.projectDir.getName());
|
||||||
|
assertThat(mainAttributes.getValue("Automatic-Module-Name"))
|
||||||
|
.isEqualTo(this.projectDir.getName().replace("-", "."));
|
||||||
|
assertThat(mainAttributes.getValue("Implementation-Version")).isEqualTo("1.2.3");
|
||||||
|
assertThat(mainAttributes.getValue("Built-By")).isEqualTo("Spring");
|
||||||
|
assertThat(mainAttributes.getValue("Build-Jdk-Spec")).isEqualTo("1.8");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertThatLicenseIsPresent(JarFile jar) {
|
||||||
|
JarEntry license = jar.getJarEntry("META-INF/LICENSE.txt");
|
||||||
|
assertThat(license).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertThatNoticeIsPresent(JarFile jar) throws IOException {
|
||||||
|
JarEntry notice = jar.getJarEntry("META-INF/NOTICE.txt");
|
||||||
|
assertThat(notice).isNotNull();
|
||||||
|
String noticeContent = FileCopyUtils.copyToString(new InputStreamReader(jar.getInputStream(notice)));
|
||||||
|
// Test that variables were replaced
|
||||||
|
assertThat(noticeContent).doesNotContain("${");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRetryIsConfiguredWithThreeRetriesOnCI() throws IOException {
|
void testRetryIsConfiguredWithThreeRetriesOnCI() throws IOException {
|
||||||
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
|
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
|
||||||
|
|
Loading…
Reference in New Issue