Skip to content

Commit eb6c3f9

Browse files
committed
[#215] Test for multi release jar sources
Refs: #215, MSOURCES-144
1 parent bd3d302 commit eb6c3f9

File tree

6 files changed

+288
-1
lines changed

6 files changed

+288
-1
lines changed

src/main/java/org/apache/maven/plugins/source/SourceJarNoForkMojo.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@
1818
*/
1919
package org.apache.maven.plugins.source;
2020

21+
import java.nio.file.Paths;
22+
import java.util.ArrayList;
23+
import java.util.Collection;
2124
import java.util.Collections;
2225
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.stream.Collectors;
28+
import java.util.stream.Stream;
2329

30+
import org.apache.maven.model.Plugin;
31+
import org.apache.maven.model.PluginExecution;
2432
import org.apache.maven.model.Resource;
2533
import org.apache.maven.plugins.annotations.LifecyclePhase;
2634
import org.apache.maven.plugins.annotations.Mojo;
2735
import org.apache.maven.plugins.annotations.Parameter;
2836
import org.apache.maven.project.MavenProject;
37+
import org.codehaus.plexus.util.xml.Xpp3Dom;
2938

3039
/**
3140
* This goal bundles all the sources into a jar archive. This goal functions the same as the jar goal but does not fork
@@ -36,6 +45,7 @@
3645
*/
3746
@Mojo(name = "jar-no-fork", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
3847
public class SourceJarNoForkMojo extends AbstractSourceJarMojo {
48+
3949
/**
4050
* @since 2.2
4151
*/
@@ -46,7 +56,56 @@ public class SourceJarNoForkMojo extends AbstractSourceJarMojo {
4656
* {@inheritDoc}
4757
*/
4858
protected List<String> getSources(MavenProject p) {
49-
return p.getCompileSourceRoots();
59+
List<String> compilerConfigurationPaths = new ArrayList<>();
60+
61+
for (Plugin plugin : p.getModel().getBuild().getPlugins()) {
62+
if (!plugin.getArtifactId().equals("maven-compiler-plugin") || !plugin.getGroupId().equals("org.apache.maven.plugins")) {
63+
continue;
64+
}
65+
66+
// TODO: detect execution configurations and default configuration whether it includes additional source roots.
67+
Object compilerConfig = plugin.getConfiguration();
68+
compilerConfigurationPaths.addAll(readPathsFromCompilerConfig(compilerConfig));
69+
70+
for (PluginExecution execution : plugin.getExecutions()) {
71+
Object executionConfiguration = execution.getConfiguration();
72+
compilerConfigurationPaths.addAll(readPathsFromCompilerConfig(executionConfiguration));
73+
}
74+
75+
break;
76+
}
77+
78+
return Stream.concat(
79+
compilerConfigurationPaths.stream(),
80+
p.getCompileSourceRoots().stream()
81+
).collect(Collectors.toList());
82+
}
83+
84+
private Collection<String> readPathsFromCompilerConfig(Object compilerConfig) {
85+
if (compilerConfig == null) {
86+
return Collections.emptyList();
87+
}
88+
89+
if (!(compilerConfig instanceof Xpp3Dom)) {
90+
return Collections.emptyList();
91+
}
92+
93+
Xpp3Dom configuration = (Xpp3Dom) compilerConfig;
94+
Xpp3Dom compileSourceRoots = configuration.getChild("compileSourceRoots");
95+
96+
if (compileSourceRoots == null) {
97+
return Collections.emptyList();
98+
}
99+
100+
Xpp3Dom[] compileSourceRootsChildren = compileSourceRoots.getChildren();
101+
102+
List<String> compileSourcePaths = new ArrayList<>();
103+
104+
for (Xpp3Dom child : compileSourceRootsChildren) {
105+
compileSourcePaths.add(child.getValue());
106+
}
107+
108+
return compileSourcePaths;
50109
}
51110

52111
/**

src/test/java/org/apache/maven/plugins/source/SourceJarMojoTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.maven.plugins.source;
2020

2121
import java.io.File;
22+
import java.nio.file.Path;
23+
import java.nio.file.Paths;
2224

2325
/**
2426
* @author <a href="mailto:oching@exist.com">Maria Odea Ching</a>
@@ -122,4 +124,17 @@ public void testIncludeMavenDescriptorWhenExplicitlyConfigured() throws Exceptio
122124
}),
123125
"sources");
124126
}
127+
128+
public void testIncludesAdditionalSourcesFromMain() throws Exception {
129+
// must include MRJar configuration from the compiler plugin
130+
doTestProjectWithSourceArchive(
131+
"msources-144",
132+
addMavenDescriptor("msources-144", new String[] {
133+
"SomeClass.java",
134+
"META-INF/versions/9/SomeClass.java"
135+
}
136+
),
137+
"sources"
138+
);
139+
}
125140
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.source.stubs;
20+
21+
import static org.apache.maven.plugins.source.stubs.Project001Stub.readModelFromFile;
22+
23+
import org.apache.maven.model.Build;
24+
import org.apache.maven.model.Model;
25+
import org.apache.maven.model.Resource;
26+
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
27+
28+
import java.io.File;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
public class MSources144Stub extends MavenProjectStub {
33+
private Build build;
34+
35+
private List<Resource> resources;
36+
37+
private List<Resource> testResources;
38+
39+
public MSources144Stub() {
40+
Model model;
41+
42+
try {
43+
model = readModelFromFile(new File(getBasedir(), "target/test-classes/unit/msources-144/pom.xml"));
44+
setModel(model);
45+
46+
setFile(new File(getBasedir(), "target/test-classes/unit/msources-144/pom.xml"));
47+
48+
setGroupId(model.getGroupId());
49+
setArtifactId(model.getArtifactId());
50+
setVersion(model.getVersion());
51+
setName(model.getName());
52+
setUrl(model.getUrl());
53+
setPackaging(model.getPackaging());
54+
55+
Build build = new Build();
56+
build.setFinalName(getArtifactId() + "-" + getVersion());
57+
build.setDirectory(getBasedir() + "/target/test/unit/msources-144/target");
58+
59+
setBuild(build);
60+
61+
String basedir = getBasedir().getAbsolutePath();
62+
List<String> compileSourceRoots = new ArrayList<>();
63+
compileSourceRoots.add(basedir + "/target/test-classes/unit/msources-144/src/main/java");
64+
setCompileSourceRoots(compileSourceRoots);
65+
66+
List<String> testCompileSourceRoots = new ArrayList<>();
67+
testCompileSourceRoots.add(basedir + "/target/test-classes/unit/msources-144/src/test/java");
68+
setTestCompileSourceRoots(testCompileSourceRoots);
69+
70+
setResources(model.getBuild().getResources());
71+
setTestResources(model.getBuild().getTestResources());
72+
73+
SourcePluginArtifactStub artifact =
74+
new SourcePluginArtifactStub(getGroupId(), getArtifactId(), getVersion(), getPackaging(), null);
75+
artifact.setArtifactHandler(new DefaultArtifactHandlerStub());
76+
artifact.setType("jar");
77+
artifact.setBaseVersion("1.0-SNAPSHOT");
78+
setArtifact(artifact);
79+
80+
} catch (Exception e) {
81+
e.printStackTrace();
82+
}
83+
}
84+
85+
public Build getBuild() {
86+
return build;
87+
}
88+
89+
public void setBuild(Build build) {
90+
this.build = build;
91+
}
92+
93+
public List<Resource> getResources() {
94+
return resources;
95+
}
96+
97+
public void setResources(List<Resource> resources) {
98+
this.resources = resources;
99+
}
100+
101+
public List<Resource> getTestResources() {
102+
return testResources;
103+
}
104+
105+
public void setTestResources(List<Resource> testResources) {
106+
this.testResources = testResources;
107+
}
108+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<groupId>source</groupId>
24+
<artifactId>maven-source-plugin-test-msources-144</artifactId>
25+
<version>99.0</version>
26+
<packaging>jar</packaging>
27+
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-source-plugin</artifactId>
33+
<configuration>
34+
<project implementation="org.apache.maven.plugins.source.stubs.MSources144Stub"/>
35+
<outputDirectory>${basedir}/target/test/unit/msources-144/target</outputDirectory>
36+
<finalName>maven-source-plugin-test-msources-144-99.0</finalName>
37+
</configuration>
38+
</plugin>
39+
<!-- this is just informational why we would expect both sources to be included -->
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<executions>
44+
<execution>
45+
<id>compile-java9</id>
46+
<phase>compile</phase>
47+
<goals>
48+
<goal>compile</goal>
49+
</goals>
50+
<configuration>
51+
<compileSourceRoots>
52+
<compileSourceroot>${project.basedir}/src/main/java9</compileSourceroot>
53+
</compileSourceRoots>
54+
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/9</outputDirectory>
55+
</configuration>
56+
</execution>
57+
</executions>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
63+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
public class SomeClass() {
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
public class SomeClass {
20+
21+
}

0 commit comments

Comments
 (0)