List of usage examples for org.apache.maven.project MavenProject getRemoteArtifactRepositories
public List<ArtifactRepository> getRemoteArtifactRepositories()
From source file:org.apache.sling.maven.slingstart.ModelPreprocessor.java
License:Apache License
private File resolveSlingstartArtifact(final Environment env, final MavenProject project, final Dependency d) throws MavenExecutionException { final Artifact prjArtifact = new DefaultArtifact(d.getGroupId(), d.getArtifactId(), VersionRange.createFromVersion(d.getVersion()), Artifact.SCOPE_PROVIDED, d.getType(), d.getClassifier(), env.artifactHandlerManager.getArtifactHandler(d.getType())); try {/* w w w. j av a2 s.co m*/ env.resolver.resolve(prjArtifact, project.getRemoteArtifactRepositories(), env.session.getLocalRepository()); } catch (final ArtifactResolutionException e) { throw new MavenExecutionException("Unable to get artifact for " + d, e); } catch (final ArtifactNotFoundException e) { throw new MavenExecutionException("Unable to get artifact for " + d, e); } return prjArtifact.getFile(); }
From source file:org.apache.sling.maven.slingstart.ModelUtils.java
License:Apache License
/** * Get a resolved Artifact from the coordinates provided * * @return the artifact, which has been resolved. * @throws MojoExecutionException/*from www.j a v a 2 s . c o m*/ */ public static Artifact getArtifact(final MavenProject project, final MavenSession session, final ArtifactHandlerManager artifactHandlerManager, final ArtifactResolver resolver, final String groupId, final String artifactId, final String version, final String type, final String classifier) throws MojoExecutionException { final Set<Artifact> artifacts = project.getDependencyArtifacts(); for (final Artifact artifact : artifacts) { if (artifact.getGroupId().equals(groupId) && artifact.getArtifactId().equals(artifactId) && artifact.getVersion().equals(version) && artifact.getType().equals(type) && ((classifier == null && artifact.getClassifier() == null) || (classifier != null && classifier.equals(artifact.getClassifier())))) { return artifact; } } final Artifact prjArtifact = new DefaultArtifact(groupId, artifactId, VersionRange.createFromVersion(version), Artifact.SCOPE_PROVIDED, type, classifier, artifactHandlerManager.getArtifactHandler(type)); try { resolver.resolve(prjArtifact, project.getRemoteArtifactRepositories(), session.getLocalRepository()); } catch (final ArtifactResolutionException e) { throw new MojoExecutionException( "Unable to get artifact for " + groupId + ":" + artifactId + ":" + version, e); } catch (final ArtifactNotFoundException e) { throw new MojoExecutionException( "Unable to get artifact for " + groupId + ":" + artifactId + ":" + version, e); } return prjArtifact; }
From source file:org.apache.tuscany.maven.plugin.eclipse.AbstractIdeSupportMojo.java
License:Apache License
/** * Resolve project dependencies. Manual resolution is needed in order to avoid resolution of multiproject artifacts * (if projects will be linked each other an installed jar is not needed) and to avoid a failure when a jar is * missing./*from w ww . j a v a2 s . co m*/ * * @throws MojoExecutionException if dependencies can't be resolved * @return resolved IDE dependencies, with attached jars for non-reactor dependencies */ protected IdeDependency[] doDependencyResolution() throws MojoExecutionException { if (ideDeps == null) { if (resolveDependencies) { MavenProject project = getProject(); Set<String> imported = Collections.emptySet(); try { imported = BundleUtil.getImportedPackages(project.getBasedir()); } catch (IOException e1) { throw new MojoExecutionException(e1.getMessage(), e1); } ArtifactRepository localRepo = getLocalRepository(); List deps = getProject().getDependencies(); // Collect the list of resolved IdeDependencies. List dependencies = new ArrayList(); if (deps != null) { Map managedVersions = createManagedVersionMap(getArtifactFactory(), project.getId(), project.getDependencyManagement()); ArtifactResolutionResult artifactResolutionResult = null; try { List listeners = new ArrayList(); if (logger.isDebugEnabled()) { listeners.add(new DebugResolutionListener(logger)); } listeners.add(new WarningResolutionListener(logger)); artifactResolutionResult = artifactCollector.collect(getProjectArtifacts(), project.getArtifact(), managedVersions, localRepo, project.getRemoteArtifactRepositories(), getArtifactMetadataSource(), null, listeners); } catch (ArtifactResolutionException e) { getLog().debug(e.getMessage(), e); getLog().error( Messages.getString("AbstractIdeSupportMojo.artifactresolution", new Object[] { //$NON-NLS-1$ e.getGroupId(), e.getArtifactId(), e.getVersion(), e.getMessage() })); // if we are here artifactResolutionResult is null, create a project without dependencies but // don't fail // (this could be a reactor projects, we don't want to fail everything) // Causes MECLIPSE-185. Not sure if it should be handled this way?? return new IdeDependency[0]; } // keep track of added reactor projects in order to avoid duplicates Set emittedReactorProjectId = new HashSet(); for (Iterator i = artifactResolutionResult.getArtifactResolutionNodes().iterator(); i .hasNext();) { ResolutionNode node = (ResolutionNode) i.next(); int dependencyDepth = node.getDepth(); Artifact art = node.getArtifact(); // don't resolve jars for reactor projects if (hasToResolveJar(art)) { try { artifactResolver.resolve(art, node.getRemoteRepositories(), localRepository); } catch (ArtifactNotFoundException e) { getLog().debug(e.getMessage(), e); getLog().warn(Messages.getString("AbstractIdeSupportMojo.artifactdownload", //$NON-NLS-1$ new Object[] { e.getGroupId(), e.getArtifactId(), e.getVersion(), e.getMessage() })); } catch (ArtifactResolutionException e) { getLog().debug(e.getMessage(), e); getLog().warn(Messages.getString("AbstractIdeSupportMojo.artifactresolution", //$NON-NLS-1$ new Object[] { e.getGroupId(), e.getArtifactId(), e.getVersion(), e.getMessage() })); } } boolean includeArtifact = true; if (getExcludes() != null) { String artifactFullId = art.getGroupId() + ":" + art.getArtifactId(); if (getExcludes().contains(artifactFullId)) { getLog().info("excluded: " + artifactFullId); includeArtifact = false; } } if (includeArtifact && (!(getUseProjectReferences() && isAvailableAsAReactorProject(art)) || emittedReactorProjectId.add(art.getGroupId() + '-' + art.getArtifactId()))) { // the following doesn't work: art.getArtifactHandler().getPackaging() always returns "jar" // also // if the packaging specified in pom.xml is different. // osgi-bundle packaging is provided by the felix osgi plugin // eclipse-plugin packaging is provided by this eclipse plugin // String packaging = art.getArtifactHandler().getPackaging(); // boolean isOsgiBundle = "osgi-bundle".equals( packaging ) || "eclipse-plugin".equals( // packaging ); // we need to check the manifest, if "Bundle-SymbolicName" is there the artifact can be // considered // an osgi bundle if ("pom".equals(art.getType())) { continue; } File artifactFile = art.getFile(); MavenProject reactorProject = getReactorProject(art); if (reactorProject != null) { artifactFile = reactorProject.getBasedir(); } boolean isOsgiBundle = false; String osgiSymbolicName = null; try { osgiSymbolicName = BundleUtil.getBundleSymbolicName(artifactFile); } catch (IOException e) { getLog().error("Unable to read jar manifest from " + artifactFile, e); } isOsgiBundle = osgiSymbolicName != null; IdeDependency dep = new IdeDependency(art.getGroupId(), art.getArtifactId(), art.getVersion(), art.getClassifier(), useProjectReference(art), Artifact.SCOPE_TEST.equals(art.getScope()), Artifact.SCOPE_SYSTEM.equals(art.getScope()), Artifact.SCOPE_PROVIDED.equals(art.getScope()), art.getArtifactHandler().isAddedToClasspath(), art.getFile(), art.getType(), isOsgiBundle, osgiSymbolicName, dependencyDepth, getProjectNameForArifact(art)); // no duplicate entries allowed. System paths can cause this problem. if (!dependencies.contains(dep)) { // [rfeng] Do not add compile/provided dependencies if (!(pde && (Artifact.SCOPE_COMPILE.equals(art.getScope()) || Artifact.SCOPE_PROVIDED.equals(art.getScope())))) { dependencies.add(dep); } else { // Check this compile dependency is an OSGi package supplier if (!imported.isEmpty()) { Set<String> exported = Collections.emptySet(); try { exported = BundleUtil.getExportedPackages(artifactFile); } catch (IOException e) { getLog().error("Unable to read jar manifest from " + art.getFile(), e); } boolean matched = false; for (String p : imported) { if (exported.contains(p)) { matched = true; break; } } if (!matched) { dependencies.add(dep); } else { getLog().debug( "Compile dependency is skipped as it is added through OSGi dependency: " + art); } } else { dependencies.add(dep); } } } } } // @todo a final report with the list of // missingArtifacts? } ideDeps = (IdeDependency[]) dependencies.toArray(new IdeDependency[dependencies.size()]); } else { ideDeps = new IdeDependency[0]; } } return ideDeps; }
From source file:org.arakhne.maven.AbstractArakhneMojo.java
License:Open Source License
/** Load the Maven project for the given artifact. * /*from ww w. j a v a2 s.c o m*/ * @param artifact * @return the maven project. */ public MavenProject getMavenProject(Artifact artifact) { try { MavenSession session = getMavenSession(); MavenProject current = session.getCurrentProject(); MavenProject prj = getMavenProjectBuilder().buildFromRepository(artifact, current.getRemoteArtifactRepositories(), session.getLocalRepository()); return prj; } catch (ProjectBuildingException e) { getLog().warn(e); } return null; }
From source file:org.axway.grapes.maven.resolver.ArtifactResolver.java
/** * Resolve an artifact from repository/*from w w w. j ava2 s. co m*/ * * @param project MavenProject * @param artifact Artifact * @throws org.apache.maven.plugin.MojoExecutionException */ public void resolveArtifact(final MavenProject project, final Artifact artifact) throws MojoExecutionException { logger.debug("Resolving artifact " + artifact.toString()); final ArtifactResolutionRequest artifactRequest = new ArtifactResolutionRequest(); artifactRequest.setArtifact(artifact); artifactRequest.setLocalRepository(localRepository); artifactRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); final ArtifactResolutionResult resolutionResult = repositorySystem.resolve(artifactRequest); if (!resolutionResult.isSuccess()) { logger.debug("Failed to resolved " + artifact.toString() + " artifact."); } }
From source file:org.codehaus.mojo.animal_sniffer.enforcer.CheckSignatureRule.java
License:Open Source License
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { try {/* w w w .ja v a 2 s. c om*/ List classpathElements = (List) helper.evaluate("${project.compileClasspathElements}"); File outputDirectory = new File((String) helper.evaluate("${project.build.outputDirectory}")); ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class); MavenProject project = (MavenProject) helper.evaluate("${project}"); ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}"); ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); helper.getLog().info("Checking unresolved references to " + signature); org.apache.maven.artifact.Artifact a = signature.createArtifact(artifactFactory); resolver.resolve(a, project.getRemoteArtifactRepositories(), localRepository); // just check code from this module final Set ignoredPackages = buildPackageList(outputDirectory, classpathElements); if (ignores != null) { for (int i = 0; i < ignores.length; i++) { String ignore = ignores[i]; if (ignore == null) { continue; } ignoredPackages.add(ignore.replace('.', '/')); } } final SignatureChecker signatureChecker = new SignatureChecker(new FileInputStream(a.getFile()), ignoredPackages, new MavenLogger(helper.getLog())); signatureChecker.process(outputDirectory); if (signatureChecker.isSignatureBroken()) { throw new EnforcerRuleException( "Signature errors found. Verify them and put @IgnoreJRERequirement on them."); } } catch (IOException e) { throw new EnforcerRuleException("Failed to check signatures", e); } catch (AbstractArtifactResolutionException e) { throw new EnforcerRuleException("Failed to obtain signature: " + signature, e); } catch (ComponentLookupException e) { throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); } catch (ExpressionEvaluationException e) { throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e); } }
From source file:org.codehaus.mojo.cobertura.InheritProject.java
License:Apache License
/** * Resolves the Artifact from the remote repository if nessessary. If no version is specified, it will * be retrieved from the dependency list or from the DependencyManagement section of the pom. * * @param project This actual project, not the inherited one. * @param artifact The artifact to be resolved; must not be null * @param transitive True to resolve the artifact transitivly * @return The resolved artifact; never null * * @throws MojoExecutionException Failed to resolve artifact *///from ww w .j a va 2 s . c om private Artifact resolveArtifact(final MavenProject project, final Artifact artifact, final boolean transitive) throws MojoExecutionException { assert artifact != null; try { if (transitive) { artifactResolver.resolveTransitively(Collections.singleton(artifact), project.getArtifact(), project.getRemoteArtifactRepositories(), artifactRepository, artifactMetadataSource); } else { artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), artifactRepository); } } catch (ArtifactResolutionException e) { throw new MojoExecutionException("Unable to resolve artifact", e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException("Unable to find artifact", e); } return artifact; }
From source file:org.codehaus.mojo.nbm.AbstractNbmMojo.java
License:Apache License
protected final ArtifactResult turnJarToNbmFile(Artifact art, ArtifactFactory artifactFactory, ArtifactResolver artifactResolver, MavenProject project, ArtifactRepository localRepository) throws MojoExecutionException { if ("jar".equals(art.getType()) || "nbm".equals(art.getType())) { //TODO, it would be nice to have a check to see if the // "to-be-created" module nbm artifact is actually already in the // list of dependencies (as "nbm-file") or not.. // that would be a timesaver ExamineManifest mnf = new ExamineManifest(getLog()); File jar = art.getFile(); if (!jar.isFile()) { //MNBMODULE-210 with recent CoS changes in netbeans (7.4) jar will be file as we link open projects in the build // via WorkspaceReader. That's fine here, as all we need is to know if project is osgi or nbm module. // the nbm file has to be in local repository though. String path = localRepository.pathOf(art); File jar2 = new File(localRepository.getBasedir(), path.replace("/", File.separator)); File manifest = new File(jar, "META-INF/MANIFEST.MF"); if (!jar2.isFile() || !manifest.isFile()) { getLog().warn("MNBMODULE-131: need to at least run install phase on " + jar2); return new ArtifactResult(null, null); }/*from w w w.j av a 2 s .c om*/ mnf.setManifestFile(manifest); } else { mnf.setJarFile(jar); } mnf.checkFile(); if (mnf.isNetBeansModule()) { Artifact nbmArt = artifactFactory.createDependencyArtifact(art.getGroupId(), art.getArtifactId(), art.getVersionRange(), "nbm-file", art.getClassifier(), art.getScope()); try { artifactResolver.resolve(nbmArt, project.getRemoteArtifactRepositories(), localRepository); } catch (ArtifactResolutionException ex) { //shall be check before actually resolving from repos? checkReactor(art, nbmArt); if (!nbmArt.isResolved()) { throw new MojoExecutionException("Failed to retrieve the nbm file from repository", ex); } } catch (ArtifactNotFoundException ex) { //shall be check before actually resolving from repos? checkReactor(art, nbmArt); if (!nbmArt.isResolved()) { throw new MojoExecutionException("Failed to retrieve the nbm file from repository", ex); } } return new ArtifactResult(nbmArt, mnf); } if (mnf.isOsgiBundle()) { return new ArtifactResult(null, mnf); } } return new ArtifactResult(null, null); }
From source file:org.codehaus.mojo.pluginsupport.dependency.DependencyHelper.java
License:Apache License
public DependencyTree getDependencies(final MavenProject project) throws ProjectBuildingException, InvalidDependencyVersionException, ArtifactResolutionException { assert project != null; Map managedVersions = getManagedVersionMap(project, artifactFactory); DependencyResolutionListener listener = new DependencyResolutionListener(); if (project.getDependencyArtifacts() == null) { project.setDependencyArtifacts(project.createArtifacts(artifactFactory, null, null)); }// www . ja v a 2 s . com artifactCollector.collect(project.getDependencyArtifacts(), project.getArtifact(), managedVersions, getArtifactRepository(), project.getRemoteArtifactRepositories(), artifactMetadataSource, null, Collections.singletonList(listener)); return listener.getDependencyTree(); }
From source file:org.deegree.maven.utils.ClasspathHelper.java
License:Open Source License
/** * @param project//from w w w .j av a 2s . c o m * @param artifactResolver * @param artifactFactory * @param metadataSource * @param localRepository * @param type * @return a list of all (possibly transitive) artifacts of the given type * @throws InvalidDependencyVersionException * @throws ArtifactResolutionException * @throws ArtifactNotFoundException */ public static Set<?> getDependencyArtifacts(MavenProject project, ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, ArtifactMetadataSource metadataSource, ArtifactRepository localRepository, final String type, boolean transitively) throws InvalidDependencyVersionException, ArtifactResolutionException, ArtifactNotFoundException { List<?> dependencies = project.getDependencies(); Set<Artifact> dependencyArtifacts = createArtifacts(artifactFactory, dependencies, null, new ArtifactFilter() { @Override public boolean include(Artifact artifact) { return artifact != null && artifact.getType() != null && artifact.getType().equals(type); } }, null); ArtifactResolutionResult result; Artifact mainArtifact = project.getArtifact(); result = artifactResolver.resolveTransitively(dependencyArtifacts, mainArtifact, EMPTY_MAP, localRepository, project.getRemoteArtifactRepositories(), metadataSource, null, EMPTY_LIST); if (transitively) { return result.getArtifacts(); } LinkedHashSet<Artifact> set = new LinkedHashSet<Artifact>(); if (mainArtifact.getType() != null && mainArtifact.getType().equals(type)) { set.add(mainArtifact); } set.addAll(dependencyArtifacts); return set; }