Skip to content

Commit

Permalink
Resolving Maven dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Suszynski authored and cardil committed Jun 6, 2019
1 parent d675ab5 commit af99f43
Show file tree
Hide file tree
Showing 36 changed files with 778 additions and 123 deletions.
11 changes: 11 additions & 0 deletions tools/plugs-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,16 @@
<filtering>true</filtering>
</testResource>
</testResources>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-Xlint:all,-processing</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package pl.wavesoftware.plugs.tools.maven.plugin;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -84,6 +85,18 @@ final class PackagePlugMojo extends AbstractMojo {
)
private MavenProject project;

/**
* The Maven session.
*
* @since 0.1.0
*/
@Parameter(
defaultValue = "${session}",
required = true,
readonly = true
)
private MavenSession session;

/**
* Classifier to add to the repackaged archive. If not given, classifier
* "plug" will be used. If given, the classifier will also be used
Expand Down Expand Up @@ -233,6 +246,7 @@ private void attachArtifactIfNeeded(PackagerConfiguration configuration) {
private PackagerConfiguration createConfiguration() {
return factories.configuration().create(
project,
session,
logger,
classifier,
attach,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package pl.wavesoftware.plugs.tools.maven.plugin.mapper;

import org.apache.maven.model.Dependency;
import io.vavr.collection.Traversable;
import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency;
import pl.wavesoftware.plugs.tools.packager.core.model.Artifact;

/**
Expand All @@ -26,5 +27,5 @@
public interface ArtifactMapper {
Artifact generalize(org.apache.maven.artifact.Artifact artifact);
org.apache.maven.artifact.Artifact mavenize(Artifact artifact);
Artifact map(Dependency dependency);
Traversable<Artifact> map(ResolvableDependency dependency);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@

package pl.wavesoftware.plugs.tools.maven.plugin.mapper;

import org.apache.maven.model.Dependency;
import io.vavr.collection.HashSet;
import io.vavr.collection.Traversable;
import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.repository.RepositorySystem;
import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency;
import pl.wavesoftware.plugs.tools.packager.core.model.Artifact;

import javax.inject.Inject;
Expand All @@ -31,10 +38,15 @@
final class ArtifactMapperImpl implements ArtifactMapper {

private final RepositorySystem repositorySystem;
private final ArtifactResolver artifactResolver;

@Inject
ArtifactMapperImpl(RepositorySystem repositorySystem) {
ArtifactMapperImpl(
RepositorySystem repositorySystem,
ArtifactResolver artifactResolver
) {
this.repositorySystem = repositorySystem;
this.artifactResolver = artifactResolver;
}

@Override
Expand All @@ -47,16 +59,26 @@ public org.apache.maven.artifact.Artifact mavenize(Artifact artifact) {
if (artifact instanceof MavenArtifact) {
return ((MavenArtifact) artifact).getDelegate();
}
if (artifact instanceof MavenDependency) {
return ((MavenDependency) artifact).asArtifact();
}
throw new UnsupportedOperationException(
"Not supported artifact type: " + artifact.getClass()
);
}

@Override
public Artifact map(Dependency dependency) {
return new MavenDependency(repositorySystem, dependency);
public Traversable<Artifact> map(ResolvableDependency resolvable) {
return resolve(resolvable).map(this::generalize);
}

private Traversable<org.apache.maven.artifact.Artifact> resolve(ResolvableDependency resolvable) {
RepositoryRequest repositoryRequest = DefaultRepositoryRequest.getRepositoryRequest(
resolvable.mavenSession(), resolvable.mavenProject()
);
ArtifactResolutionRequest request = new ArtifactResolutionRequest(repositoryRequest);
request.setResolveTransitively(true);
org.apache.maven.artifact.Artifact artifact =
repositorySystem.createDependencyArtifact(resolvable.dependency());
request.setArtifact(artifact);
ArtifactResolutionResult response = artifactResolver.resolve(request);
return HashSet.ofAll(response.getArtifacts());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ public Path path() {
public ArtifactType type() {
return ArtifactType.fromPackging(getDelegate().getType());
}

@Override
public String toString() {
return "MavenArtifact{" +
name() + "@" + version() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import io.vavr.Lazy;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
import pl.wavesoftware.plugs.tools.maven.plugin.model.ResolvableDependency;
import pl.wavesoftware.plugs.tools.packager.core.model.Artifact;
import pl.wavesoftware.plugs.tools.packager.core.model.Project;

Expand All @@ -33,6 +35,7 @@
final class MavenBackedProject implements Project {
private final ArtifactMapper artifactMapper;
private final MavenProject mavenProject;
private final MavenSession mavenSession;
private final Path path;
private final String finalName;
private final String classifier;
Expand All @@ -43,12 +46,14 @@ final class MavenBackedProject implements Project {
MavenBackedProject(
ArtifactMapper artifactMapper,
MavenProject mavenProject,
MavenSession mavenSession,
Path path,
String finalName,
String classifier
) {
this.artifactMapper = artifactMapper;
this.mavenProject = mavenProject;
this.mavenSession = mavenSession;
this.path = path;
this.finalName = finalName;
this.classifier = classifier;
Expand All @@ -57,6 +62,11 @@ final class MavenBackedProject implements Project {
importsLazy = Lazy.of(this::calculateImports);
}

@Override
public Path buildFilePath() {
return mavenProject.getBasedir().toPath().resolve("pom.xml");
}

@Override
public Artifact mainArtifact() {
return artifactMapper.generalize(mavenProject.getArtifact());
Expand Down Expand Up @@ -91,14 +101,20 @@ private Set<Artifact> calculateDependencies() {
return HashSet
.ofAll(mavenProject.getDependencies())
.reject(MavenBackedProject::hasProvidedScope)
.map(artifactMapper::map);
.map(this::asResolvable)
.flatMap(artifactMapper::map);
}

private Set<Artifact> calculateImports() {
return HashSet
.ofAll(mavenProject.getDependencies())
.filter(MavenBackedProject::hasProvidedScope)
.map(artifactMapper::map);
.map(this::asResolvable)
.flatMap(artifactMapper::map);
}

private ResolvableDependency asResolvable(Dependency dependency) {
return new ResolvableDependency(dependency, mavenProject, mavenSession);
}

private static boolean hasProvidedScope(Dependency dependency) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package pl.wavesoftware.plugs.tools.maven.plugin.mapper;

import io.vavr.Lazy;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration;
Expand All @@ -35,6 +36,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration {
private final ArtifactMapper artifactMapper;
private final Logger logger;
private final MavenProject mavenProject;
private final MavenSession mavenSession;
private final Supplier<Project> project;
private final String classifier;
private final boolean attach;
Expand All @@ -46,6 +48,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration {
ArtifactMapper artifactMapper,
Logger logger,
MavenProject project,
MavenSession session,
String classifier,
boolean attach,
File outputDirectory,
Expand All @@ -54,6 +57,7 @@ final class MavenPackagerConfiguration implements PackagerConfiguration {
this.artifactMapper = artifactMapper;
this.logger = logger;
this.mavenProject = project;
this.mavenSession = session;
this.classifier = classifier;
this.attach = attach;
this.outputDirectory = outputDirectory;
Expand Down Expand Up @@ -86,6 +90,7 @@ private Project calculateProject() {
return new MavenBackedProject(
artifactMapper,
mavenProject,
mavenSession,
outputDirectory.toPath(),
finalName,
classifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package pl.wavesoftware.plugs.tools.maven.plugin.mapper;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration;
Expand All @@ -41,6 +42,7 @@ final class MavenPackagerConfigurationFactory implements PackagerConfigurationFa
@Override
public PackagerConfiguration create(
MavenProject project,
MavenSession session,
Logger logger,
String classifier,
boolean attach,
Expand All @@ -51,6 +53,7 @@ public PackagerConfiguration create(
artifactMapper,
logger,
project,
session,
classifier,
attach,
outputDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package pl.wavesoftware.plugs.tools.maven.plugin.mapper;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import pl.wavesoftware.plugs.tools.packager.core.model.PackagerConfiguration;
Expand All @@ -33,6 +34,7 @@ public interface PackagerConfigurationFactory {
* Creates a configuration
*
* @param project a maven project
* @param session a maven session
* @param logger a logger
* @param classifier classifier of plug to create
* @param attach should created plug be attached to reactor
Expand All @@ -42,6 +44,7 @@ public interface PackagerConfigurationFactory {
*/
PackagerConfiguration create(
MavenProject project,
MavenSession session,
Logger logger,
String classifier,
boolean attach,
Expand Down
Loading

0 comments on commit af99f43

Please sign in to comment.