List of usage examples for org.apache.maven.artifact.factory ArtifactFactory createArtifact
Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type);
From source file:org.codehaus.mojo.animal_sniffer.enforcer.Signature.java
License:Open Source License
public org.apache.maven.artifact.Artifact createArtifact(ArtifactFactory factory) { return factory.createArtifact(groupId, artifactId, version, null, "signature"/*don't really care*/ ); }
From source file:org.codehaus.mojo.animal_sniffer.maven.Signature.java
License:Open Source License
public org.apache.maven.artifact.Artifact createArtifact(ArtifactFactory factory) { return factory.createArtifact(groupId, artifactId, version, null, "signature"); }
From source file:org.hardisonbrewing.maven.core.DependencyService.java
License:Open Source License
public static final Artifact createArtifact(Artifact artifact) { ArtifactFactory artifactFactory = getArtifactFactory(); return artifactFactory.createArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope(), artifact.getType()); }
From source file:org.mobicents.maven.plugin.eclipse.ClasspathWriter.java
License:Open Source License
/** * Writes the .classpath file for eclipse. * /*from w w w . j a v a 2 s . c o m*/ * @param projects * the list of projects from which the .classpath will get its * dependencies. * @param repositoryVariableName * the name of the maven repository variable. * @param artifactFactory * the factory for constructing artifacts. * @param artifactResolver * the artifact resolver. * @param localRepository * the local repository instance. * @param artifactMetadataSource * @param classpathArtifactTypes * the artifacts types that are allowed in the classpath file. * @param remoteRepositories * the list of remote repository instances. * @param resolveTransitiveDependencies * whether or not dependencies shall be transitively resolved. * @param merge * anything extra (not auto-generated), that should be "merged" * into the generated .classpath * @param classpathExcludes * @param includeTestsDirectory * @param includeResourcesDirectory * @throws Exception */ public void write(final List projects, final String repositoryVariableName, final ArtifactFactory artifactFactory, final ArtifactResolver artifactResolver, final ArtifactRepository localRepository, final ArtifactMetadataSource artifactMetadataSource, final Set classpathArtifactTypes, final List remoteRepositories, final boolean resolveTransitiveDependencies, final String merge, Set classpathExcludes, boolean includeResourcesDirectory) throws Exception { final String rootDirectory = PathNormalizer.normalizePath(this.project.getBasedir().toString()); final File classpathFile = new File(rootDirectory, ".classpath"); final FileWriter fileWriter = new FileWriter(classpathFile); final XMLWriter writer = new PrettyPrintXMLWriter(fileWriter, "UTF-8", null); writer.startElement("classpath"); final Set projectArtifactIds = new LinkedHashSet(); for (final Iterator iterator = projects.iterator(); iterator.hasNext();) { final MavenProject project = (MavenProject) iterator.next(); final Artifact projectArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging()); projectArtifactIds.add(projectArtifact.getId()); } // - collect the source roots for the root project (if they are any) Set<String> sourceRoots = collectSourceRoots(this.project, rootDirectory, writer, includeResourcesDirectory); final Set allArtifacts = new LinkedHashSet(this.project.createArtifacts(artifactFactory, null, null)); for (final Iterator iterator = projects.iterator(); iterator.hasNext();) { final MavenProject project = (MavenProject) iterator.next(); sourceRoots.addAll(collectSourceRoots(project, rootDirectory, writer, includeResourcesDirectory)); final Set artifacts = project.createArtifacts(artifactFactory, null, null); // - get the direct dependencies for (final Iterator artifactIterator = artifacts.iterator(); artifactIterator.hasNext();) { final Artifact artifact = (Artifact) artifactIterator.next(); // - don't attempt to resolve the artifact if its part of the // project (we // infer this if it has the same id has one of the projects or // is in // the same groupId). if (!projectArtifactIds.contains(artifact.getId()) && !project.getGroupId().equals(artifact.getGroupId())) { artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), localRepository); allArtifacts.add(artifact); } else { allArtifacts.add(artifact); } } } // we have all source roots now, sort and write for (String sourceRoot : sourceRoots) { logger.info("Adding src path " + sourceRoot); this.writeClasspathEntry(writer, "src", sourceRoot); } // - remove the project artifacts for (final Iterator iterator = projects.iterator(); iterator.hasNext();) { final MavenProject project = (MavenProject) iterator.next(); final Artifact projectArtifact = project.getArtifact(); if (projectArtifact != null) { for (final Iterator artifactIterator = allArtifacts.iterator(); artifactIterator.hasNext();) { final Artifact artifact = (Artifact) artifactIterator.next(); final String projectId = projectArtifact.getArtifactId(); final String projectGroupId = projectArtifact.getGroupId(); final String artifactId = artifact.getArtifactId(); final String groupId = artifact.getGroupId(); if (artifactId.equals(projectId) && groupId.equals(projectGroupId)) { artifactIterator.remove(); } } } } // - now we resolve transitively, if we have the flag on if (resolveTransitiveDependencies) { final Artifact rootProjectArtifact = artifactFactory.createArtifact(this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(), null, this.project.getPackaging()); final OrArtifactFilter filter = new OrArtifactFilter(); filter.add(new ScopeArtifactFilter(Artifact.SCOPE_COMPILE)); filter.add(new ScopeArtifactFilter(Artifact.SCOPE_PROVIDED)); filter.add(new ScopeArtifactFilter(Artifact.SCOPE_TEST)); final ArtifactResolutionResult result = artifactResolver.resolveTransitively(allArtifacts, rootProjectArtifact, localRepository, remoteRepositories, artifactMetadataSource, filter); allArtifacts.clear(); allArtifacts.addAll(result.getArtifacts()); } // remove excluded ones for (Iterator i = allArtifacts.iterator(); i.hasNext();) { Artifact artifact = (Artifact) i.next(); if (classpathExcludes != null) { if (classpathExcludes.contains(artifact.getGroupId())) { logger.info("Excluding " + artifact + " from .classpath, groupId is excluded"); i.remove(); } else if (classpathExcludes.contains(artifact.getGroupId() + ":" + artifact.getArtifactId())) { logger.info("Excluding " + artifact + " from .classpath, groupId:artifactId is excluded"); i.remove(); } else if (classpathExcludes.contains( artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion())) { logger.info( "Excluding " + artifact + " from .classpath, groupId:artifactId:version is excluded"); i.remove(); } } } final List allArtifactPaths = new ArrayList(allArtifacts); for (final ListIterator iterator = allArtifactPaths.listIterator(); iterator.hasNext();) { final Artifact artifact = (Artifact) iterator.next(); if (classpathArtifactTypes.contains(artifact.getType())) { File artifactFile = artifact.getFile(); if (artifactFile == null) { artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), localRepository); artifactFile = artifact.getFile(); } if (artifactFile != null) { final String path = StringUtils.replace(PathNormalizer.normalizePath(artifactFile.toString()), PathNormalizer.normalizePath(localRepository.getBasedir()), repositoryVariableName); iterator.set(path); } else { iterator.remove(); } } else { iterator.remove(); } } // - sort the paths Collections.sort(allArtifactPaths); for (final Iterator iterator = allArtifactPaths.iterator(); iterator.hasNext();) { String path = (String) iterator.next(); if (path.startsWith(repositoryVariableName)) { this.writeClasspathEntry(writer, "var", path); } else { if (path.startsWith(rootDirectory)) { path = StringUtils.replace(path, rootDirectory + '/', ""); } this.writeClasspathEntry(writer, "lib", path); } } this.writeClasspathEntry(writer, "con", "org.eclipse.jdt.launching.JRE_CONTAINER"); String outputPath = StringUtils.replace( PathNormalizer.normalizePath(this.project.getBuild().getOutputDirectory()), rootDirectory, ""); if (outputPath.startsWith("/")) { outputPath = outputPath.substring(1, outputPath.length()); } this.writeClasspathEntry(writer, "output", outputPath); if (StringUtils.isNotBlank(merge)) { writer.writeMarkup(merge); } writer.endElement(); logger.info("Classpath file written --> '" + classpathFile + "'"); IOUtil.close(fileWriter); }
From source file:org.semver.enforcer.AbstractEnforcerRule.java
License:Apache License
@Override public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException { final MavenProject project = getMavenProject(helper); if (shouldSkipRuleExecution(project)) { helper.getLog().debug("Skipping non " + AbstractEnforcerRule.JAR_ARTIFACT_TYPE + " or " + BUNDLE_ARTIFACT_TYPE + " artifact."); return;//from w ww .j ava 2 s . c o m } final Artifact previousArtifact; final Artifact currentArtifact = validateArtifact(project.getArtifact()); final Version current = Version.parse(currentArtifact.getVersion()); try { final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}"); final String version; if (this.previousVersion != null) { version = this.previousVersion; helper.getLog().info("Version specified as <" + version + ">"); } else { final ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) helper .getComponent(ArtifactMetadataSource.class); final List<ArtifactVersion> availableVersions = getAvailableReleasedVersions(artifactMetadataSource, project, localRepository); final List<ArtifactVersion> availablePreviousVersions = filterNonPreviousVersions(availableVersions, current); if (availablePreviousVersions.isEmpty()) { helper.getLog() .warn("No previously released version. Backward compatibility check not performed."); return; } version = availablePreviousVersions.iterator().next().toString(); helper.getLog().info("Version deduced as <" + version + "> (among all availables: " + availablePreviousVersions + ")"); } final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, project.getArtifact().getType()); final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class); resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository); validateArtifact(previousArtifact); } catch (Exception e) { helper.getLog().warn("Exception while accessing artifacts; skipping check.", e); return; } final Version previous = Version.parse(previousArtifact.getVersion()); final File previousJar = previousArtifact.getFile(); final File currentJar = currentArtifact.getFile(); compareJars(helper, previous, previousJar, current, currentJar); }
From source file:org.wso2.maven.core.utils.MavenUtils.java
License:Open Source License
/** * Resolve a maven artifact from specified repositories and the system path * //from w ww . j ava 2 s . c om * @param groupId * @param artifactId * @param version * @param type * @param scope * @param file * @param artifactFactory * @param remoteRepositories * @param localRepository * @param resolver * @return resolved artifact * @throws MojoExecutionException */ public static Artifact getResolvedArtifact(String groupId, String artifactId, String version, String type, String scope, File file, ArtifactFactory artifactFactory, List<?> remoteRepositories, ArtifactRepository localRepository, ArtifactResolver resolver) throws MojoExecutionException { Artifact artifact = artifactFactory.createArtifact(groupId, artifactId, version, scope, type); if (null != file) { artifact.setFile(file); } try { resolver.resolve(artifact, remoteRepositories, localRepository); } catch (ArtifactResolutionException | ArtifactNotFoundException e) { throw new MojoExecutionException( "Failed to resolve dependency in system path or specified repositories.", e); } return artifact; }