Skip to content

Commit 2b3d6cd

Browse files
committed
[MASSEMBLY-832] Consider dependency optional attribute when gather dependencies
1 parent 60c81ed commit 2b3d6cd

File tree

4 files changed

+106
-24
lines changed

4 files changed

+106
-24
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,19 @@ Set<Artifact> resolveDependencyArtifacts( final DependencySet dependencySet )
340340
final ScopeFilter scopeFilter = FilterUtils.newScopeFilter( dependencySet.getScope() );
341341

342342
final ArtifactFilter filter = new ArtifactIncludeFilterTransformer().transform( scopeFilter );
343+
344+
final ArtifactFilter optionalFilter = new ArtifactFilter()
345+
{
346+
@Override
347+
public boolean include( Artifact artifact )
348+
{
349+
return artifact.isOptional() ? dependencySet.isUseOptionalDependencies() : true;
350+
}
351+
};
343352

344353
FilterUtils.filterArtifacts( dependencyArtifacts, dependencySet.getIncludes(), dependencySet.getExcludes(),
345354
dependencySet.isUseStrictFiltering(), dependencySet.isUseTransitiveFiltering(),
346-
logger, filter );
355+
logger, filter, optionalFilter );
347356

348357
return dependencyArtifacts;
349358
}

src/main/mdo/assembly-component.mdo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,15 @@
568568
build should be included in this dependency set. Default value is false. (Since 2.2)
569569
]]></description>
570570
</field>
571+
<field>
572+
<name>useOptionalDependencies</name>
573+
<version>2.1.0+</version>
574+
<type>boolean</type>
575+
<defaultValue>false</defaultValue>
576+
<description>
577+
Determines whether optional dependencies should be included in this dependency set.
578+
</description>
579+
</field>
571580
<field>
572581
<name>useTransitiveDependencies</name>
573582
<version>1.1.0+</version>

src/main/mdo/assembly.mdo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,15 @@
685685
build should be included in this dependency set. (Since 2.2-beta-1)
686686
]]></description>
687687
</field>
688+
<field>
689+
<name>useOptionalDependencies</name>
690+
<version>2.1.0+</version>
691+
<type>boolean</type>
692+
<defaultValue>false</defaultValue>
693+
<description>
694+
Determines whether optional dependencies should be included in this dependency set.
695+
</description>
696+
</field>
688697
<field>
689698
<name>useTransitiveDependencies</name>
690699
<version>1.1.0+</version>

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

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,7 @@
1919
* under the License.
2020
*/
2121

22-
import static org.hamcrest.MatcherAssert.assertThat;
23-
import static org.hamcrest.Matchers.is;
24-
import static org.junit.Assert.assertEquals;
25-
import static org.junit.Assert.assertNotNull;
26-
import static org.junit.Assert.assertSame;
27-
import static org.mockito.ArgumentMatchers.any;
28-
import static org.mockito.ArgumentMatchers.eq;
29-
import static org.mockito.ArgumentMatchers.isNull;
30-
import static org.mockito.Mockito.atLeastOnce;
31-
import static org.mockito.Mockito.mock;
32-
import static org.mockito.Mockito.times;
33-
import static org.mockito.Mockito.verify;
34-
import static org.mockito.Mockito.when;
35-
36-
import java.io.File;
37-
import java.io.IOException;
38-
import java.nio.charset.Charset;
39-
import java.util.Arrays;
40-
import java.util.Collections;
41-
import java.util.HashSet;
42-
import java.util.Properties;
43-
import java.util.Set;
44-
22+
import com.google.common.collect.ImmutableSet;
4523
import org.apache.maven.artifact.Artifact;
4624
import org.apache.maven.artifact.handler.ArtifactHandler;
4725
import org.apache.maven.execution.MavenSession;
@@ -71,6 +49,29 @@
7149
import org.mockito.ArgumentCaptor;
7250
import org.mockito.junit.MockitoJUnitRunner;
7351

