List of usage examples for org.apache.maven.artifact Artifact getScope
String getScope();
From source file:at.yawk.mdep.GenerateMojo.java
@Override public void execute() throws MojoExecutionException, MojoFailureException { if (cacheHours > 0) { cacheStore = Environment.createCacheStore(new Logger() { @Override//from ww w . ja v a 2 s.c o m public void info(String msg) { getLog().info(msg); } @Override public void warn(String msg) { getLog().warn(msg); } }, "mdep-maven-plugin"); } ArtifactMatcher includesMatcher; if (includes == null) { includesMatcher = ArtifactMatcher.acceptAll(); } else { includesMatcher = ArtifactMatcher.anyMatch(toAntMatchers(includes)); } ArtifactMatcher excludesMatcher = ArtifactMatcher.anyMatch(toAntMatchers(excludes)); ArtifactMatcher matcher = includesMatcher.and(excludesMatcher.not()); List<Artifact> artifacts = new ArrayList<>(); try { ArtifactFilter subtreeFilter = artifact -> artifact.getScope() == null || artifact.getScope().equals(Artifact.SCOPE_COMPILE) || artifact.getScope().equals(Artifact.SCOPE_RUNTIME); DependencyNode root = dependencyTreeBuilder.buildDependencyTree(project, localRepository, subtreeFilter); root.accept(new DependencyNodeVisitor() { @Override public boolean visit(DependencyNode node) { if (node.getArtifact() != null) { if (!subtreeFilter.include(node.getArtifact())) { return false; } artifacts.add(node.getArtifact()); } return true; } @Override public boolean endVisit(DependencyNode node) { return true; } }); } catch (DependencyTreeBuilderException e) { throw new MojoExecutionException("Failed to build dependency tree", e); } List<Dependency> dependencies = new ArrayList<>(); for (Artifact artifact : artifacts) { if (matcher.matches(artifact)) { dependencies.add(findArtifact(artifact)); } } getLog().info("Saving dependency xml"); DependencySet dependencySet = new DependencySet(); dependencySet.setDependencies(dependencies); if (!outputDirectory.mkdirs()) { throw new MojoExecutionException("Failed to create output directory"); } File outputFile = new File(outputDirectory, "mdep-dependencies.xml"); try { JAXBContext jaxbContext = JAXBContext.newInstance(DependencySet.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(dependencySet, outputFile); } catch (JAXBException e) { throw new MojoExecutionException("Failed to serialize dependency set", e); } Resource resource = new Resource(); resource.setDirectory(outputDirectory.toString()); resource.setFiltering(false); project.addResource(resource); }
From source file:at.yawk.mdep.GenerateMojo.java
private Dependency findArtifact(Artifact artifact) throws MojoExecutionException { // all are scanned, the first is used to store the dependency List<Path> cacheSearchLocations = new ArrayList<>(); List<String> coordinateComponents = Arrays.asList(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope()); // in 1.0, we used only ':' as the separator. This does not work on windows, and the following code fixes // that problem. for (String separator : Arrays.asList(":", "/")) { try {//from w w w . j a va 2 s. c o m cacheSearchLocations.add(cacheStore.resolve(String.join(separator, coordinateComponents))); } catch (InvalidPathException ignored) { } } // check local cache if (cacheHours > 0) { for (Path searchLocation : cacheSearchLocations) { if (Files.exists(searchLocation)) { Instant cacheDeadline = Instant.now().minusSeconds((long) (60 * 60 * cacheHours)); try { if (Files.getLastModifiedTime(searchLocation).toInstant().isAfter(cacheDeadline)) { try (InputStream in = Files.newInputStream(searchLocation)) { Dependency dependency = (Dependency) JAXBContext.newInstance(Dependency.class) .createUnmarshaller().unmarshal(in); getLog().info("Checksum was present in local cache: " + artifact); return dependency; } } } catch (IOException | JAXBException e) { throw new MojoExecutionException("Failed to read local cache", e); } } } } for (ArtifactRepository repository : remoteArtifactRepositories) { // only scan configured repositories if (this.repositories != null && !this.repositories.contains(repository.getId())) { continue; } Dependency dependency = findArtifactInRepository(artifact, repository); if (dependency != null) { if (cacheHours > 0) { try { Path target = cacheSearchLocations.get(0); if (!Files.isDirectory(target.getParent())) { Files.createDirectories(target.getParent()); } try (OutputStream out = Files.newOutputStream(target)) { JAXBContext.newInstance(Dependency.class).createMarshaller().marshal(dependency, out); } } catch (IOException | JAXBException e) { getLog().warn("Could not save dependency to local cache", e); } } return dependency; } } throw new MojoExecutionException("Could not find " + artifact + " in configured repositories"); }
From source file:biz.gabrys.maven.plugin.util.classpath.ContextClassLoaderExtender.java
License:Open Source License
/** * Creates a text representation of the {@link Artifact}. * @param artifact the artifact./* w ww .jav a 2 s . c o m*/ * @return the text representation of the {@link Artifact}. * @since 1.4.0 */ protected String createDisplayText(final Artifact artifact) { return String.format("%s:%s-%s.%s (%s)", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getScope()); }
From source file:cn.wanghaomiao.maven.plugin.seimi.util.WarUtils.java
License:Apache License
/** * @param artifact {@link Artifact}/*from w ww . ja va2s . c o m*/ * @param dependency {@link Dependency} * @return is related or not. */ public static boolean isRelated(Artifact artifact, Dependency dependency) { if (artifact == null || dependency == null) { return false; } if (!StringUtils.equals(artifact.getGroupId(), dependency.getGroupId())) { return false; } if (!StringUtils.equals(artifact.getArtifactId(), dependency.getArtifactId())) { return false; } if (artifact.getVersion() != null ? !artifact.getVersion().equals(dependency.getVersion()) : dependency.getVersion() != null) { return false; } if (artifact.getType() != null ? !artifact.getType().equals(dependency.getType()) : dependency.getType() != null) { return false; } if (artifact.getClassifier() != null ? !artifact.getClassifier().equals(dependency.getClassifier()) : dependency.getClassifier() != null) { return false; } if (artifact.getScope() != null ? !artifact.getScope().equals(dependency.getScope()) : dependency.getScope() != null) { return false; } if (artifact.isOptional() != dependency.isOptional()) { return false; } return true; }
From source file:com.actility.maven.plugin.cocoon.ClassifierTypeTranslator.java
License:Apache License
public Set<Artifact> translate(Set<Artifact> artifacts, Log log) { Set<Artifact> results = artifacts; log.debug("Translating Artifacts using Classifier: " + this.classifier + " and Type: " + this.type); results = new HashSet<Artifact>(); for (Artifact artifact : artifacts) { // this translator must pass both type and classifier here so we // will use the // base artifact value if null comes in String useType = null;/*from w ww . j a v a 2 s .c o m*/ if (StringUtils.isNotEmpty(this.type)) { useType = this.type; } else { useType = artifact.getType(); } String useClassifier = null; if (StringUtils.isNotEmpty(this.classifier)) { useClassifier = this.classifier; } else { useClassifier = artifact.getClassifier(); } // Create a new artifact Artifact newArtifact = factory.createArtifactWithClassifier(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), useType, useClassifier); // note the new artifacts will always have the scope set to null. We // should // reset it here so that it will pass other filters if needed newArtifact.setScope(artifact.getScope()); results.add(newArtifact); } return results; }
From source file:com.actility.maven.plugin.cocoon.DependencyUtil.java
License:Apache License
/** * Formats the outputDirectory based on type. * * @param useSubdirsPerType if a new sub directory should be used for each type. * @param useSubdirPerArtifact if a new sub directory should be used for each artifact. * @param useRepositoryLayout if dependencies must be moved into a Maven repository layout, if set, other settings will be * ignored.//from w w w .j av a2 s.c o m * @param removeVersion if the version must not be mentioned in the filename * @param outputDirectory base outputDirectory. * @param artifact information about the artifact. * @return a formatted File object to use for output. */ public static File getFormattedOutputDirectory(boolean useSubdirsPerScope, boolean useSubdirsPerType, boolean useSubdirPerArtifact, boolean useRepositoryLayout, boolean removeVersion, File outputDirectory, Artifact artifact) { StringBuffer sb = new StringBuffer(128); if (useRepositoryLayout) { // group id sb.append(artifact.getGroupId().replace('.', File.separatorChar)).append(File.separatorChar); // artifact id sb.append(artifact.getArtifactId()).append(File.separatorChar); // version sb.append(artifact.getBaseVersion()).append(File.separatorChar); } else { if (useSubdirsPerScope) { sb.append(artifact.getScope()).append(File.separatorChar); } if (useSubdirsPerType) { sb.append(artifact.getType()).append("s").append(File.separatorChar); } if (useSubdirPerArtifact) { String artifactString = getDependencyId(artifact, removeVersion); sb.append(artifactString).append(File.separatorChar); } } return new File(outputDirectory, sb.toString()); }
From source file:com.adviser.maven.GraphArtifactCollector.java
License:Apache License
private void recurse(ResolutionNode node, Map resolvedArtifacts, Map managedVersions, ArtifactRepository localRepository, List remoteRepositories, ArtifactMetadataSource source, ArtifactFilter filter, List listeners) throws CyclicDependencyException, ArtifactResolutionException, OverConstrainedVersionException { fireEvent(ResolutionListener.TEST_ARTIFACT, listeners, node); // TODO: use as a conflict resolver Object key = node.getKey();/* ww w . j av a 2s .c o m*/ if (managedVersions.containsKey(key)) { Artifact artifact = (Artifact) managedVersions.get(key); fireEvent(ResolutionListener.MANAGE_ARTIFACT, listeners, node, artifact); if (artifact.getVersion() != null) { node.getArtifact().setVersion(artifact.getVersion()); } if (artifact.getScope() != null) { node.getArtifact().setScope(artifact.getScope()); } } List previousNodes = (List) resolvedArtifacts.get(key); if (previousNodes != null) { node = checkPreviousNodes(node, listeners, previousNodes); } else { previousNodes = new ArrayList(); resolvedArtifacts.put(key, previousNodes); } previousNodes.add(node); if (node.isActive()) { fireEvent(ResolutionListener.INCLUDE_ARTIFACT, listeners, node); } // don't pull in the transitive deps of a system-scoped dependency. if (node.isActive() && !Artifact.SCOPE_SYSTEM.equals(node.getArtifact().getScope())) { fireEvent(ResolutionListener.PROCESS_CHILDREN, listeners, node); for (Iterator i = node.getChildrenIterator(); i.hasNext();) { ResolutionNode child = (ResolutionNode) i.next(); // We leave in optional ones, but don't pick up its dependencies if (!child.isResolved() && (!child.getArtifact().isOptional() || child.isChildOfRootNode())) { Artifact artifact = child.getArtifact(); try { if (artifact.getVersion() == null) { // set the recommended version // TODO: maybe its better to just pass the range // through to retrieval and use a transformation? ArtifactVersion version; version = getArtifactVersion(localRepository, remoteRepositories, source, artifact); artifact.selectVersion(version.toString()); fireEvent(ResolutionListener.SELECT_VERSION_FROM_RANGE, listeners, child); } ResolutionGroup rGroup = source.retrieve(artifact, localRepository, remoteRepositories); // TODO might be better to have source.retreive() throw // a specific exception for this situation // and catch here rather than have it return null if (rGroup == null) { // relocated dependency artifact is declared // excluded, no need to add and recurse further continue; } child.addDependencies(rGroup.getArtifacts(), rGroup.getResolutionRepositories(), filter); } catch (CyclicDependencyException e) { // would like to throw this, but we have crappy stuff in // the repo fireEvent(ResolutionListener.OMIT_FOR_CYCLE, listeners, new ResolutionNode(e.getArtifact(), remoteRepositories, child)); } catch (ArtifactMetadataRetrievalException e) { artifact.setDependencyTrail(node.getDependencyTrail()); throw new ArtifactResolutionException( "Unable to get dependency information: " + e.getMessage(), artifact, e); } recurse(child, resolvedArtifacts, managedVersions, localRepository, remoteRepositories, source, filter, listeners); } } fireEvent(ResolutionListener.FINISH_PROCESSING_CHILDREN, listeners, node); } }
From source file:com.adviser.maven.GraphArtifactCollector.java
License:Apache License
private void checkScopeUpdate(ResolutionNode farthest, ResolutionNode nearest, List listeners) { boolean updateScope = false; Artifact farthestArtifact = farthest.getArtifact(); Artifact nearestArtifact = nearest.getArtifact(); if (Artifact.SCOPE_RUNTIME.equals(farthestArtifact.getScope()) && (Artifact.SCOPE_TEST.equals(nearestArtifact.getScope()) || Artifact.SCOPE_PROVIDED.equals(nearestArtifact.getScope()))) { updateScope = true;//w w w. j a v a 2 s . com } if (Artifact.SCOPE_COMPILE.equals(farthestArtifact.getScope()) && !Artifact.SCOPE_COMPILE.equals(nearestArtifact.getScope())) { updateScope = true; } // current POM rules all if (nearest.getDepth() < 2 && updateScope) { updateScope = false; fireEvent(ResolutionListener.UPDATE_SCOPE_CURRENT_POM, listeners, nearest, farthestArtifact); } if (updateScope) { fireEvent(ResolutionListener.UPDATE_SCOPE, listeners, nearest, farthestArtifact); // previously we cloned the artifact, but it is more effecient to // just update the scope // if problems are later discovered that the original object needs // its original scope value, cloning may // again be appropriate nearestArtifact.setScope(farthestArtifact.getScope()); } }
From source file:com.adviser.maven.GraphArtifactCollector.java
License:Apache License
private void fireEvent(int event, List listeners, ResolutionNode node, Artifact replacement, VersionRange newRange) {//from w w w . j a v a 2 s. c o m for (Iterator i = listeners.iterator(); i.hasNext();) { ResolutionListener listener = (ResolutionListener) i.next(); switch (event) { case ResolutionListener.TEST_ARTIFACT: listener.testArtifact(node.getArtifact()); break; case ResolutionListener.PROCESS_CHILDREN: listener.startProcessChildren(node.getArtifact()); break; case ResolutionListener.FINISH_PROCESSING_CHILDREN: listener.endProcessChildren(node.getArtifact()); break; case ResolutionListener.INCLUDE_ARTIFACT: listener.includeArtifact(node.getArtifact()); break; case ResolutionListener.OMIT_FOR_NEARER: String version = node.getArtifact().getVersion(); String replacementVersion = replacement.getVersion(); if (version != null ? !version.equals(replacementVersion) : replacementVersion != null) { listener.omitForNearer(node.getArtifact(), replacement); } break; case ResolutionListener.OMIT_FOR_CYCLE: listener.omitForCycle(node.getArtifact()); break; case ResolutionListener.UPDATE_SCOPE: listener.updateScope(node.getArtifact(), replacement.getScope()); break; case ResolutionListener.UPDATE_SCOPE_CURRENT_POM: listener.updateScopeCurrentPom(node.getArtifact(), replacement.getScope()); break; case ResolutionListener.MANAGE_ARTIFACT: listener.manageArtifact(node.getArtifact(), replacement); break; case ResolutionListener.SELECT_VERSION_FROM_RANGE: listener.selectVersionFromRange(node.getArtifact()); break; case ResolutionListener.RESTRICT_RANGE: if (node.getArtifact().getVersionRange().hasRestrictions() || replacement.getVersionRange().hasRestrictions()) { listener.restrictRange(node.getArtifact(), replacement, newRange); } break; default: throw new IllegalStateException("Unknown event: " + event); } } }
From source file:com.adviser.maven.ResolutionListenerImpl.java
License:Apache License
public void manageArtifact(Artifact artifact, Artifact replacement) { // getLog().debug("manageArtifact: artifact=" + artifact + ", // replacement=" + replacement); Node node = (Node) artifacts.get(artifact.getDependencyConflictId()); if (node != null) { if (replacement.getVersion() != null) { node.getArtifact().setVersion(replacement.getVersion()); }//w w w . jav a 2 s .co m if (replacement.getScope() != null) { node.getArtifact().setScope(replacement.getScope()); } } }