List of usage examples for org.apache.maven.artifact Artifact getGroupId
String getGroupId();
From source file:at.yawk.mdep.AntArtifactMatcher.java
@Override public boolean matches(Artifact artifact) { List<String> parts = Arrays.asList(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier());/*from ww w . java 2s .c om*/ for (int i = 0; i < this.patterns.size(); i++) { if (i > parts.size()) { // too specific return false; } if (!patternMatches(patterns.get(i), parts.get(i))) { return false; } } return true; }
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 v a2 s . c om*/ 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:at.yawk.mdep.GenerateMojo.java
private static String getArtifactPath(Artifact artifact, String version) { StringBuilder builder = new StringBuilder().append(artifact.getGroupId().replace('.', '/')).append('/') .append(artifact.getArtifactId()).append('/').append(artifact.getBaseVersion()).append('/') .append(artifact.getArtifactId()).append('-').append(version); if (artifact.getArtifactHandler().getClassifier() != null) { builder.append('-').append(artifact.getArtifactHandler().getClassifier()); }//from w w w . j a va2s . c om String extension = artifact.getArtifactHandler().getExtension(); if (extension == null) { extension = "jar"; } return builder.append('.').append(extension).toString(); }
From source file:au.net.coldeq.enforcer.BanDuplicateClasses.java
License:Apache License
private void assertThatAllDuplicatesAreOfClassesThatMatchToTheByte(Map<String, Set<Artifact>> duplicates) throws EnforcerRuleException { int errorCount = 0; for (Map.Entry<String, Set<Artifact>> entry : duplicates.entrySet()) { String className = entry.getKey(); Iterator<Artifact> iter = entry.getValue().iterator(); Artifact artifactA = iter.next(); byte[] artifactABytes = readBytesForClassFromArtifact(className, artifactA); while (iter.hasNext()) { Artifact artifactB = iter.next(); byte[] artifactBBytes = readBytesForClassFromArtifact(className, artifactB); if (areByteArraysDifferent(artifactABytes, artifactBBytes)) { errorCount++;//from w ww . ja v a 2 s . co m log.error(String.format("Multiple differing copies of %s found in %s:%s:%s and %s:%s:%s", StringUtils.replace(className.substring(0, className.indexOf(".class")), "/", "."), artifactA.getGroupId(), artifactA.getArtifactId(), artifactA.getVersion(), artifactB.getGroupId(), artifactB.getArtifactId(), artifactB.getVersion())); break; } } } if (errorCount > 0) { throw new EnforcerRuleException( "Duplicate classes found on classpath. " + errorCount + " instances detected."); } }
From source file:au.net.coldeq.enforcer.BanDuplicateClasses.java
License:Apache License
private byte[] readBytesForClassFromArtifact(String className, Artifact artifact) throws EnforcerRuleException { try {/* w w w .j av a2 s.com*/ JarFile jar = new JarFile(artifact.getFile()); try { for (JarEntry entry : Collections.<JarEntry>list(jar.entries())) { if (className.equals(entry.getName())) { return convertInputStreamToByteArray(jar.getInputStream(entry)); } } throw new RuntimeException(String.format("Expected to find %s in artifact: %s:%s:%s", className, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion())); } finally { try { jar.close(); } catch (IOException e) { } } } catch (IOException e) { throw new EnforcerRuleException("Unable to process dependency " + artifact.getFile().getAbsolutePath() + " due to " + e.getMessage(), e); } }
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./*from w ww . j a va2 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:br.com.anteros.restdoc.maven.plugin.AnterosRestDocMojo.java
License:Apache License
/** * Returns a ArtifactFilter that only includes direct dependencies of this * project (verified via groupId and artifactId). * * @return/* w w w . j a v a 2 s .co m*/ */ private ArtifactFilter createDependencyArtifactFilter() { Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts(); List<String> artifactPatterns = new ArrayList<String>(dependencyArtifacts.size()); for (Artifact artifact : dependencyArtifacts) { artifactPatterns.add(artifact.getGroupId() + ":" + artifact.getArtifactId()); } return new IncludesArtifactFilter(artifactPatterns); }
From source file:br.com.anteros.restdoc.maven.plugin.util.ResourceResolver.java
License:Apache License
@SuppressWarnings("unchecked") public static List<JavadocBundle> resolveDependencyJavadocBundles(final SourceResolverConfig config) throws IOException { final List<JavadocBundle> bundles = new ArrayList<JavadocBundle>(); final Map<String, MavenProject> projectMap = new HashMap<String, MavenProject>(); if (config.reactorProjects() != null) { for (final MavenProject p : config.reactorProjects()) { projectMap.put(key(p.getGroupId(), p.getArtifactId()), p); }//from ww w . j a va2 s . c o m } final List<Artifact> artifacts = config.project().getTestArtifacts(); final List<Artifact> forResourceResolution = new ArrayList<Artifact>(artifacts.size()); for (final Artifact artifact : artifacts) { final String key = key(artifact.getGroupId(), artifact.getArtifactId()); final MavenProject p = projectMap.get(key); if (p != null) { bundles.addAll(resolveBundleFromProject(config, p, artifact)); } else { forResourceResolution.add(artifact); } } bundles.addAll(resolveBundlesFromArtifacts(config, forResourceResolution)); return bundles; }
From source file:br.com.anteros.restdoc.maven.plugin.util.ResourceResolver.java
License:Apache License
@SuppressWarnings("unchecked") public static List<String> resolveDependencySourcePaths(final SourceResolverConfig config) throws ArtifactResolutionException, ArtifactNotFoundException { final List<String> dirs = new ArrayList<String>(); final Map<String, MavenProject> projectMap = new HashMap<String, MavenProject>(); if (config.reactorProjects() != null) { for (final MavenProject p : config.reactorProjects()) { projectMap.put(key(p.getGroupId(), p.getArtifactId()), p); }//from w ww . j a v a 2 s . c o m } final List<Artifact> artifacts = config.project().getTestArtifacts(); final List<Artifact> forResourceResolution = new ArrayList<Artifact>(artifacts.size()); for (final Artifact artifact : artifacts) { final String key = key(artifact.getGroupId(), artifact.getArtifactId()); final MavenProject p = projectMap.get(key); if (p != null) { dirs.addAll(resolveFromProject(config, p, artifact)); } else { forResourceResolution.add(artifact); } } dirs.addAll(resolveFromArtifacts(config, forResourceResolution)); return dirs; }
From source file:br.com.anteros.restdoc.maven.plugin.util.ResourceResolver.java
License:Apache License
private static Artifact createResourceArtifact(final Artifact artifact, final String classifier, final SourceResolverConfig config) { final DefaultArtifact a = (DefaultArtifact) config.artifactFactory().createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "jar", classifier); a.setRepository(artifact.getRepository()); return a;// w w w .j a v a 2 s .c o m }