Skip to content

Commit b8594a9

Browse files
committed
[MASSEMBLY-832] Consider dependency optional attribute when gather dependencies
1 parent 55c1dc7 commit b8594a9

File tree

8 files changed

+216
-2
lines changed

8 files changed

+216
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ under the License.
7272
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
7373

7474
<javaVersion>8</javaVersion>
75-
<mdoVersion>2.2.0</mdoVersion>
75+
<mdoVersion>2.3.0</mdoVersion>
7676
<mavenVersion>3.6.3</mavenVersion>
7777
<slf4jVersion>1.7.5</slf4jVersion>
7878
<mavenFilteringVersion>3.4.0</mavenFilteringVersion>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
<parent>
26+
<groupId>org.apache.maven.plugin.assembly.test</groupId>
27+
<artifactId>it-project-parent</artifactId>
28+
<version>1</version>
29+
</parent>
30+
31+
<groupId>test</groupId>
32+
<artifactId>massembly-832</artifactId>
33+
<packaging>pom</packaging>
34+
<version>1</version>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.postgresql</groupId>
38+
<artifactId>postgresql</artifactId>
39+
<version>42.7.7</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.mysql</groupId>
43+
<artifactId>mysql-connector-j</artifactId>
44+
<version>9.3.0</version>
45+
<optional>true</optional>
46+
</dependency>
47+
</dependencies>
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<artifactId>maven-assembly-plugin</artifactId>
52+
<executions>
53+
<execution>
54+
<id>assembly</id>
55+
<phase>package</phase>
56+
<goals>
57+
<goal>single</goal>
58+
</goals>
59+
<configuration>
60+
<descriptors>
61+
<descriptor>src/main/assembly/bin.xml</descriptor>
62+
</descriptors>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.3.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.3.0
22+
http://maven.apache.org/xsd/assembly-2.3.0.xsd">
23+
<id>bin</id>
24+
<formats>
25+
<format>dir</format>
26+
</formats>
27+
<includeBaseDirectory>false</includeBaseDirectory>
28+
<dependencySets>
29+
<dependencySet>
30+
<unpack>false</unpack>
31+
<outputDirectory></outputDirectory>
32+
<useProjectArtifact>false</useProjectArtifact>
33+
<useOptionalDependencies>false</useOptionalDependencies>
34+
</dependencySet>
35+
</dependencySets>
36+
</assembly>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
20+
def expectedFilenames = [
21+
"checker-qual-3.49.3.jar",
22+
"postgresql-42.7.7.jar"
23+
]
24+
25+
File assemblyBasedir = new File( basedir, "target/massembly-832-1-bin/" )
26+
27+
assert assemblyBasedir.listFiles().length == expectedFilenames.size()
28+
29+
for ( fileName in expectedFilenames )
30+
{
31+
File file = new File( assemblyBasedir, fileName )
32+
assert file.isFile() // exists and is file
33+
}
34+
35+
// defined set vs listed set: same cardinality and all present: OK
36+
37+
return true

src/main/java/org/apache/maven/plugins/assembly/archive/task/AddDependencySetsTask.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,18 @@ Set<Artifact> resolveDependencyArtifacts(final DependencySet dependencySet)
302302

303303
final ArtifactFilter filter = new ArtifactIncludeFilterTransformer().transform(scopeFilter);
304304

305+
final ArtifactFilter optionalFilter =
306+
artifact -> !artifact.isOptional() || dependencySet.isUseOptionalDependencies();
307+
305308
FilterUtils.filterArtifacts(
306309
dependencyArtifacts,
307310
dependencySet.getIncludes(),
308311
dependencySet.getExcludes(),
309312
dependencySet.isUseStrictFiltering(),
310313
dependencySet.isUseTransitiveFiltering(),
311314
LOGGER,
312-
filter);
315+
filter,
316+
optionalFilter);
313317

314318
return dependencyArtifacts;
315319
}

