List of usage examples for org.apache.maven.artifact Artifact getId
String getId();
From source file:br.com.anteros.restdoc.maven.plugin.util.ResourceResolver.java
License:Apache License
@SuppressWarnings("unchecked") private static List<String> resolveAndUnpack(final List<Artifact> artifacts, final SourceResolverConfig config, final List<String> validClassifiers, final boolean propagateErrors) throws ArtifactResolutionException, ArtifactNotFoundException { // NOTE: Since these are '-sources' and '-test-sources' artifacts, they won't actually // resolve transitively...this is just used to aggregate resolution failures into a single // exception. final Set<Artifact> artifactSet = new LinkedHashSet<Artifact>(artifacts); final Artifact pomArtifact = config.project().getArtifact(); final ArtifactRepository localRepo = config.localRepository(); final List<ArtifactRepository> remoteRepos = config.project().getRemoteArtifactRepositories(); final ArtifactMetadataSource metadataSource = config.artifactMetadataSource(); final ArtifactFilter filter = config.filter(); ArtifactFilter resolutionFilter = null; if (filter != null) { // Wrap the filter in a ProjectArtifactFilter in order to always include the pomArtifact for resolution. // NOTE that this is necessary, b/c the -sources artifacts are added dynamically to the pomArtifact // and the resolver also checks the dependency trail with the given filter, thus the pomArtifact has // to be explicitly included by the filter, otherwise the -sources artifacts won't be resolved. resolutionFilter = new ProjectArtifactFilter(pomArtifact, filter); }//from w ww. ja va2 s. c o m final ArtifactResolver resolver = config.artifactResolver(); @SuppressWarnings("rawtypes") Map managed = config.project().getManagedVersionMap(); final ArtifactResolutionResult resolutionResult = resolver.resolveTransitively(artifactSet, pomArtifact, managed, localRepo, remoteRepos, metadataSource, resolutionFilter); final List<String> result = new ArrayList<String>(artifacts.size()); for (final Artifact a : (Collection<Artifact>) resolutionResult.getArtifacts()) { if (!validClassifiers.contains(a.getClassifier()) || (filter != null && !filter.include(a))) { continue; } final File d = new File(config.outputBasedir(), a.getArtifactId() + "-" + a.getVersion() + "-" + a.getClassifier()); if (!d.exists()) { d.mkdirs(); } try { final UnArchiver unArchiver = config.archiverManager().getUnArchiver(a.getType()); unArchiver.setDestDirectory(d); unArchiver.setSourceFile(a.getFile()); unArchiver.extract(); result.add(d.getAbsolutePath()); } catch (final NoSuchArchiverException e) { if (propagateErrors) { throw new ArtifactResolutionException( "Failed to retrieve valid un-archiver component: " + a.getType(), a, e); } } catch (final ArchiverException e) { if (propagateErrors) { throw new ArtifactResolutionException("Failed to unpack: " + a.getId(), a, e); } } } return result; }
From source file:br.com.uggeri.maven.builder.mojo.DependencyResolver.java
public List<Artifact> resolveDependencies(MavenProject project, Artifact artifact) throws MojoExecutionException { final List<Artifact> artifactDependencies = new ArrayList<Artifact>(); Artifact pomArtifact = createArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "pom"); resolveArtifact(project, pomArtifact); if (pomArtifact.isResolved()) { DefaultModelReader dmr = new DefaultModelReader(); try {// w w w . j a v a 2 s . c om Model pomModel = dmr.read(pomArtifact.getFile(), null); for (Dependency dep : pomModel.getDependencies()) { final Artifact depArtifact = createArtifact(dep); if (log != null && log.isDebugEnabled()) { log.debug("Dependencia encontrada para " + artifact.getId() + "."); } resolveArtifact(project, depArtifact); if (depArtifact.isResolved()) { artifactDependencies.add(depArtifact); } } } catch (IOException ex) { throw new MojoExecutionException("Erro ao ler o arquivo POM do artefato " + artifact.getId(), ex); } } return artifactDependencies; }
From source file:com.actility.maven.plugin.cocoon.DefaultArtifactsResolver.java
License:Apache License
public Set<Artifact> resolve(Set<Artifact> artifacts, Log log) throws MojoExecutionException { Set<Artifact> resolvedArtifacts = new HashSet<Artifact>(); for (Artifact artifact : artifacts) { try {/*from w w w .j a va 2 s .c o m*/ resolver.resolve(artifact, remoteRepositories, local); resolvedArtifacts.add(artifact); } catch (ArtifactResolutionException ex) { // an error occurred during resolution, log it an continue log.debug("error resolving: " + artifact.getId()); log.debug(ex); if (stopOnFailure) { throw new MojoExecutionException("error resolving: " + artifact.getId(), ex); } } catch (ArtifactNotFoundException ex) { // not found, log it and continue log.debug("not found in any repository: " + artifact.getId()); if (stopOnFailure) { throw new MojoExecutionException("not found in any repository: " + artifact.getId(), ex); } } } return resolvedArtifacts; }
From source file:com.actility.maven.plugin.cocoon.DependencyStatusSets.java
License:Apache License
public String getOutput(boolean outputAbsoluteArtifactFilename, boolean outputScope) { StringBuffer sb = new StringBuffer(); sb.append("\n"); sb.append("The following files have been resolved:\n"); if (this.resolvedDependencies == null || this.resolvedDependencies.isEmpty()) { sb.append(" none\n"); } else {/*from w w w .j a va 2s . c o m*/ for (Artifact artifact : resolvedDependencies) { String artifactFilename = null; if (outputAbsoluteArtifactFilename) { try { // we want to print the absolute file name here artifactFilename = artifact.getFile().getAbsoluteFile().getPath(); } catch (NullPointerException e) { // ignore the null pointer, we'll output a null string artifactFilename = null; } } String id = outputScope ? artifact.toString() : artifact.getId(); sb.append(" " + id + (outputAbsoluteArtifactFilename ? ":" + artifactFilename : "") + "\n"); } } if (this.skippedDependencies != null && !this.skippedDependencies.isEmpty()) { sb.append("\n"); sb.append("The following files were skipped:\n"); Set<Artifact> skippedDependencies = new LinkedHashSet<Artifact>(); skippedDependencies.addAll(this.skippedDependencies); for (Artifact artifact : skippedDependencies) { sb.append(" " + artifact.getId() + "\n"); } } if (this.unResolvedDependencies != null && !this.unResolvedDependencies.isEmpty()) { sb.append("\n"); sb.append("The following files have NOT been resolved:\n"); Set<Artifact> unResolvedDependencies = new LinkedHashSet<Artifact>(); unResolvedDependencies.addAll(this.unResolvedDependencies); for (Artifact artifact : unResolvedDependencies) { sb.append(" " + artifact.getId() + "\n"); } } sb.append("\n"); return sb.toString(); }
From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.EclipsePlugin.java
License:Apache License
/** * Utility method that locates a project in the workspace for the given artifact. * * @param artifact the artifact a project should produce. * @return <code>true</code> if the artifact is produced by a reactor projectart. *//*from w w w .j a va 2s . co m*/ private boolean isAvailableAsAWorkspaceProject(Artifact artifact) { IdeDependency[] workspaceArtefacts = getWorkspaceArtefacts(); for (int index = 0; workspaceArtefacts != null && index < workspaceArtefacts.length; index++) { IdeDependency workspaceArtefact = workspaceArtefacts[index]; if (workspaceArtefact.getGroupId().equals(artifact.getGroupId()) && workspaceArtefact.getArtifactId().equals(artifact.getArtifactId())) { if (workspaceArtefact.getVersion().equals(artifact.getBaseVersion())) { workspaceArtefact.setAddedToClasspath(true); getLog().debug("Using workspace project: " + workspaceArtefact.getEclipseProjectName()); return true; } else { getLog().info("Artifact " + artifact.getId() + " already available as a workspace project, but with different version. Expected: " + artifact.getBaseVersion() + ", found: " + workspaceArtefact.getVersion()); } } } return false; }
From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.InstallPluginsMojo.java
License:Open Source License
/** * Traverse the list of resolved dependency artifacts. For each one having a type that is listed in the * pluginDependencyTypes parameter value, resolve the associated project metadata (POM), and perform install(..) on * that artifact.//from w ww . j a v a 2 s .c om */ public void execute() throws MojoExecutionException, MojoFailureException { if (eclipseDir == null) { getLog().info("Eclipse directory? "); String eclipseDirString; try { eclipseDirString = inputHandler.readLine(); } catch (IOException e) { throw new MojoExecutionException("Unable to read from standard input", e); } eclipseDir = new File(eclipseDirString); } if (eclipseDir.exists() && !eclipseDir.isDirectory()) { throw new MojoFailureException("Invalid Eclipse directory: " + eclipseDir); } else if (!eclipseDir.exists()) { eclipseDir.mkdirs(); } for (Iterator it = artifacts.iterator(); it.hasNext();) { Artifact artifact = (Artifact) it.next(); if (pluginDependencyTypes.indexOf(artifact.getType()) > -1) { getLog().debug("Processing Eclipse plugin dependency: " + artifact.getId()); MavenProject project; try { project = projectBuilder.buildFromRepository(artifact, Collections.EMPTY_LIST, localRepository, true); } catch (ProjectBuildingException e) { throw new MojoExecutionException( "Failed to load project metadata (POM) for: " + artifact.getId(), e); } install(artifact, project); } else { getLog().debug("Skipping dependency: " + artifact.getId() + ". Set pluginDependencyTypes with a comma-separated list of types to change this."); } } }
From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.InstallPluginsMojo.java
License:Open Source License
/** * <p>// w w w .jav a 2s . c om * Install the plugin into the eclipse instance's /plugins directory * </p> * <ol> * <li>Determine whether the plugin should be extracted into a directory or not</li> * <li>If the plugin's target location exists, or overwrite is set to true: * <ol type="a"> * <li>if extract, ensure the plugin target location exists (mkdirs), and extract there.</li> * <li>copy the plugin file from the local repository to the target location</li> * </ol> * <p> * Warn whenever a plugin will overwrite an existing file or directory, and emit an INFO message whenever a plugin * installation is skipped because of an existing file and overwrite == false. * </p> * * @param artifact The plugin dependency as it has been resolved. * @param project The project metadata for the accompanying plugin-dependency artifact, used to determine whether to * install as a jar or as a directory * @throws MojoExecutionException In the event the plugin should be extracted but cannot, or the file copy fails (in * the event it should not be extracted) * @throws MojoFailureException In the event that the plugins target directory (inside the Eclipse instance * directory) does not exist, or is not a directory. */ private void install(Artifact artifact, MavenProject project) throws MojoExecutionException, MojoFailureException { if (pluginsDir == null) { pluginsDir = new File(eclipseDir, "plugins"); } if (!pluginsDir.exists() || !pluginsDir.isDirectory()) { throw new MojoFailureException("Invalid Eclipse directory: " + eclipseDir + " (plugins directory is missing or not a directory)."); } boolean installAsJar = true; Properties properties = project.getProperties(); if (properties != null) { installAsJar = !Boolean.valueOf(properties.getProperty(PROP_UNPACK_PLUGIN, "false")).booleanValue(); } Attributes attributes = null; try { // don't verify, plugins zipped by eclipse:make-artifacts could have a bad signature JarFile jar = new JarFile(artifact.getFile(), false); Manifest manifest = jar.getManifest(); if (manifest == null) { getLog().debug("Ignoring " + artifact.getArtifactId() + " as it is does not have a Manifest (and so is not an OSGi bundle)"); return; } attributes = manifest.getMainAttributes(); } catch (IOException e) { throw new MojoExecutionException( "Unable to read manifest of plugin " + artifact.getFile().getAbsolutePath(), e); } String pluginName = formatEclipsePluginName(artifact); File pluginFile = new File(pluginsDir, pluginName + ".jar"); File pluginDir = new File(pluginsDir, pluginName); boolean skipped = true; /* check if artifact is an OSGi bundle and ignore if not */ Object bundleName = attributes.getValue("Bundle-Name"); Object bundleSymbolicName = attributes.getValue("Bundle-SymbolicName"); if (bundleSymbolicName == null && bundleName == null) { getLog().debug("Ignoring " + artifact.getArtifactId() + " as it is not an OSGi bundle (no Bundle-SymbolicName or Bundle-Name in manifest)"); return; } if (overwrite) { if (pluginFile.exists() || pluginDir.exists()) { getLog().warn("Overwriting old plugin with contents of: " + artifact.getId()); getLog().debug("Removing old plugin from both: " + pluginFile + " and: " + pluginDir); try { FileUtils.forceDelete(pluginDir); FileUtils.forceDelete(pluginFile); } catch (IOException e) { throw new MojoExecutionException( "Failed to remove old plugin from: " + pluginFile + " or: " + pluginDir, e); } getLog().debug("Removal of old plugin is complete; proceeding with plugin installation."); } performFileOperations(installAsJar, artifact, pluginFile, pluginDir); skipped = false; } else if (installAsJar && !pluginFile.exists()) { performFileOperations(installAsJar, artifact, pluginFile, pluginDir); skipped = false; } else if (!installAsJar && !pluginDir.exists()) { performFileOperations(installAsJar, artifact, pluginFile, pluginDir); skipped = false; } if (skipped) { if (installAsJar) { getLog().info("Skipping plugin installation for: " + artifact.getId() + "; file: " + pluginFile + " already exists. Set overwrite = true to override this."); } else if (!installAsJar) { getLog().info("Skipping plugin installation for: " + artifact.getId() + "; directory: " + pluginDir + " already exists. Set overwrite = true to override this."); } } }
From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.InstallPluginsMojo.java
License:Open Source License
private void performFileOperations(boolean installAsJar, Artifact artifact, File pluginFile, File pluginDir) throws MojoExecutionException { File artifactFile = artifact.getFile(); if (installAsJar) { try {/*from w w w. jav a 2s . c o m*/ getLog().debug("Copying: " + artifact.getId() + " to: " + pluginFile); FileUtils.copyFile(artifactFile, pluginFile); } catch (IOException e) { throw new MojoExecutionException("Failed to copy Eclipse plugin: " + artifact.getId() + "\nfrom: " + artifact.getFile() + "\nto: " + pluginFile, e); } } else { try { getLog().debug("Expanding: " + artifact.getId() + " into: " + pluginDir); pluginDir.mkdirs(); UnArchiver unarchiver = archiverManager.getUnArchiver(artifactFile); unarchiver.setSourceFile(artifactFile); unarchiver.setDestDirectory(pluginDir); unarchiver.extract(); } catch (NoSuchArchiverException e) { throw new MojoExecutionException("Could not find unarchiver for: " + artifactFile, e); } catch (ArchiverException e) { throw new MojoExecutionException("Could not extract: " + artifactFile, e); } catch (IOException e) { throw new MojoExecutionException("Could not extract: " + artifactFile, e); } } }
From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.RadPlugin.java
License:Open Source License
/** * Utility method that locates a project producing the given artifact. * * @param artifact the artifact a project should produce. * @return <code>true</code> if the artifact is produced by a reactor projectart. *//*from w ww. j a va2 s . c o m*/ protected boolean isAvailableAsAReactorProject(Artifact artifact) { if (this.reactorProjects != null && (Constants.PROJECT_PACKAGING_JAR.equals(artifact.getType()) || Constants.PROJECT_PACKAGING_EJB.equals(artifact.getType()) || Constants.PROJECT_PACKAGING_WAR.equals(artifact.getType()))) { for (Iterator iter = this.reactorProjects.iterator(); iter.hasNext();) { MavenProject reactorProject = (MavenProject) iter.next(); if (reactorProject.getGroupId().equals(artifact.getGroupId()) && reactorProject.getArtifactId().equals(artifact.getArtifactId())) { if (reactorProject.getVersion().equals(artifact.getVersion())) { return true; } else { getLog().info("Artifact " + artifact.getId() + " already available as a reactor project, but with different version. Expected: " + artifact.getVersion() + ", found: " + reactorProject.getVersion()); } } } } return false; }
From source file:com.alibaba.citrus.maven.eclipse.base.ide.AbstractIdeSupportMojo.java
License:Apache License
/** * Checks the list of reactor projects to see if the artifact is included. * * @param artifact the artifact to check if it is in the reactor * @return the reactor project or null if it is not in the reactor *//*from w w w .ja v a 2 s. c o m*/ protected MavenProject getReactorProject(Artifact artifact) { if (reactorProjects != null) { for (Iterator iter = reactorProjects.iterator(); iter.hasNext();) { MavenProject reactorProject = (MavenProject) iter.next(); if (reactorProject.getGroupId().equals(artifact.getGroupId()) && reactorProject.getArtifactId().equals(artifact.getArtifactId())) { if (reactorProject.getVersion().equals(artifact.getVersion())) { return reactorProject; } else { getLog().info("Artifact " + artifact.getId() + " already available as a reactor project, but with different version. Expected: " + artifact.getVersion() + ", found: " + reactorProject.getVersion()); } } } } return null; }