17
17
18
18
import java .util .Arrays ;
19
19
import java .util .List ;
20
+ import java .util .Map ;
21
+ import java .util .Set ;
22
+ import java .util .TreeMap ;
23
+ import java .util .stream .Collectors ;
20
24
21
25
import org .gradle .api .JavaVersion ;
22
26
import org .gradle .api .Project ;
23
27
import org .gradle .api .plugins .JavaBasePlugin ;
28
+ import org .gradle .api .tasks .SourceSet ;
29
+ import org .gradle .api .tasks .SourceSetContainer ;
30
+ import org .gradle .api .tasks .bundling .Jar ;
24
31
import org .gradle .api .tasks .compile .JavaCompile ;
25
32
import org .gradle .api .tasks .javadoc .Javadoc ;
26
33
import org .gradle .api .tasks .testing .Test ;
@@ -38,6 +45,7 @@ void apply(Project project) {
38
45
configureJavaConventions (project );
39
46
configureJavadocConventions (project );
40
47
configureTestConventions (project );
48
+ configureJarManifestConventions (project );
41
49
});
42
50
}
43
51
@@ -79,4 +87,36 @@ private void configureTestConventions(Project project) {
79
87
test .useJUnitPlatform ();
80
88
});
81
89
}
90
+
91
+ private void configureJarManifestConventions (Project project ) {
92
+ SourceSetContainer sourceSets = project .getExtensions ().getByType (SourceSetContainer .class );
93
+ Set <String > sourceJarTaskNames = sourceSets .stream ().map (SourceSet ::getSourcesJarTaskName )
94
+ .collect (Collectors .toSet ());
95
+ Set <String > javadocJarTaskNames = sourceSets .stream ().map (SourceSet ::getJavadocJarTaskName )
96
+ .collect (Collectors .toSet ());
97
+
98
+ project .getTasks ().withType (Jar .class , jar -> {
99
+ jar .manifest (manifest -> {
100
+ Map <String , Object > attributes = new TreeMap <>();
101
+ attributes .put ("Automatic-Module-Name" , project .getName ().replace ("-" , "." ));
102
+ attributes .put ("Build-Jdk-Spec" , SOURCE_AND_TARGET_COMPATIBILITY );
103
+ attributes .put ("Built-By" , "Spring" );
104
+ attributes .put ("Implementation-Title" ,
105
+ determineImplementationTitle (project , sourceJarTaskNames , javadocJarTaskNames , jar ));
106
+ attributes .put ("Implementation-Version" , project .getVersion ());
107
+ manifest .attributes (attributes );
108
+ });
109
+ });
110
+ }
111
+
112
+ private String determineImplementationTitle (Project project , Set <String > sourceJarTaskNames ,
113
+ Set <String > javadocJarTaskNames , Jar jar ) {
114
+ if (sourceJarTaskNames .contains (jar .getName ())) {
115
+ return "Source for " + project .getName ();
116
+ }
117
+ if (javadocJarTaskNames .contains (jar .getName ())) {
118
+ return "Javadoc for " + project .getName ();
119
+ }
120
+ return "Jar for " + project .getName ();
121
+ }
82
122
}
0 commit comments