/
DependenciesTool.java
242 lines (206 loc) · 8.58 KB
/
DependenciesTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package org.codehaus.mojo.license.api;
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2011 CodeLutin, Codehaus, Tony Chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.logging.Logger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* A tool to deal with dependencies of a project.
*
* @author tchemit dev@tchemit.fr
* @since 1.0
*/
@Component( role = DependenciesTool.class, hint = "default" )
public class DependenciesTool
extends AbstractLogEnabled
{
/**
* Message used when an invalid expression pattern is found.
*/
public static final String INVALID_PATTERN_MESSAGE =
"The pattern specified by expression <%s> seems to be invalid.";
/**
* Project builder.
*/
@Requirement
private MavenProjectBuilder mavenProjectBuilder;
// CHECKSTYLE_OFF: MethodLength
/**
* For a given {@code project}, obtain the universe of its dependencies after applying transitivity and
* filtering rules given in the {@code configuration} object.
*
* Result is given in a map where keys are unique artifact id
*
* @param dependencies the project dependencies
* @param configuration the configuration
* @param localRepository local repository used to resolv dependencies
* @param remoteRepositories remote repositories used to resolv dependencies
* @param cache a optional cache where to keep resolved dependencies
* @return the map of resolved dependencies indexed by their unique id.
* @see MavenProjectDependenciesConfigurator
*/
public SortedMap<String, MavenProject> loadProjectDependencies( ResolvedProjectDependencies artifacts,
MavenProjectDependenciesConfigurator configuration,
ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories,
SortedMap<String, MavenProject> cache )
{
final ArtifactFilters artifactFilters = configuration.getArtifactFilters();
final boolean excludeTransitiveDependencies = configuration.isExcludeTransitiveDependencies();
final Set<Artifact> depArtifacts;
if ( configuration.isIncludeTransitiveDependencies() )
{
// All project dependencies
depArtifacts = artifacts.getAllDependencies();
}
else
{
// Only direct project dependencies
depArtifacts = artifacts.getDirectDependencies();
}
boolean verbose = configuration.isVerbose();
SortedMap<String, MavenProject> result = new TreeMap<>();
Map<String, Artifact> excludeArtifacts = new HashMap<>();
Map<String, Artifact> includeArtifacts = new HashMap<>();
SortedMap<String, MavenProject> localCache = new TreeMap<>();
if ( cache != null )
{
localCache.putAll( cache );
}
final Logger log = getLogger();
for ( Artifact artifact : depArtifacts )
{
excludeArtifacts.put( artifact.getId(), artifact );
if ( DefaultThirdPartyTool.LICENSE_DB_TYPE.equals( artifact.getType() ) )
{
// the special dependencies for license databases don't count.
// Note that this will still see transitive deps of a license db; so using the build helper inside of
// another project to make them will be noisy.
continue;
}
if ( !artifactFilters.isIncluded( artifact ) )
{
if ( verbose )
{
log.debug( "Excluding artifact " + artifact );
}
continue;
}
String id = MojoHelper.getArtifactId( artifact );
if ( verbose )
{
log.info( "detected artifact " + id );
}
MavenProject depMavenProject;
// try to get project from cache
depMavenProject = localCache.get( id );
if ( depMavenProject != null )
{
if ( verbose )
{
log.info( "add dependency [" + id + "] (from cache)" );
}
}
else
{
// build project
try
{
depMavenProject =
mavenProjectBuilder.buildFromRepository( artifact, remoteRepositories, localRepository, true );
depMavenProject.getArtifact().setScope( artifact.getScope() );
// In case maven-metadata.xml has different artifactId, groupId or version.
if ( !depMavenProject.getGroupId().equals( artifact.getGroupId() ) )
{
depMavenProject.setGroupId( artifact.getGroupId() );
depMavenProject.getArtifact().setGroupId( artifact.getGroupId() );
}
if ( !depMavenProject.getArtifactId().equals( artifact.getArtifactId() ) )
{
depMavenProject.setArtifactId( artifact.getArtifactId() );
depMavenProject.getArtifact().setArtifactId( artifact.getArtifactId() );
}
if ( !depMavenProject.getVersion().equals( artifact.getVersion() ) )
{
depMavenProject.setVersion( artifact.getVersion() );
depMavenProject.getArtifact().setVersion( artifact.getVersion() );
}
}
catch ( ProjectBuildingException e )
{
log.warn( "Unable to obtain POM for artifact : " + artifact, e );
continue;
}
if ( verbose )
{
log.info( "add dependency [" + id + "]" );
}
// store it also in cache
localCache.put( id, depMavenProject );
}
// keep the project
result.put( id, depMavenProject );
excludeArtifacts.remove( artifact.getId() );
includeArtifacts.put( artifact.getId(), artifact );
}
// exclude artifacts from the result that contain excluded artifacts in the dependency trail
if ( excludeTransitiveDependencies )
{
for ( Map.Entry<String, Artifact> entry : includeArtifacts.entrySet() )
{
List<String> dependencyTrail = entry.getValue().getDependencyTrail();
boolean remove = false;
for ( int i = 1; i < dependencyTrail.size() - 1; i++ )
{
if ( excludeArtifacts.containsKey( dependencyTrail.get( i ) ) )
{
remove = true;
break;
}
}
if ( remove )
{
result.remove( MojoHelper.getArtifactId( entry.getValue() ) );
}
}
}
if ( cache != null )
{
cache.putAll( result );
}
return result;
}
// CHECKSTYLE_ON: MethodLength
}