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);
}
}
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();
}
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());
}
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));
}
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));
}
Aggregations