|
20 | 20 | */ |
21 | 21 |
|
22 | 22 | import java.io.File; |
| 23 | +import java.util.Arrays; |
23 | 24 | import java.util.Collections; |
24 | 25 | import java.util.List; |
25 | 26 | import java.util.Objects; |
| 27 | +import java.util.Set; |
26 | 28 |
|
| 29 | +import javax.inject.Inject; |
| 30 | + |
| 31 | +import org.apache.maven.ProjectDependenciesResolver; |
27 | 32 | import org.apache.maven.artifact.Artifact; |
28 | 33 | import org.apache.maven.artifact.handler.ArtifactHandler; |
29 | 34 | import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; |
30 | | -import org.apache.maven.model.Dependency; |
| 35 | +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
| 36 | +import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
31 | 37 | import org.apache.maven.plugin.MojoExecutionException; |
32 | 38 | import org.apache.maven.plugin.MojoFailureException; |
33 | 39 | import org.apache.maven.plugins.annotations.Component; |
34 | 40 | import org.apache.maven.plugins.annotations.Parameter; |
35 | 41 | import org.apache.maven.plugins.dependency.AbstractDependencyMojo; |
36 | 42 | import org.apache.maven.plugins.dependency.utils.DependencyUtil; |
37 | 43 | import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter; |
38 | | -import org.apache.maven.project.MavenProject; |
39 | 44 | import org.apache.maven.project.ProjectBuildingRequest; |
40 | 45 | import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; |
41 | 46 | import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate; |
@@ -270,33 +275,31 @@ protected Artifact getArtifact( ArtifactItem artifactItem ) |
270 | 275 | private void fillMissingArtifactVersion( ArtifactItem artifact ) |
271 | 276 | throws MojoExecutionException |
272 | 277 | { |
273 | | - MavenProject project = getProject(); |
274 | | - List<Dependency> deps = project.getDependencies(); |
275 | | - List<Dependency> depMngt = project.getDependencyManagement() == null ? Collections.<Dependency>emptyList() |
276 | | - : project.getDependencyManagement().getDependencies(); |
277 | | - |
278 | | - if ( !findDependencyVersion( artifact, deps, false ) |
279 | | - && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, false ) ) |
280 | | - && !findDependencyVersion( artifact, deps, true ) |
281 | | - && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, true ) ) ) |
| 278 | + try |
| 279 | + { |
| 280 | + if ( !this.findDependencyVersion( artifact, false ) && !this.findDependencyVersion( artifact, true ) ) |
| 281 | + { |
| 282 | + throw new MojoExecutionException( "Unable to find artifact version of " + artifact.getGroupId() + ":" |
| 283 | + + artifact.getArtifactId() + " in transitively resolved dependencies." ); |
| 284 | + } |
| 285 | + } |
| 286 | + catch ( final ArtifactResolutionException | ArtifactNotFoundException e ) |
282 | 287 | { |
283 | | - throw new MojoExecutionException( "Unable to find artifact version of " + artifact.getGroupId() + ":" |
284 | | - + artifact.getArtifactId() + " in either dependency list or in project's dependency management." ); |
| 288 | + throw new MojoExecutionException( "Failed to transitively resolve project dependencies.", e ); |
285 | 289 | } |
286 | 290 | } |
287 | 291 |
|
288 | | - /** |
289 | | - * Tries to find missing version from a list of dependencies. If found, the artifact is updated with the correct |
290 | | - * version. |
291 | | - * |
292 | | - * @param artifact representing configured file. |
293 | | - * @param dependencies list of dependencies to search. |
294 | | - * @param looseMatch only look at artifactId and groupId |
295 | | - * @return the found dependency |
296 | | - */ |
297 | | - private boolean findDependencyVersion( ArtifactItem artifact, List<Dependency> dependencies, boolean looseMatch ) |
| 292 | + @Inject |
| 293 | + private ProjectDependenciesResolver resolver; |
| 294 | + |
| 295 | + private boolean findDependencyVersion( final ArtifactItem artifact, boolean looseMatch ) |
| 296 | + throws ArtifactResolutionException, ArtifactNotFoundException |
298 | 297 | { |
299 | | - for ( Dependency dependency : dependencies ) |
| 298 | + final Set<Artifact> resolvedArtifacts = |
| 299 | + this.resolver.resolve( this.getProject(), |
| 300 | + Arrays.asList( "compile", "provided", "runtime", "test", "system", "import" ), |
| 301 | + this.session ); |
| 302 | + for ( final Artifact dependency : resolvedArtifacts ) |
300 | 303 | { |
301 | 304 | if ( Objects.equals( dependency.getArtifactId(), artifact.getArtifactId() ) |
302 | 305 | && Objects.equals( dependency.getGroupId(), artifact.getGroupId() ) |
|
0 commit comments