@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 Artifact makeArtifact(String groupId, String artifactId, String version) { Artifact artifact = artifactFactory.createBuildArtifact(groupId, artifactId, version, "jar"); artifact.setFile( getTestFile( "src/test/repository/" + groupId + "/jars/" + artifactId + "-" + version + ".jar")); return artifact; }
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); } }
// generic method to retrieve all the transitive dependencies private Collection<Artifact> getAllDependencies() throws MojoExecutionException { List<Artifact> artifacts = new ArrayList<Artifact>(); for (Iterator<?> dependencies = project.getDependencies().iterator(); dependencies.hasNext(); ) { Dependency dependency = (Dependency) dependencies.next(); String groupId = dependency.getGroupId(); String artifactId = dependency.getArtifactId(); VersionRange versionRange; try { versionRange = VersionRange.createFromVersionSpec(dependency.getVersion()); } catch (InvalidVersionSpecificationException e) { throw new MojoExecutionException("unable to parse version", e); } String type = dependency.getType(); if (type == null) { type = "jar"; } String classifier = dependency.getClassifier(); boolean optional = dependency.isOptional(); String scope = dependency.getScope(); if (scope == null) { scope = Artifact.SCOPE_COMPILE; } Artifact art = this.artifactFactory.createDependencyArtifact( groupId, artifactId, versionRange, type, classifier, scope, null, optional); if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) { art.setFile(new File(dependency.getSystemPath())); } List<String> exclusions = new ArrayList<String>(); for (Iterator<?> j = dependency.getExclusions().iterator(); j.hasNext(); ) { Exclusion e = (Exclusion) j.next(); exclusions.add(e.getGroupId() + ":" + e.getArtifactId()); } ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions); art.setDependencyFilter(newFilter); artifacts.add(art); } return artifacts; }
private boolean resolveAsModule(Artifact a) { // method is called from different threads, so we have to copy the reference so ensure there is // no race conditions. MavenWorkspaceMap map = myWorkspaceMap; if (map == null) return false; MavenWorkspaceMap.Data resolved = map.findFileAndOriginalId(MavenModelConverter.createMavenId(a)); if (resolved == null) return false; a.setResolved(true); a.setFile(resolved.getFile(a.getType())); a.selectVersion(resolved.originalId.getVersion()); return true; }
// DefaultProjectBuilder public Artifact createDependencyArtifact(Dependency d) { if (d.getVersion() == null) { return null; } VersionRange versionRange; try { versionRange = VersionRange.createFromVersionSpec(d.getVersion()); } catch (InvalidVersionSpecificationException e) { return null; } Artifact artifact = XcreateDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, d.getType(), d.getClassifier(), d.getScope(), d.isOptional()); if (Artifact.SCOPE_SYSTEM.equals(d.getScope()) && d.getSystemPath() != null) { artifact.setFile(new File(d.getSystemPath())); } if (!d.getExclusions().isEmpty()) { List<String> exclusions = new ArrayList<>(); for (Exclusion exclusion : d.getExclusions()) { exclusions.add(exclusion.getGroupId() + ':' + exclusion.getArtifactId()); } artifact.setDependencyFilter(new ExcludesArtifactFilter(exclusions)); } return artifact; }
/** * Returns the list of project artifacts. Also artifacts generated from referenced projects will * be added, but with the <code>resolved</code> property set to true. * * @return list of projects artifacts * @throws MojoExecutionException if unable to parse dependency versions */ private Set getProjectArtifacts() throws MojoExecutionException { // keep it sorted, this should avoid random classpath order in tests Set artifacts = new TreeSet(); for (Iterator dependencies = getProject().getDependencies().iterator(); dependencies.hasNext(); ) { Dependency dependency = (Dependency) dependencies.next(); String groupId = dependency.getGroupId(); String artifactId = dependency.getArtifactId(); VersionRange versionRange; try { versionRange = VersionRange.createFromVersionSpec(dependency.getVersion()); } catch (InvalidVersionSpecificationException e) { throw new MojoExecutionException( Messages.getString( "unabletoparseversion", new Object[] { // $NON-NLS-1$ dependency.getArtifactId(), dependency.getVersion(), dependency.getManagementKey(), e.getMessage() }), e); } String type = dependency.getType(); if (type == null) { type = "jar"; // $NON-NLS-1$ } String classifier = dependency.getClassifier(); boolean optional = dependency.isOptional(); String scope = dependency.getScope(); if (scope == null) { scope = Artifact.SCOPE_COMPILE; } Artifact art = getArtifactFactory() .createDependencyArtifact( groupId, artifactId, versionRange, type, classifier, scope, optional); if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) { art.setFile(new File(dependency.getSystemPath())); } List exclusions = new ArrayList(); for (Iterator j = dependency.getExclusions().iterator(); j.hasNext(); ) { Exclusion e = (Exclusion) j.next(); exclusions.add(e.getGroupId() + ":" + e.getArtifactId()); // $NON-NLS-1$ } ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions); art.setDependencyFilter(newFilter); artifacts.add(art); } return artifacts; }
package org.apache.maven.plugin.failsafe;
private void resolveOld( Artifact artifact, List<ArtifactRepository> remoteRepositories, RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException { if (artifact == null) { return; } if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) { File systemFile = artifact.getFile(); if (systemFile == null) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " has no file attached", artifact); } if (!systemFile.exists()) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " not found in path: " + systemFile, artifact); } if (!systemFile.isFile()) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " is not a file: " + systemFile, artifact); } artifact.setResolved(true); return; } if (!artifact.isResolved()) { ArtifactResult result; try { ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact)); artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories)); // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved // or not LocalRepositoryManager lrm = session.getLocalRepositoryManager(); String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact()); artifact.setFile(new File(lrm.getRepository().getBasedir(), path)); result = repoSystem.resolveArtifact(session, artifactRequest); } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) { if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) { throw new ArtifactNotFoundException( e.getMessage(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(), artifact.getDependencyTrail(), e); } else { throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e); } } artifact.selectVersion(result.getArtifact().getVersion()); artifact.setFile(result.getArtifact().getFile()); artifact.setResolved(true); if (artifact.isSnapshot()) { Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion()); if (matcher.matches()) { Snapshot snapshot = new Snapshot(); snapshot.setTimestamp(matcher.group(2)); try { snapshot.setBuildNumber(Integer.parseInt(matcher.group(3))); artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot)); } catch (NumberFormatException e) { logger.warn( "Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage()); } } } } }