Skip to content

Commit

Permalink
Merge pull request #148 from ascopes/task/simplify-project-dependency…
Browse files Browse the repository at this point in the history
…-resolution

Simplify project dependency resolution
  • Loading branch information
ascopes authored Apr 5, 2024
2 parents 0a98aa9 + 8ec2948 commit 1b77c15
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
Expand Down Expand Up @@ -255,10 +254,9 @@ public abstract class AbstractGenerateMojo extends AbstractMojo {
* Whether to fail on missing sources.
*
* <p>If no sources are detected, it is usually a sign that this plugin
* is misconfigured, or that you are including this plugin in a project
* that does not need it. For this reason, the plugin defaults this setting
* to being enabled. If you wish to not fail, you can explicitly set this
* to false instead.
* is misconfigured, or that you are including this plugin in a project that does not need it. For
* this reason, the plugin defaults this setting to being enabled. If you wish to not fail, you
* can explicitly set this to false instead.
*
* @since 0.5.0
*/
Expand Down Expand Up @@ -310,12 +308,11 @@ public abstract class AbstractGenerateMojo extends AbstractMojo {
private boolean pythonEnabled;

/**
* Enable generating Python stubs ({@code *.pyi} files) for static typechecking
* from the protobuf sources.
* Enable generating Python stubs ({@code *.pyi} files) for static typechecking from the protobuf
* sources.
*
* <p>If you enable this, you probably will also want to enable Python itself
* to get actual source code
* to accompany the stubs.
* to get actual source code to accompany the stubs.
*
* @since 1.1.0
*/
Expand Down Expand Up @@ -384,12 +381,11 @@ public abstract class AbstractGenerateMojo extends AbstractMojo {
private boolean liteOnly;

/**
* Whether to register the output directories as compilation roots with
* Maven.
* Whether to register the output directories as compilation roots with Maven.
*
* <p>Generally, you want to do this, but there may be edge cases where you
* wish to control this behaviour manually instead. In this case, set this
* parameter to be {@code false}.
* wish to control this behaviour manually instead. In this case, set this parameter to be
* {@code false}.
*
* @since 0.5.0
*/
Expand All @@ -410,8 +406,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
throw new MojoExecutionException(ex.getMessage(), ex);
}

//noinspection DataFlowIssue
var request = ImmutableGenerationRequest.builder()
.allowedDependencyScopes(allowedScopes())
.binaryMavenPlugins(nonNullList(binaryMavenPlugins))
.binaryPathPlugins(nonNullList(binaryPathPlugins))
.binaryUrlPlugins(nonNullList(binaryUrlPlugins))
Expand Down Expand Up @@ -499,16 +495,6 @@ private Collection<Path> sourceDirectories() {
*/
protected abstract Path defaultOutputDirectory(MavenSession session);

/**
* Provides the scopes allowed for dependencies.
*
* <p>Dependencies matching one of these scopes will be indexed and made visible
* to the protoc compiler if proto files are discovered.
*
* @return a set of the scopes.
*/
protected abstract Set<String> allowedScopes();

/**
* Validate this Mojo's parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

/**
* Generate source code from protobuf files.
Expand All @@ -42,6 +43,8 @@
@Mojo(
name = "generate",
defaultPhase = LifecyclePhase.GENERATE_SOURCES,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
threadSafe = true
)
public final class MainGenerateMojo extends AbstractGenerateMojo {
Expand All @@ -51,11 +54,6 @@ protected SourceRootRegistrar sourceRootRegistrar() {
return SourceRootRegistrar.MAIN;
}

@Override
protected Set<String> allowedScopes() {
return Set.of("compile", "provided", "system");
}

@Override
protected Path defaultSourceDirectory(MavenSession session) {
return session.getCurrentProject().getBasedir().toPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

/**
* Generate source code from protobuf files for use in tests.
Expand All @@ -47,6 +48,8 @@
@Mojo(
name = "generate-test",
defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES,
requiresDependencyCollection = ResolutionScope.TEST,
requiresDependencyResolution = ResolutionScope.TEST,
threadSafe = true
)
public final class TestGenerateMojo extends AbstractGenerateMojo {
Expand All @@ -56,11 +59,6 @@ protected SourceRootRegistrar sourceRootRegistrar() {
return SourceRootRegistrar.TEST;
}

@Override
protected Set<String> allowedScopes() {
return Set.of("compile", "provided", "system", "test");
}

@Override
protected Path defaultSourceDirectory(MavenSession session) {
return session.getCurrentProject().getBasedir().toPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
@Named
public final class JvmPluginResolver {

private static final Set<String> SCOPES = Set.of("compile", "runtime", "system");

private final HostSystem hostSystem;
private final MavenDependencyPathResolver dependencyPathResolver;
private final TemporarySpace temporarySpace;
Expand Down Expand Up @@ -104,7 +102,6 @@ private List<String> resolveAndBuildArgLine(
var dependencyIterator = dependencyPathResolver
.resolveDependencyTreePaths(
session,
SCOPES,
DependencyResolutionDepth.TRANSITIVE,
plugin
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

package io.github.ascopes.protobufmavenplugin.dependency;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.resolve.ScopeFilter;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
Expand Down Expand Up @@ -56,27 +57,31 @@ public MavenDependencyPathResolver(

public Collection<Path> resolveProjectDependencyPaths(
MavenSession session,
Set<String> allowedScopes,
DependencyResolutionDepth dependencyResolutionDepth
) throws ResolutionException {
var paths = new ArrayList<Path>();

for (var dependency : session.getCurrentProject().getDependencies()) {
var artifact = MavenArtifact.fromDependency(dependency);
paths.addAll(resolveDependencyTreePaths(
session,
allowedScopes,
dependencyResolutionDepth,
artifact
));
if (dependencyResolutionDepth == DependencyResolutionDepth.DIRECT) {
var artifacts = session.getCurrentProject().getDependencies()
.stream()
.map(MavenArtifact::fromDependency)
.collect(Collectors.toList());

var paths = new ArrayList<Path>();
for (var artifact : artifacts) {
paths.add(resolveArtifact(session, artifact));
}
return paths;
}

return paths;
return session.getCurrentProject().getArtifacts()
.stream()
.map(Artifact::getFile)
.map(File::toPath)
.distinct()
.collect(Collectors.toList());
}

public Collection<Path> resolveDependencyTreePaths(
MavenSession session,
Set<String> allowedScopes,
DependencyResolutionDepth dependencyResolutionDepth,
MavenArtifact artifact
) throws ResolutionException {
Expand All @@ -92,11 +97,10 @@ public Collection<Path> resolveDependencyTreePaths(
}

var request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
var scopes = ScopeFilter.including(allowedScopes);
var coordinate = artifact.toDependableCoordinate();

try {
for (var next : dependencyResolver.resolveDependencies(request, coordinate, scopes)) {
for (var next : dependencyResolver.resolveDependencies(request, coordinate, null)) {
allDependencyPaths.add(next.getArtifact().getFile().toPath());
}
} catch (DependencyResolverException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.net.URL;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Set;
import org.apache.maven.execution.MavenSession;
import org.immutables.value.Value.Immutable;

Expand All @@ -45,8 +44,6 @@ public interface GenerationRequest {

Collection<MavenArtifact> getJvmMavenPlugins();

Set<String> getAllowedDependencyScopes();

MavenSession getMavenSession();

Path getOutputDirectory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.github.ascopes.protobufmavenplugin.generate;

import io.github.ascopes.protobufmavenplugin.dependency.BinaryPluginResolver;
import io.github.ascopes.protobufmavenplugin.dependency.DependencyResolutionDepth;
import io.github.ascopes.protobufmavenplugin.dependency.JvmPluginResolver;
import io.github.ascopes.protobufmavenplugin.dependency.MavenDependencyPathResolver;
import io.github.ascopes.protobufmavenplugin.dependency.ProtocResolver;
Expand Down Expand Up @@ -179,7 +178,6 @@ private Collection<ProtoFileListing> discoverImportPaths(

var dependencyPaths = mavenDependencyPathResolver.resolveProjectDependencyPaths(
session,
request.getAllowedDependencyScopes(),
request.getDependencyResolutionDepth()
);

Expand Down

0 comments on commit 1b77c15

Please sign in to comment.