Skip to content

Commit

Permalink
Polish manifests of source and javadoc jars
Browse files Browse the repository at this point in the history
Previously, the main jar file and the source and javadoc jar files all
had the same changes applied to their manifests.

The commit changes the Implementation-Title of source and javadoc jars
so that the title indicates that's what they are. Rather than using
the project's description as the title (as is done for the main jar),
the titles for source and javadoc jars will now be "Source for
${project.name}" and "Javadoc for ${project.name}" respectively.

Closes gh-23974
  • Loading branch information
wilkinsona committed Nov 3, 2020
1 parent 8294083 commit e820400
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import io.spring.javaformat.gradle.FormatTask;
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
Expand All @@ -34,6 +36,8 @@
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.quality.CheckstyleExtension;
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.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
Expand Down Expand Up @@ -97,20 +101,37 @@ private void configureJarManifestConventions(Project project) {
extractLegalResources.getDestinationDirectory().set(project.getLayout().getBuildDirectory().dir("legal"));
extractLegalResources.setResourcesNames(Arrays.asList("LICENSE.txt", "NOTICE.txt"));
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) -> {
jar.metaInf((metaInf) -> metaInf.from(extractLegalResources));
jar.manifest((manifest) -> {
Map<String, Object> attributes = new TreeMap<>();
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
attributes.put("Build-Jdk-Spec", project.property("sourceCompatibility"));
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());
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) {
project.getTasks().withType(Test.class, (test) -> {
withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

Expand Down Expand Up @@ -71,23 +72,99 @@ void jarIncludesLegalFiles() throws IOException {
out.println(" id 'java'");
out.println(" id 'org.springframework.boot.conventions'");
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'");
}
runGradle("jar");
File file = new File(this.projectDir, "/build/libs/test.jar");
assertThat(file).exists();
try (JarFile jar = new JarFile(file)) {
JarEntry license = jar.getJarEntry("META-INF/LICENSE.txt");
assertThat(license).isNotNull();
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("${");
assertThatLicenseIsPresent(jar);
assertThatNoticeIsPresent(jar);
Attributes mainAttributes = jar.getManifest().getMainAttributes();
assertThat(mainAttributes.getValue("Implementation-Title"))
.isEqualTo("Test project for manifest customization");
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 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
void testRetryIsConfiguredWithThreeRetriesOnCI() throws IOException {
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
Expand Down

0 comments on commit e820400

Please sign in to comment.