Skip to content

Commit 1f3b728

Browse files
authored
Add javadoc/sources variants if the corresponding Jar is published (#46)
1 parent 7760ddc commit 1f3b728

File tree

11 files changed

+564
-12
lines changed

11 files changed

+564
-12
lines changed

src/main/java/org/gradlex/maven/gmm/GradleModuleMetadataWriter.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Collections;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.Optional;
3334
import java.util.TreeMap;
3435

3536
/**
@@ -40,16 +41,24 @@ public class GradleModuleMetadataWriter {
4041
private static final String FORMAT_VERSION = "1.1";
4142

4243
private enum Variant {
43-
API_ELEMENTS("apiElements", "java-api", Collections.singletonList("compile")),
44-
RUNTIME_ELEMENTS("runtimeElements", "java-runtime", Arrays.asList("compile", "runtime"));
44+
API_ELEMENTS("apiElements", "java-api", "library", "jar", null, Collections.singletonList("compile")),
45+
RUNTIME_ELEMENTS("runtimeElements", "java-runtime", "library", "jar", null, Arrays.asList("compile", "runtime")),
46+
JAVADOC_ELEMENTS("javadocElements", "java-runtime", "documentation", null, "javadoc", Collections.emptyList()),
47+
SOURCES_ELEMENTS("sourcesElements", "java-runtime", "documentation", null, "sources", Collections.emptyList());
4548

4649
private final String name;
4750
private final String usage;
51+
private final String category;
52+
private final String libraryelements; //nullable
53+
private final String docstype; // nullable
4854
private final List<String> scopes;
4955

50-
Variant(String name, String usage, List<String> scopes) {
56+
Variant(String name, String usage, String category, String libraryelements, String docstype, List<String> scopes) {
5157
this.name = name;
5258
this.usage = usage;
59+
this.category = category;
60+
this.libraryelements = libraryelements;
61+
this.docstype = docstype;
5362
this.scopes = scopes;
5463
}
5564
}
@@ -78,9 +87,14 @@ private static Map<String, String> componentAttributes(MavenProject project) {
7887
private static Map<String, String> variantAttributes(Variant variant) {
7988
Map<String, String> attributes = new TreeMap<>();
8089

81-
attributes.put("org.gradle.category", "library");
90+
attributes.put("org.gradle.category", variant.category);
8291
attributes.put("org.gradle.dependency.bundling", "external");
83-
attributes.put("org.gradle.libraryelements", "jar");
92+
if (variant.libraryelements != null) {
93+
attributes.put("org.gradle.libraryelements", variant.libraryelements);
94+
}
95+
if (variant.docstype != null) {
96+
attributes.put("org.gradle.docstype", variant.docstype);
97+
}
8498

8599
attributes.put("org.gradle.usage", variant.usage);
86100

@@ -124,10 +138,20 @@ private static void writeVariants(MavenProject project,
124138
List<Dependency> removedDependencies,
125139
List<Dependency> compileOnlyApiDependencies,
126140
JsonWriter jsonWriter) throws IOException {
141+
142+
Optional<Artifact> javadocJar = project.getAttachedArtifacts().stream().filter(jar -> "javadoc".equals(jar.getClassifier())).findFirst();
143+
Optional<Artifact> sourcesJar = project.getAttachedArtifacts().stream().filter(jar -> "sources".equals(jar.getClassifier())).findFirst();
144+
127145
jsonWriter.name("variants");
128146
jsonWriter.beginArray();
129-
writeVariant(project, Variant.API_ELEMENTS, platformDependencies, capabilities, removedDependencies, compileOnlyApiDependencies, jsonWriter);
130-
writeVariant(project, Variant.RUNTIME_ELEMENTS, platformDependencies, capabilities, removedDependencies, null, jsonWriter);
147+
writeVariant(project, Variant.API_ELEMENTS, platformDependencies, capabilities, removedDependencies, compileOnlyApiDependencies, project.getArtifact(), jsonWriter);
148+
writeVariant(project, Variant.RUNTIME_ELEMENTS, platformDependencies, capabilities, removedDependencies, null, project.getArtifact(), jsonWriter);
149+
if (javadocJar.isPresent()) {
150+
writeVariant(project, Variant.JAVADOC_ELEMENTS, null, null, null, null, javadocJar.get(), jsonWriter);
151+
}
152+
if (sourcesJar.isPresent()) {
153+
writeVariant(project, Variant.SOURCES_ELEMENTS, null, null, null, null, sourcesJar.get(), jsonWriter);
154+
}
131155
jsonWriter.endArray();
132156
}
133157

@@ -141,13 +165,13 @@ private static void writeVariant(MavenProject project, Variant variant,
141165
List<Capability> capabilities,
142166
List<Dependency> removedDependencies,
143167
List<Dependency> addedDependencies,
144-
JsonWriter jsonWriter) throws IOException {
168+
Artifact artifact, JsonWriter jsonWriter) throws IOException {
145169
jsonWriter.beginObject();
146170
jsonWriter.name("name");
147171
jsonWriter.value(variant.name);
148172
writeAttributes(variantAttributes(variant), jsonWriter);
149173
writeDependencies(variant, project.getDependencies(), platformDependencies, removedDependencies, addedDependencies, jsonWriter);
150-
writeArtifacts(project, jsonWriter);
174+
writeArtifacts(artifact, jsonWriter);
151175
writeCapabilities(project, capabilities, jsonWriter);
152176

153177
jsonWriter.endObject();
@@ -168,10 +192,10 @@ private static void writeAttributes(Map<String, String> attributes, JsonWriter j
168192
jsonWriter.endObject();
169193
}
170194

171-
private static void writeArtifacts(MavenProject project, JsonWriter jsonWriter) throws IOException {
195+
private static void writeArtifacts(Artifact artifact, JsonWriter jsonWriter) throws IOException {
172196
jsonWriter.name("files");
173197
jsonWriter.beginArray();
174-
writeArtifact(project.getArtifact(), jsonWriter);
198+
writeArtifact(artifact, jsonWriter);
175199
jsonWriter.endArray();
176200
}
177201

src/test/java/org/gradlex/maven/gmm/test/GMMMavenPluginTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.io.InputStreamReader;
3333
import java.nio.file.Files;
34+
import java.nio.file.Path;
3435
import java.util.Arrays;
3536
import java.util.List;
3637

@@ -246,6 +247,21 @@ void testVariantDependencies() {
246247
assertExpectedGMM("variant-dependencies");
247248
}
248249

250+
@Test
251+
void testWithJavadoc() {
252+
assertExpectedGMM("with-javadoc");
253+
}
254+
255+
@Test
256+
void testWithSources() {
257+
assertExpectedGMM("with-sources");
258+
}
259+
260+
@Test
261+
void testWithJavadocAndSources() {
262+
assertExpectedGMM("with-javadoc-and-sources");
263+
}
264+
249265
List<String> resolve() {
250266
BuildResult buildResult = GradleRunner.create()
251267
.forwardOutput()
@@ -280,6 +296,7 @@ private void assertExpectedGMM(String name) {
280296
File testPom = new File("src/test/resources/" + name + "/pom.xml");
281297
File testPomParent = new File("src/test/resources/" + name + "/parent/pom.xml");
282298
File gmmExpected = new File("src/test/resources/" + name + "/expected-module.json");
299+
File javaClass = new File("src/test/resources/" + name + "/Dummy.java");
283300
assertThat(gmmExpected).exists();
284301

285302
try {
@@ -289,6 +306,11 @@ private void assertExpectedGMM(String name) {
289306
Files.createDirectories(mavenProducerParent.getParentFile().toPath());
290307
Files.copy(testPomParent.toPath(), mavenProducerParent.toPath());
291308
}
309+
if (javaClass.exists()) {
310+
Path target = mavenProducerBuild.getParentFile().toPath().resolve("src/main/java/org/example/Dummy.java");
311+
Files.createDirectories(target.getParent());
312+
Files.copy(javaClass.toPath(), target);
313+
}
292314

293315
packageProducer();
294316

@@ -297,7 +319,7 @@ private void assertExpectedGMM(String name) {
297319

298320
JsonElement expected = JsonParser.parseReader(new FileReader(gmmExpected));
299321
JsonElement actual = JsonParser.parseReader(new FileReader(gmmActual));
300-
assertThat(expected).isEqualTo(actual);
322+
assertThat(actual).isEqualTo(expected);
301323
} catch (IOException e) {
302324
throw new RuntimeException(e);
303325
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.example;
2+
3+
/**
4+
* Doc
5+
*/
6+
public class Dummy {}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"formatVersion": "1.1",
3+
"component": {
4+
"group": "org.testing",
5+
"module": "example",
6+
"version": "1.0",
7+
"attributes": {
8+
"org.gradle.status": "release"
9+
}
10+
},
11+
"variants": [
12+
{
13+
"name": "apiElements",
14+
"attributes": {
15+
"org.gradle.category": "library",
16+
"org.gradle.dependency.bundling": "external",
17+
"org.gradle.libraryelements": "jar",
18+
"org.gradle.usage": "java-api"
19+
},
20+
"dependencies": [
21+
{
22+
"group": "org.junit",
23+
"module": "junit-bom",
24+
"version": {
25+
"requires": "5.6.0"
26+
},
27+
"attributes": {
28+
"org.gradle.category": "platform"
29+
},
30+
"endorseStrictVersions": true
31+
}
32+
],
33+
"files": [
34+
{
35+
"name": "example-1.0.jar",
36+
"url": "example-1.0.jar",
37+
"size": 2182,
38+
"sha512": "e3904e23db50e6d095b5f3f504c1f5da168341b6221584c8b060e8a29d6c632ad5936bed252d33aa0856f9de54300619a153fa5050432dcc20f1699c8d2087a5",
39+
"sha256": "6576b82592def810f66407778861e13a14dfc2bca37be7510e49c2e9bf983d6e",
40+
"sha1": "49ccac69a7c955fd2fc88ea49dfcc36d7f1ef044",
41+
"md5": "40241c3e9ff52d412e298b11b107fbfc"
42+
}
43+
]
44+
},
45+
{
46+
"name": "runtimeElements",
47+
"attributes": {
48+
"org.gradle.category": "library",
49+
"org.gradle.dependency.bundling": "external",
50+
"org.gradle.libraryelements": "jar",
51+
"org.gradle.usage": "java-runtime"
52+
},
53+
"dependencies": [
54+
{
55+
"group": "org.junit",
56+
"module": "junit-bom",
57+
"version": {
58+
"requires": "5.6.0"
59+
},
60+
"attributes": {
61+
"org.gradle.category": "platform"
62+
},
63+
"endorseStrictVersions": true
64+
}
65+
],
66+
"files": [
67+
{
68+
"name": "example-1.0.jar",
69+
"url": "example-1.0.jar",
70+
"size": 2182,
71+
"sha512": "e3904e23db50e6d095b5f3f504c1f5da168341b6221584c8b060e8a29d6c632ad5936bed252d33aa0856f9de54300619a153fa5050432dcc20f1699c8d2087a5",
72+
"sha256": "6576b82592def810f66407778861e13a14dfc2bca37be7510e49c2e9bf983d6e",
73+
"sha1": "49ccac69a7c955fd2fc88ea49dfcc36d7f1ef044",
74+
"md5": "40241c3e9ff52d412e298b11b107fbfc"
75+
}
76+
]
77+
},
78+
{
79+
"name": "javadocElements",
80+
"attributes": {
81+
"org.gradle.category": "documentation",
82+
"org.gradle.dependency.bundling": "external",
83+
"org.gradle.docstype": "javadoc",
84+
"org.gradle.usage": "java-runtime"
85+
},
86+
"files": [
87+
{
88+
"name": "example-1.0-javadoc.jar",
89+
"url": "example-1.0-javadoc.jar",
90+
"size": 82294,
91+
"sha512": "1aeb842140bbd05bbfba4883826fb5f14a7d5edce2af77304c4d321346864abe617137576e85201813e128e6b9a5e953730b5b9456b7b0a6ad370dbeee9b09e3",
92+
"sha256": "a6230826fb438bb6ee392295e4e1901fbe92b6d854dd7c72f421f694aa1bc1e8",
93+
"sha1": "071a6ac4c28b8b573cb647743c37953a4c9c9dba",
94+
"md5": "7daa17d2c9da4a70c984ee39ef05f6f6"
95+
}
96+
]
97+
},
98+
{
99+
"name": "sourcesElements",
100+
"attributes": {
101+
"org.gradle.category": "documentation",
102+
"org.gradle.dependency.bundling": "external",
103+
"org.gradle.docstype": "sources",
104+
"org.gradle.usage": "java-runtime"
105+
},
106+
"files": [
107+
{
108+
"name": "example-1.0-sources.jar",
109+
"url": "example-1.0-sources.jar",
110+
"size": 2018,
111+
"sha512": "49453629e5b110824b33adfff1527ccbdb79b1b0c958f4f15e919a15584e97e8923370fa32432e1a723acb34c5a9e6f8b831be54cc6d8a97d20a6a8258be9c17",
112+
"sha256": "1f20f7ba9c82e12187f61bbbfa232ddc99c8595c1c0ff055a6e5a3b652af1be8",
113+
"sha1": "b5e2375cb759a08bc638761220a756f6fad16ea5",
114+
"md5": "abfbaffd482cd013e146031fae07660a"
115+
}
116+
]
117+
}
118+
]
119+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<!-- do_not_remove: published-with-gradle-metadata -->
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>org.testing</groupId>
8+
<artifactId>example</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
<name>Test GMM Mojo</name>
12+
<properties>
13+
<maven.compiler.release>8</maven.compiler.release>
14+
<project.build.outputTimestamp>2023-01-01T00:00:00Z</project.build.outputTimestamp>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-javadoc-plugin</artifactId>
22+
<executions>
23+
<execution>
24+
<id>attach-javadocs</id>
25+
<goals>
26+
<goal>jar</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-source-plugin</artifactId>
34+
<executions>
35+
<execution>
36+
<id>attach-sources</id>
37+
<goals>
38+
<goal>jar</goal>
39+
</goals>
40+
</execution>
41+
</executions>
42+
</plugin>
43+
<plugin>
44+
<groupId>org.gradlex</groupId>
45+
<artifactId>gradle-module-metadata-maven-plugin</artifactId>
46+
<executions>
47+
<execution>
48+
<goals>
49+
<goal>gmm</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
<configuration>
54+
<platformDependencies>
55+
<dependency>
56+
<groupId>org.junit</groupId>
57+
<artifactId>junit-bom</artifactId>
58+
<version>5.6.0</version>
59+
</dependency>
60+
</platformDependencies>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.example;
2+
3+
/**
4+
* Doc
5+
*/
6+
public class Dummy {}

0 commit comments

Comments
 (0)