/** * Get a resolved Artifact from the coordinates provided * * @return the artifact, which has been resolved. * @throws MojoExecutionException */ protected Artifact getArtifact( String groupId, String artifactId, String version, String type, String classifier) throws MojoExecutionException { Artifact artifact; VersionRange vr; try { vr = VersionRange.createFromVersionSpec(version); } catch (InvalidVersionSpecificationException e) { vr = VersionRange.createFromVersion(version); } if (StringUtils.isEmpty(classifier)) { artifact = factory.createDependencyArtifact( groupId, artifactId, vr, type, null, Artifact.SCOPE_COMPILE); } else { artifact = factory.createDependencyArtifact( groupId, artifactId, vr, type, classifier, Artifact.SCOPE_COMPILE); } try { resolver.resolve(artifact, remoteRepos, local); } catch (ArtifactResolutionException e) { throw new MojoExecutionException("Unable to resolve artifact.", e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException("Unable to find artifact.", e); } return artifact; }
@SuppressWarnings("unchecked") private Set<Artifact> getAllArtifacts() throws MojoExecutionException { Set<Artifact> artifacts = new LinkedHashSet<Artifact>(); for (Dependency dep : (List<Dependency>) project.getDependencies()) { VersionRange versionRange; try { versionRange = VersionRange.createFromVersionSpec(dep.getVersion()); } catch (InvalidVersionSpecificationException e) { throw new MojoExecutionException( String.format( "%1s: unable to parse version '%2s' for dependency '%3s': %4s", dep.getArtifactId(), dep.getVersion(), dep.getManagementKey(), e.getMessage()), e); } String type = dep.getType() == null ? "jar" : dep.getType(); boolean optional = dep.isOptional(); String scope = dep.getScope() == null ? Artifact.SCOPE_COMPILE : dep.getScope(); Artifact artifact = artifactFactory.createDependencyArtifact( dep.getGroupId(), dep.getArtifactId(), versionRange, type, dep.getClassifier(), scope, optional); if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) { artifact.setFile(new File(dep.getSystemPath())); } handleExclusions(artifact, dep); artifacts.add(artifact); } return artifacts; }
private Map<String, Artifact> createManagedVersionMap() throws MojoExecutionException { Map<String, Artifact> map = new HashMap<String, Artifact>(); DependencyManagement dependencyManagement = project.getDependencyManagement(); if (dependencyManagement != null && dependencyManagement.getDependencies() != null) { for (Dependency d : dependencyManagement.getDependencies()) { try { VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion()); Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, d.getType(), d.getClassifier(), d.getScope(), d.isOptional()); handleExclusions(artifact, d); map.put(d.getManagementKey(), artifact); } catch (InvalidVersionSpecificationException e) { throw new MojoExecutionException( String.format( "%1s: unable to parse version '%2s' for dependency '%3s': %4s", project.getId(), d.getVersion(), d.getManagementKey(), e.getMessage()), e); } } } return map; }
private Set<Artifact> processTransientDependencies(Dependency dependency, boolean sharedLibraries) throws MojoExecutionException { try { final Set<Artifact> artifacts = new LinkedHashSet<Artifact>(); final CollectRequest collectRequest = new CollectRequest(); collectRequest.setRoot(dependency); collectRequest.setRepositories(projectRepos); final DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot(); final DependencyRequest dependencyRequest = new DependencyRequest( node, new AndDependencyFilter( new ScopeDependencyFilter( Arrays.asList("compile", "runtime"), Arrays.asList("test")), // Also exclude any optional dependencies new DependencyFilter() { @Override public boolean accept( DependencyNode dependencyNode, List<DependencyNode> dependencyNodes) { return !dependencyNode.getDependency().isOptional(); } })); repoSystem.resolveDependencies(repoSession, dependencyRequest); PreorderNodeListGenerator nlg = new PreorderNodeListGenerator(); node.accept(nlg); final List<Dependency> dependencies = nlg.getDependencies(false); for (Dependency dep : dependencies) { final org.sonatype.aether.artifact.Artifact depAetherArtifact = dep.getArtifact(); if (isNativeLibrary(sharedLibraries, depAetherArtifact.getExtension())) { final Artifact mavenArtifact = artifactFactory.createDependencyArtifact( depAetherArtifact.getGroupId(), depAetherArtifact.getArtifactId(), VersionRange.createFromVersion(depAetherArtifact.getVersion()), depAetherArtifact.getExtension(), depAetherArtifact.getClassifier(), dep.getScope()); mavenArtifact.setFile(depAetherArtifact.getFile()); artifacts.add(mavenArtifact); } } return artifacts; } catch (Exception e) { throw new MojoExecutionException("Error while processing transient dependencies", e); } }
private void addProvider( SurefireBooter surefireBooter, String provider, String version, Artifact filteredArtifact) throws ArtifactNotFoundException, ArtifactResolutionException { Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider, VersionRange.createFromVersion(version), "jar", null, Artifact.SCOPE_TEST); ArtifactResolutionResult result = resolveArtifact(filteredArtifact, providerArtifact); for (Iterator i = result.getArtifacts().iterator(); i.hasNext(); ) { Artifact artifact = (Artifact) i.next(); getLog().debug("Adding to surefire test classpath: " + artifact.getFile().getAbsolutePath()); surefireBooter.addSurefireClassPathUrl(artifact.getFile().getAbsolutePath()); } }
private Map createManagedVersionMap( ArtifactFactory artifactFactory, String projectId, DependencyManagement dependencyManagement) throws MojoExecutionException { Map map; if (dependencyManagement != null && dependencyManagement.getDependencies() != null) { map = new HashMap(); for (Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext(); ) { Dependency d = (Dependency) i.next(); try { VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion()); Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, d.getType(), d.getClassifier(), d.getScope(), d.isOptional()); map.put(d.getManagementKey(), artifact); } catch (InvalidVersionSpecificationException e) { throw new MojoExecutionException( Messages.getString( "unabletoparseversion", new Object[] { // $NON-NLS-1$ projectId, d.getVersion(), d.getManagementKey(), e.getMessage() }), e); } } } else { map = Collections.EMPTY_MAP; } return map; }