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; }
@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 List<File> getFrameworkExtensions() throws MojoExecutionException { List<File> files = new ArrayList<File>(); if (frameworkExtensions != null) { for (Dependency frameworkExtension : frameworkExtensions) { Artifact artifact = repositorySystem.createDependencyArtifact(frameworkExtension); ArtifactResolutionRequest request = new ArtifactResolutionRequest(); request.setArtifact(artifact); request.setResolveRoot(true).setResolveTransitively(false); request.setLocalRepository(session.getLocalRepository()); request.setRemoteRepositories(project.getPluginArtifactRepositories()); request.setOffline(session.isOffline()); request.setForceUpdate(session.getRequest().isUpdateSnapshots()); ArtifactResolutionResult result = repositorySystem.resolve(request); try { resolutionErrorHandler.throwErrors(request, result); } catch (ArtifactResolutionException e) { throw new MojoExecutionException( "Failed to resolve framework extension " + frameworkExtension.getManagementKey(), e); } files.add(artifact.getFile()); } } return files; }
private Set<String> findDuplicateDependencies(List<Dependency> modelDependencies) { List<String> modelDependencies2 = new ArrayList<String>(); for (Dependency dep : modelDependencies) { modelDependencies2.add(dep.getManagementKey()); } return new HashSet<String>( CollectionUtils.disjunction(modelDependencies2, new HashSet<String>(modelDependencies2))); }
private static List mergeDependencyList(List child, List parent) { Map depsMap = new LinkedHashMap(); if (parent != null) { for (Iterator it = parent.iterator(); it.hasNext(); ) { Dependency dependency = (Dependency) it.next(); depsMap.put(dependency.getManagementKey(), dependency); } } if (child != null) { for (Iterator it = child.iterator(); it.hasNext(); ) { Dependency dependency = (Dependency) it.next(); depsMap.put(dependency.getManagementKey(), dependency); } } return new ArrayList(depsMap.values()); }
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; }
static InputLocation findLocationForManagedArtifact( final ManagedArtifactRegion region, MavenProject mavprj) { Model mdl = mavprj.getModel(); InputLocation openLocation = null; if (region.isDependency) { DependencyManagement dm = mdl.getDependencyManagement(); if (dm != null) { List<Dependency> list = dm.getDependencies(); String id = region.groupId + ":" + region.artifactId + ":"; // $NON-NLS-1$ //$NON-NLS-2$ if (list != null) { for (Dependency dep : list) { if (dep.getManagementKey().startsWith(id)) { InputLocation location = dep.getLocation(ARTIFACT_ID); // $NON-NLS-1$ // when would this be null? if (location != null) { openLocation = location; break; } } } } } } if (region.isPlugin) { Build build = mdl.getBuild(); if (build != null) { PluginManagement pm = build.getPluginManagement(); if (pm != null) { List<Plugin> list = pm.getPlugins(); String id = Plugin.constructKey(region.groupId, region.artifactId); if (list != null) { for (Plugin plg : list) { if (id.equals(plg.getKey())) { InputLocation location = plg.getLocation(ARTIFACT_ID); // $NON-NLS-1$ // when would this be null? if (location != null) { openLocation = location; break; } } } } } } } return openLocation; }
/** * @param dependency is the {@link Dependency} to {@link Map#put(Object, Object) put} or {@link * Map#get(Object) get} . * @return the {@link java.util.Map.Entry#getKey() key} for the {@link Dependency}. */ protected String getKey(Dependency dependency) { return dependency.getManagementKey() + ":" + dependency.getClassifier(); }
/** * 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; }