Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
TransitivityFilter |
|
| 2.0;2 |
1 | package org.apache.maven.plugin.dependency.utils.filters; |
|
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 | import java.util.HashSet; |
|
23 | import java.util.Iterator; |
|
24 | import java.util.Set; |
|
25 | ||
26 | import org.apache.maven.artifact.Artifact; |
|
27 | import org.apache.maven.plugin.logging.Log; |
|
28 | ||
29 | /** |
|
30 | * @author <a href="mailto:brianf@apache.org">Brian Fox</a> |
|
31 | * @version $Id: TransitivityFilter.java 552528 2007-07-02 16:12:47Z markh $ |
|
32 | */ |
|
33 | public class TransitivityFilter |
|
34 | extends AbstractArtifactsFilter |
|
35 | { |
|
36 | ||
37 | private boolean excludeTransitive; |
|
38 | ||
39 | private Set directDependencies; |
|
40 | ||
41 | public TransitivityFilter( Set directDependencies, boolean excludeTransitive ) |
|
42 | 104 | { |
43 | 104 | this.excludeTransitive = excludeTransitive; |
44 | 104 | this.directDependencies = directDependencies; |
45 | 104 | } |
46 | ||
47 | public Set filter( Set artifacts, Log log ) |
|
48 | { |
|
49 | // why not just take the directDependencies here? |
|
50 | // because if this filter is run after some other process, the |
|
51 | // set of artifacts may not be the same as the directDependencies. |
|
52 | 103 | Set result = artifacts; |
53 | ||
54 | 103 | if ( excludeTransitive ) |
55 | { |
|
56 | 4 | log.debug( "Excluding Transitive Dependencies." ); |
57 | 4 | result = new HashSet(); |
58 | 4 | Iterator iterator = artifacts.iterator(); |
59 | 32 | while ( iterator.hasNext() ) |
60 | { |
|
61 | 28 | Artifact artifact = (Artifact) iterator.next(); |
62 | 28 | if ( artifactIsADirectDependency( artifact ) ) |
63 | { |
|
64 | 8 | result.add( artifact ); |
65 | 8 | log.debug( "Added: " + artifact ); |
66 | } |
|
67 | 28 | } |
68 | 4 | log.debug( "Added " + result.size() ); |
69 | 4 | } |
70 | else |
|
71 | { |
|
72 | 99 | log.debug( "Including Transitive Dependencies." ); |
73 | } |
|
74 | ||
75 | 103 | return result; |
76 | } |
|
77 | ||
78 | /** |
|
79 | * Compares the artifact to the list of dependencies to see if it is |
|
80 | * directly included by this project |
|
81 | * |
|
82 | * @param artifact |
|
83 | * representing the item to compare. |
|
84 | * @return true if artifact is a direct dependency |
|
85 | */ |
|
86 | public boolean artifactIsADirectDependency( Artifact artifact ) |
|
87 | { |
|
88 | 28 | boolean result = false; |
89 | 28 | Iterator iterator = this.directDependencies.iterator(); |
90 | 72 | while ( iterator.hasNext() ) |
91 | { |
|
92 | 52 | Artifact dependency = (Artifact) iterator.next(); |
93 | 52 | if ( dependency.getGroupId().equals( artifact.getGroupId() ) |
94 | && dependency.getArtifactId().equals( artifact.getArtifactId() ) ) |
|
95 | { |
|
96 | 8 | result = true; |
97 | 8 | break; |
98 | } |
|
99 | 44 | } |
100 | 28 | return result; |
101 | } |
|
102 | ||
103 | /** |
|
104 | * @return Returns the excludeTransitive. |
|
105 | */ |
|
106 | public boolean isExcludeTransitive() |
|
107 | { |
|
108 | 2 | return this.excludeTransitive; |
109 | } |
|
110 | ||
111 | /** |
|
112 | * @param excludeTransitive |
|
113 | * The excludeTransitive to set. |
|
114 | */ |
|
115 | public void setExcludeTransitive( boolean excludeTransitive ) |
|
116 | { |
|
117 | 1 | this.excludeTransitive = excludeTransitive; |
118 | 1 | } |
119 | } |