Search in sources :

Example 1 with ArtifactCoordinate

use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.

the class AbstractDependencyFilterMojo method resolve.

protected Set<Artifact> resolve(Set<ArtifactCoordinate> coordinates, boolean stopOnFailure) throws MojoExecutionException {
    ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
    Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
    for (ArtifactCoordinate coordinate : coordinates) {
        try {
            Artifact artifact = artifactResolver.resolveArtifact(buildingRequest, coordinate).getArtifact();
            resolvedArtifacts.add(artifact);
        } catch (ArtifactResolverException ex) {
            // an error occurred during resolution, log it an continue
            getLog().debug("error resolving: " + coordinate);
            getLog().debug(ex);
            if (stopOnFailure) {
                throw new MojoExecutionException("error resolving: " + coordinate, ex);
            }
        }
    }
    return resolvedArtifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Artifact(org.apache.maven.artifact.Artifact)

Example 2 with ArtifactCoordinate

use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.

the class TestClassifierTypeTranslator method testClassifierAndType.

public void testClassifierAndType() {
    String classifier = "jdk14";
    String type = "sources";
    ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
    Set<ArtifactCoordinate> results = at.translate(artifacts, log);
    for (Artifact artifact : artifacts) {
        Iterator<ArtifactCoordinate> resultIter = results.iterator();
        boolean found = false;
        while (!found && resultIter.hasNext()) {
            ArtifactCoordinate translatedArtifact = resultIter.next();
            if (artifact.getArtifactId() == translatedArtifact.getArtifactId() && artifact.getGroupId() == translatedArtifact.getGroupId()) /*&& artifact.getScope() == translatedArtifact.getScope()*/
            {
                assertEquals(translatedArtifact.getClassifier(), classifier);
                assertEquals(translatedArtifact.getExtension(), type);
                found = true;
                break;
            }
        }
        assertTrue(found);
    }
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) Artifact(org.apache.maven.artifact.Artifact)

Example 3 with ArtifactCoordinate

use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.

the class TestClassifierTypeTranslator method doTestNullEmptyClassifier.

public void doTestNullEmptyClassifier(String classifier) {
    String type = "zip";
    ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
    Set<ArtifactCoordinate> results = at.translate(artifacts, log);
    for (Artifact artifact : artifacts) {
        Iterator<ArtifactCoordinate> resultIter = results.iterator();
        boolean found = false;
        while (resultIter.hasNext()) {
            ArtifactCoordinate translatedArtifact = resultIter.next();
            if (artifact.getArtifactId().equals(translatedArtifact.getArtifactId()) && artifact.getGroupId().equals(translatedArtifact.getGroupId())) /*&& artifact.getScope().equals(translatedArtifact.getScope())*/
            {
                // classifier is null, should be the same as the artifact
                assertEquals(artifact.getClassifier(), translatedArtifact.getClassifier());
                assertEquals(type, translatedArtifact.getExtension());
                found = true;
                break;
            }
        }
        assertTrue(found);
    }
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) Artifact(org.apache.maven.artifact.Artifact)

Example 4 with ArtifactCoordinate

use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.

the class ShadeMojoTest method testShadeWithFilter.

/**
     * Tests if a Filter is installed correctly, also if createSourcesJar is set to true.
     *
     * @throws Exception
     */
public void testShadeWithFilter() throws Exception {
    ShadeMojo mojo = new ShadeMojo();
    // set createSourcesJar = true
    Field createSourcesJar = ShadeMojo.class.getDeclaredField("createSourcesJar");
    createSourcesJar.setAccessible(true);
    createSourcesJar.set(mojo, Boolean.TRUE);
    // configure artifactResolver (mocked) for mojo
    ArtifactResolver mockArtifactResolver = new ArtifactResolver() {

        @Override
        public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final Artifact art) throws ArtifactResolverException {
            return new ArtifactResult() {

                @Override
                public Artifact getArtifact() {
                    art.setResolved(true);
                    String fileName = art.getArtifactId() + "-" + art.getVersion() + (art.getClassifier() != null ? "-" + art.getClassifier() : "") + ".jar";
                    art.setFile(new File(fileName));
                    return art;
                }
            };
        }

        @Override
        public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final ArtifactCoordinate coordinate) throws ArtifactResolverException {
            return new ArtifactResult() {

                @Override
                public Artifact getArtifact() {
                    Artifact art = mock(Artifact.class);
                    when(art.getGroupId()).thenReturn(coordinate.getGroupId());
                    when(art.getArtifactId()).thenReturn(coordinate.getArtifactId());
                    when(art.getType()).thenReturn(coordinate.getExtension());
                    when(art.getClassifier()).thenReturn(coordinate.getClassifier());
                    when(art.isResolved()).thenReturn(true);
                    String fileName = coordinate.getArtifactId() + "-" + coordinate.getVersion() + (coordinate.getClassifier() != null ? "-" + coordinate.getClassifier() : "") + ".jar";
                    when(art.getFile()).thenReturn(new File(fileName));
                    return art;
                }
            };
        }
    };
    Field artifactResolverField = ShadeMojo.class.getDeclaredField("artifactResolver");
    artifactResolverField.setAccessible(true);
    artifactResolverField.set(mojo, mockArtifactResolver);
    // create and configure MavenProject
    MavenProject project = new MavenProject();
    ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE);
    Artifact artifact = new DefaultArtifact("org.apache.myfaces.core", "myfaces-impl", VersionRange.createFromVersion("2.0.1-SNAPSHOT"), "compile", "jar", null, artifactHandler);
    // setFile and setResolved
    artifact = mockArtifactResolver.resolveArtifact(null, artifact).getArtifact();
    project.setArtifact(artifact);
    Field projectField = ShadeMojo.class.getDeclaredField("project");
    projectField.setAccessible(true);
    projectField.set(mojo, project);
    // create and configure the ArchiveFilter
    ArchiveFilter archiveFilter = new ArchiveFilter();
    Field archiveFilterArtifact = ArchiveFilter.class.getDeclaredField("artifact");
    archiveFilterArtifact.setAccessible(true);
    archiveFilterArtifact.set(archiveFilter, "org.apache.myfaces.core:myfaces-impl");
    // add ArchiveFilter to mojo
    Field filtersField = ShadeMojo.class.getDeclaredField("filters");
    filtersField.setAccessible(true);
    filtersField.set(mojo, new ArchiveFilter[] { archiveFilter });
    Field sessionField = ShadeMojo.class.getDeclaredField("session");
    sessionField.setAccessible(true);
    sessionField.set(mojo, mock(MavenSession.class));
    // invoke getFilters()
    Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", new Class[0]);
    getFilters.setAccessible(true);
    List<Filter> filters = (List<Filter>) getFilters.invoke(mojo);
    // assertions - there must be one filter
    assertEquals(1, filters.size());
    // the filter must be able to filter the binary and the sources jar
    Filter filter = filters.get(0);
    // binary jar
    assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT.jar")));
    // sources jar
    assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT-sources.jar")));
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) Method(java.lang.reflect.Method) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) Field(java.lang.reflect.Field) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) MavenProject(org.apache.maven.project.MavenProject) Filter(org.apache.maven.plugins.shade.filter.Filter) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 5 with ArtifactCoordinate

