Search in sources :

Example 11 with DefaultArtifact

use of org.apache.maven.artifact.DefaultArtifact in project build-info by JFrogDev.

the class BuildInfoRecorder method mergeProjectDependencies.

/**
 * Merge the dependencies taken from the MavenProject object with those
 * collected inside the resolvedArtifacts collection.
 *
 * @param projectDependencies The artifacts taken from the MavenProject
 *                            object.
 */
private void mergeProjectDependencies(Set<Artifact> projectDependencies) {
    // Go over all the artifacts taken from the MavenProject object, and replace their equals method, so that we are
    // able to merge them together with the artifacts inside the resolvedArtifacts set:
    Set<Artifact> dependecies = Sets.newHashSet();
    for (Artifact artifact : projectDependencies) {
        String classifier = artifact.getClassifier();
        classifier = classifier == null ? "" : classifier;
        String scope = artifact.getScope();
        // HAP-909
        scope = StringUtils.isBlank(scope) ? Artifact.SCOPE_COMPILE : scope;
        DefaultArtifact art = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), scope, artifact.getType(), classifier, artifact.getArtifactHandler());
        art.setFile(artifact.getFile());
        dependecies.add(art);
    }
    // Now we merge the artifacts from the two collections. In case an artifact is included in both collections, we'd like to keep
    // the one that was taken from the MavenProject, because of the scope it has.
    // The merge is done only if the client is configured to do so.
    Set<Artifact> moduleDependencies = currentModuleDependencies.get();
    Set<Artifact> tempSet = Sets.newHashSet(moduleDependencies);
    moduleDependencies.clear();
    moduleDependencies.addAll(dependecies);
    moduleDependencies.addAll(tempSet);
    if (conf.publisher.isRecordAllDependencies()) {
        moduleDependencies.addAll(resolvedArtifacts);
    }
}
Also used : BuildInfoExtractorUtils.getModuleIdString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString) BuildInfoExtractorUtils.getTypeString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 12 with DefaultArtifact

use of org.apache.maven.artifact.DefaultArtifact in project component-runtime by Talend.

the class ArtifactTransformer method hasTransformedResource.