52+
import java.io.File;
53+
import java.io.IOException;
54+
import java.nio.charset.Charset;
55+
import java.util.Arrays;
56+
import java.util.Collections;
57+
import java.util.HashSet;
58+
import java.util.Properties;
59+
import java.util.Set;
60+
61+
import static org.hamcrest.MatcherAssert.assertThat;
62+
import static org.hamcrest.Matchers.is;
63+
import static org.junit.Assert.assertEquals;
64+
import static org.junit.Assert.assertNotNull;
65+
import static org.junit.Assert.assertSame;
66+
import static org.mockito.ArgumentMatchers.any;
67+
import static org.mockito.ArgumentMatchers.eq;
68+
import static org.mockito.ArgumentMatchers.isNull;
69+
import static org.mockito.Mockito.atLeastOnce;
70+
import static org.mockito.Mockito.mock;
71+
import static org.mockito.Mockito.times;
72+
import static org.mockito.Mockito.verify;
73+
import static org.mockito.Mockito.when;
74+
7475
@RunWith( MockitoJUnitRunner.class )
7576
public class AddDependencySetsTaskTest
7677
{
@@ -425,6 +426,60 @@ public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclu
425426
assertSame( am1, result.iterator().next() );
426427
}
427428

429+
@Test
430+
public void testGetDependencyArtifacts_ShouldFilterOptionalArtifactsByDefault() throws Exception
431+
{
432+
Artifact am1 = mockArtifact(1);
433+
when(am1.isOptional()).thenReturn(true);
434+
Artifact am2 = mockArtifact(2);
435+
Set<Artifact> resolvedArtifacts = resolveDependencyArtifacts(new DependencySet(), ImmutableSet.of(am1, am2));
436+
assertEquals(1, resolvedArtifacts.size());
437+
assertSame(am2, resolvedArtifacts.iterator().next());
438+
}
439+
440+
@Test
441+
public void testGetDependencyArtifacts_ShouldFilterOptionalArtifactsExplicitly() throws Exception
442+
{
443+
Artifact am1 = mockArtifact(1);
444+
when(am1.isOptional()).thenReturn(true);
445+
Artifact am2 = mockArtifact(2);
446+
DependencySet dependencySet = new DependencySet();
447+
dependencySet.setUseOptionalDependencies(false);
448+
449+
Set<Artifact> resolvedArtifacts = resolveDependencyArtifacts(dependencySet, ImmutableSet.of(am1, am2));
450+
assertEquals(1, resolvedArtifacts.size());
451+
assertSame(am2, resolvedArtifacts.iterator().next());
452+
}
453+
454+
@Test
455+
public void testGetDependencyArtifacts_ShouldNotFilterOptionalArtifactsExplicitly() throws Exception
456+
{
457+
Artifact am1 = mockArtifact(1);
458+
when(am1.isOptional()).thenReturn(true);
459+
Artifact am2 = mockArtifact(2);
460+
DependencySet dependencySet = new DependencySet();
461+
dependencySet.setUseOptionalDependencies(true);
462+
463+
Set<Artifact> resolvedArtifacts = resolveDependencyArtifacts(dependencySet, ImmutableSet.of(am1, am2));
464+
assertEquals(2, resolvedArtifacts.size());
465+
}
466+
467+
private static Artifact mockArtifact( int id )
468+
{
469+
Artifact a = mock(Artifact.class);
470+
when(a.getId()).thenReturn("group:artifact" + id + ":1.0:jar");
471+
return a;
472+
}
473+
474+
private Set<Artifact> resolveDependencyArtifacts( DependencySet dependencySet, Set<Artifact> artifacts ) throws Exception
475+
{
476+
MavenProject project = new MavenProject(new Model());
477+
Logger logger = new ConsoleLogger(Logger.LEVEL_DEBUG, "test");
478+
AddDependencySetsTask task =
479+
new AddDependencySetsTask(Collections.singletonList(dependencySet), artifacts, project, null, logger);
480+
return task.resolveDependencyArtifacts(dependencySet);
481+
}
482+
428483
@Test
429484
public void testGetDependencyArtifacts_ShouldIgnoreTransitivePathFilteringWhenIncludeNotTransitive()
430485
throws Exception

0 commit comments

Comments
 (0)