use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.

the class EvaluateMojo method getArtifactFile.

/**
     * @param isPom <code>true</code> to lookup the <code>maven-model</code> artifact jar, <code>false</code> to lookup
     *            the <code>maven-settings</code> artifact jar.
     * @return the <code>org.apache.maven:maven-model|maven-settings</code> artifact jar file for this current
     *         HelpPlugin pom.
     * @throws MojoExecutionException if any
     * @throws ProjectBuildingException if any
     * @throws ArtifactResolverException if any
     */
private File getArtifactFile(boolean isPom) throws MojoExecutionException, ProjectBuildingException, ArtifactResolverException {
    List<Dependency> dependencies = getHelpPluginPom().getDependencies();
    for (Dependency depependency : dependencies) {
        if (!(depependency.getGroupId().equals("org.apache.maven"))) {
            continue;
        }
        if (isPom) {
            if (!(depependency.getArtifactId().equals("maven-model"))) {
                continue;
            }
        } else {
            if (!(depependency.getArtifactId().equals("maven-settings"))) {
                continue;
            }
        }
        ArtifactCoordinate coordinate = getArtifactCoordinate(depependency.getGroupId(), depependency.getArtifactId(), depependency.getVersion(), "jar");
        ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        pbr.setRemoteRepositories(remoteRepositories);
        return artifactResolver.resolveArtifact(pbr, coordinate).getArtifact().getFile();
    }
    throw new MojoExecutionException("Unable to find the 'org.apache.maven:" + (isPom ? "maven-model" : "maven-settings") + "' artifact");
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Dependency(org.apache.maven.model.Dependency) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Aggregations

ArtifactCoordinate (org.apache.maven.shared.artifact.ArtifactCoordinate)10 Artifact (org.apache.maven.artifact.Artifact)9 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)3 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 ArtifactHandler (org.apache.maven.artifact.handler.ArtifactHandler)2 DefaultArtifactCoordinate (org.apache.maven.shared.artifact.DefaultArtifactCoordinate)2 File (java.io.File)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)1 MavenSession (org.apache.maven.execution.MavenSession)1 Dependency (org.apache.maven.model.Dependency)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 MojoDescriptor (org.apache.maven.plugin.descriptor.MojoDescriptor)1