@Override
public boolean hasTransformedResource() {
    artifacts = new ArrayList<>();
    final ArtifactFilter filter = getFilter();
    if (userArtifacts != null && !userArtifacts.isEmpty()) {
        final ArtifactResolver resolver;
        final DependencyGraphBuilder graphBuilder;
        final ProjectBuilder projectBuilder;
        final PlexusContainer container = session.getContainer();
        try {
            resolver = ArtifactResolver.class.cast(container.lookup(ArtifactResolver.class, "default"));
            projectBuilder = ProjectBuilder.class.cast(container.lookup(ProjectBuilder.class, "default"));
            graphBuilder = includeTransitiveDependencies ? DependencyGraphBuilder.class.cast(container.lookup(DependencyGraphBuilder.class, "default")) : null;
        } catch (final ComponentLookupException e) {
            throw new IllegalArgumentException(e);
        }
        artifacts.addAll(userArtifacts.stream().flatMap(coords -> {
            try {
                final String type = ofNullable(coords.type).filter(s -> !s.isEmpty()).orElse("jar");
                final Artifact art = new DefaultArtifact(coords.groupId, coords.artifactId, coords.version, coords.scope, type, ofNullable(coords.classifier).orElse(""), new DefaultArtifactHandler() {

                    {
                        setExtension(type);
                    }
                });
                final Artifact artifact = resolver.resolveArtifact(session.getProjectBuildingRequest(), art).getArtifact();
                if (includeTransitiveDependencies) {
                    final DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
                    try {
                        final ProjectBuildingResult projectBuildingResult = projectBuilder.build(artifact, true, request);
                        final DependencyNode transitives = graphBuilder.buildDependencyGraph(projectBuildingResult.getProject(), filter);
                        final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
                        transitives.accept(new FilteringDependencyNodeVisitor(visitor, new ArtifactDependencyNodeFilter(filter)));
                        return Stream.concat(Stream.of(artifact), visitor.getNodes().stream().map(DependencyNode::getArtifact).map(a -> {
                            try {
                                return resolver.resolveArtifact(request, a).getArtifact();
                            } catch (final ArtifactResolverException e) {
                                throw new IllegalStateException(e);
                            }
                        }));
                    } catch (final ProjectBuildingException | DependencyGraphBuilderException e) {
                        throw new IllegalStateException(e);
                    }
                }
                return Stream.of(artifact);
            } catch (final ArtifactResolverException e) {
                throw new IllegalArgumentException(e);
            }
        }).collect(toSet()));
    }
    if (includeProjectComponentDependencies) {
        // this is a weird category of projects but can exist
        final MavenProject project = session.getCurrentProject();
        if (project.getArtifacts() != null && !project.getArtifacts().isEmpty()) {
            project.setArtifactFilter(filter);
            try {
                artifacts.addAll(project.getArtifacts());
            } finally {
                // shade plugin uses it OOTB so reset it for the end of the execution (in case
                // another transformer needs it)
                project.setArtifactFilter(new CumulativeScopeArtifactFilter(singletonList("runtime")));
            }
        }
    }
    return !artifacts.isEmpty();
}
Also used : CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) Setter(lombok.Setter) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) Collectors.toSet(java.util.stream.Collectors.toSet) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) MavenSession(org.apache.maven.execution.MavenSession) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) Optional.ofNullable(java.util.Optional.ofNullable) NONE(lombok.AccessLevel.NONE) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) IOException(java.io.IOException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) PlexusContainer(org.codehaus.plexus.PlexusContainer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Stream(java.util.stream.Stream) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) Data(lombok.Data) ProjectBuilder(org.apache.maven.project.ProjectBuilder) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) InputStream(java.io.InputStream) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) PlexusContainer(org.codehaus.plexus.PlexusContainer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 13 with DefaultArtifact

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

the class ResolutionManagementInfoTest method testAddSingleArtifactWithReplacemen.

public void testAddSingleArtifactWithReplacemen() throws Exception {
    ResolutionManagementInfo rmi = new ResolutionManagementInfo(new MavenProject());
    Artifact a1 = new DefaultArtifact("groupid", "1", VersionRange.createFromVersion("1.0"), "test", "jar", null, new DefaultArtifactHandler());
    rmi.addArtifacts(Collections.singleton(a1));
    Artifact a2 = new DefaultArtifact("groupid", "1", VersionRange.createFromVersion("1.0"), "compile", "jar", null, new DefaultArtifactHandler());
    rmi.addArtifacts(Collections.singleton(a2));
    assertEquals(1, rmi.getArtifacts().size());
    Artifact next = rmi.getArtifacts().iterator().next();
    assertEquals("compile", next.getScope());
}
Also used : MavenProject(org.apache.maven.project.MavenProject) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 14 with DefaultArtifact

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

the class RemoteResourcesMojoTest method testVelocityUTF8.

public void testVelocityUTF8() throws Exception {
    final MavenProjectResourcesStub project = createTestProject("default-utf8");
    final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings(project, new String[] { "test:test:1.2" });
    setupDefaultProject(project);
    ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject(mojo, "localRepository");
    String path = repo.pathOf(new DefaultArtifact("test", "test", VersionRange.createFromVersion("1.2"), null, "jar", "", new DefaultArtifactHandler()));
    File file = new File(repo.getBasedir() + "/" + path + ".jar");
    file.getParentFile().mkdirs();
    buildResourceBundle("default-utf8-create", "UTF-8", new String[] { "UTF-8.bin.vm" }, file);
    mojo.execute();
    file = (File) getVariableValueFromObject(mojo, "outputDirectory");
    file = new File(file, "UTF-8.bin");
    assertTrue(file.exists());
    InputStream in = new FileInputStream(file);
    byte[] data = IOUtil.toByteArray(in);
    in.close();
    byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes("UTF-8");
    assertTrue(Arrays.equals(expected, data));
}
Also used : MavenProjectResourcesStub(org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) FileInputStream(java.io.FileInputStream)

Example 15 with DefaultArtifact

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

the class RemoteResourcesMojoTest method testVelocityISO88591.

public void testVelocityISO88591() throws Exception {
    final MavenProjectResourcesStub project = createTestProject("default-iso88591");
    final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings(project, new String[] { "test:test:1.3" });
    setupDefaultProject(project);
    ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject(mojo, "localRepository");
    String path = repo.pathOf(new DefaultArtifact("test", "test", VersionRange.createFromVersion("1.3"), null, "jar", "", new DefaultArtifactHandler()));
    File file = new File(repo.getBasedir() + "/" + path + ".jar");
    file.getParentFile().mkdirs();
    buildResourceBundle("default-iso88591-create", "ISO-8859-1", new String[] { "ISO-8859-1.bin.vm" }, file);
    mojo.execute();
    file = (File) getVariableValueFromObject(mojo, "outputDirectory");
    file = new File(file, "ISO-8859-1.bin");
    assertTrue(file.exists());
    InputStream in = new FileInputStream(file);
    byte[] data = IOUtil.toByteArray(in);
    in.close();
    byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes("ISO-8859-1");
    assertTrue(Arrays.equals(expected, data));
}
Also used : MavenProjectResourcesStub(org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) FileInputStream(java.io.FileInputStream)

Aggregations

DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)51 Artifact (org.apache.maven.artifact.Artifact)35 File (java.io.File)26 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)26 ArtifactHandler (org.apache.maven.artifact.handler.ArtifactHandler)22 VersionRange (org.apache.maven.artifact.versioning.VersionRange)17 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)10 DefaultArtifactHandlerStub (org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub)10 MavenProject (org.apache.maven.project.MavenProject)9 MavenProjectResourcesStub (org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub)7 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 InputStream (java.io.InputStream)3 List (java.util.List)3 Map (java.util.Map)3 DefaultArtifactRepository (org.apache.maven.artifact.repository.DefaultArtifactRepository)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2