src/main/mdo/assembly-component.mdo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,15 @@
553553
build should be included in this dependency set. Default value is false. (Since 2.2)
554554
]]></description>
555555
</field>
556+
<field>
557+
<name>useOptionalDependencies</name>
558+
<version>2.3.0+</version>
559+
<type>boolean</type>
560+
<defaultValue>true</defaultValue>
561+
<description>
562+
Determines whether optional dependencies should be included in this dependency set.
563+
</description>
564+
</field>
556565
<field>
557566
<name>useTransitiveDependencies</name>
558567
<version>1.1.0+</version>

src/main/mdo/assembly.mdo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,15 @@
671671
build should be included in this dependency set. (Since 2.2-beta-1)
672672
]]></description>
673673
</field>
674+
<field>
675+
<name>useOptionalDependencies</name>
676+
<version>2.3.0+</version>
677+
<type>boolean</type>
678+
<defaultValue>true</defaultValue>
679+
<description>
680+
Determines whether optional dependencies should be included in this dependency set.
681+
</description>
682+
</field>
674683
<field>
675684
<name>useTransitiveDependencies</name>
676685
<version>1.1.0+</version>

src/test/java/org/apache/maven/plugins/assembly/archive/task/AddDependencySetsTaskTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Properties;
2727
import java.util.Set;
2828

29+
import com.google.common.collect.ImmutableSet;
2930
import org.apache.maven.artifact.Artifact;
3031
import org.apache.maven.artifact.handler.ArtifactHandler;
3132
import org.apache.maven.execution.MavenSession;
@@ -387,6 +388,54 @@ public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclu
387388
assertSame(am1, result.iterator().next());
388389
}
389390

391+
@Test
392+
public void testGetDependencyArtifacts_ShouldNotFilterOptionalArtifactsByDefault() throws Exception {
393+
Artifact am1 = mockArtifact(1);
394+
when(am1.isOptional()).thenReturn(true);
395+
Artifact am2 = mockArtifact(2);
396+
Set<Artifact> resolvedArtifacts = resolveDependencyArtifacts(new DependencySet(), ImmutableSet.of(am1, am2));
397+
assertEquals(2, resolvedArtifacts.size());
398+
}
399+
400+
@Test
401+
public void testGetDependencyArtifacts_ShouldFilterOptionalArtifactsExplicitly() throws Exception {
402+
Artifact am1 = mockArtifact(1);
403+
when(am1.isOptional()).thenReturn(true);
404+
Artifact am2 = mockArtifact(2);
405+
DependencySet dependencySet = new DependencySet();
406+
dependencySet.setUseOptionalDependencies(false);
407+
408+
Set<Artifact> resolvedArtifacts = resolveDependencyArtifacts(dependencySet, ImmutableSet.of(am1, am2));
409+
assertEquals(1, resolvedArtifacts.size());
410+
assertSame(am2, resolvedArtifacts.iterator().next());
411+
}
412+
413+
@Test
414+
public void testGetDependencyArtifacts_ShouldNotFilterOptionalArtifactsExplicitly() throws Exception {
415+
Artifact am1 = mockArtifact(1);
416+
when(am1.isOptional()).thenReturn(true);
417+
Artifact am2 = mockArtifact(2);
418+
DependencySet dependencySet = new DependencySet();
419+
dependencySet.setUseOptionalDependencies(true);
420+
421+
Set<Artifact> resolvedArtifacts = resolveDependencyArtifacts(dependencySet, ImmutableSet.of(am1, am2));
422+
assertEquals(2, resolvedArtifacts.size());
423+
}
424+
425+
private static Artifact mockArtifact(int id) {
426+
Artifact a = mock(Artifact.class);
427+
when(a.getId()).thenReturn("group:artifact" + id + ":1.0:jar");
428+
return a;
429+
}
430+
431+
private Set<Artifact> resolveDependencyArtifacts(DependencySet dependencySet, Set<Artifact> artifacts)
432+
throws Exception {
433+
MavenProject project = new MavenProject(new Model());
434+
AddDependencySetsTask task =
435+
new AddDependencySetsTask(Collections.singletonList(dependencySet), artifacts, project, null);
436+
return task.resolveDependencyArtifacts(dependencySet);
437+
}
438+
390439
@Test
391440
public void testGetDependencyArtifacts_ShouldIgnoreTransitivePathFilteringWhenIncludeNotTransitive()
392441
throws Exception {

0 commit comments

Comments
 (0)