Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-135: Add ability to exclude project dependencies from import paths #154

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ public abstract class AbstractGenerateMojo extends AbstractMojo {
@Parameter(defaultValue = "true")
private boolean registerAsCompilationRoot;

/**
* Whether to ignore the {@code <dependencies/>} blocks in the Maven project when discovering
* {@code *.proto} files to add to the import paths.
*
* <p>Generally you will want to leave this enabled unless you have a very specific case where
* you wish to take control of how dependency resolution works.
*
* @since 1.2.0
*/
@Parameter(defaultValue = "false")
private boolean ignoreProjectDependencies;

/**
* Execute the plugin and generate sources.
*
Expand Down Expand Up @@ -426,6 +438,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.isCsharpEnabled(csharpEnabled)
.isFailOnMissingSources(failOnMissingSources)
.isFatalWarnings(fatalWarnings)
.isIgnoreProjectDependencies(ignoreProjectDependencies)
.isJavaEnabled(javaEnabled)
.isKotlinEnabled(kotlinEnabled)
.isLiteEnabled(liteOnly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public interface GenerationRequest {

boolean isFatalWarnings();

boolean isIgnoreProjectDependencies();

boolean isJavaEnabled();

boolean isKotlinEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -174,40 +175,39 @@
GenerationRequest request
) throws IOException, ResolutionException {
var session = request.getMavenSession();
var importPaths = new ArrayList<ProtoFileListing>();

if (!request.getImportPaths().isEmpty()) {
log.debug(

Check warning on line 181 in src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java#L181

Added line #L181 was not covered by tests
"Finding importable protobuf sources from explicitly provided import paths ({})",
request.getDependencyResolutionDepth()

Check warning on line 183 in src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java#L183

Added line #L183 was not covered by tests
);
importPaths.addAll(protoListingResolver.createProtoFileListings(request.getImportPaths()));

Check warning on line 185 in src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java#L185

Added line #L185 was not covered by tests
}

log.debug(
"Finding importable protobuf sources from the classpath ({})",
request.getDependencyResolutionDepth()
);

var sourceDependencyPaths = mavenDependencyPathResolver.resolveAll(
session,
request.getSourceDependencies(),
request.getDependencyResolutionDepth()
);

var sourceDependencyPathListings = protoListingResolver.createProtoFileListings(
sourceDependencyPaths
);

var projectDependencyPaths = mavenProjectDependencyPathResolver.resolveProjectDependencies(
session,
request.getDependencyResolutionDepth()
);

var inheritedDependencies = protoListingResolver
.createProtoFileListings(projectDependencyPaths);
if (!request.getSourceDependencies().isEmpty()) {
log.debug(
"Finding importable protobuf sources from explicitly provided source dependencies ({})",
request.getSourceDependencies()
);
var sourceDependencyPaths = mavenDependencyPathResolver.resolveAll(
session,
request.getSourceDependencies(),
request.getDependencyResolutionDepth()
);
importPaths.addAll(protoListingResolver.createProtoFileListings(sourceDependencyPaths));
}

// Always use all provided additional import paths, as we assume they are valid given the user
// has explicitly included them in their configuration.
var explicitDependencies = protoListingResolver
.createProtoFileListings(request.getImportPaths());
if (!request.isIgnoreProjectDependencies()) {
log.debug("Finding importable protobuf sources from project dependencies");
var projectDependencyPaths = mavenProjectDependencyPathResolver.resolveProjectDependencies(
session,
request.getDependencyResolutionDepth()
);
importPaths.addAll(protoListingResolver.createProtoFileListings(projectDependencyPaths));
}

return concat(
inheritedDependencies,
explicitDependencies,
sourceDependencyPathListings
);
return importPaths;
}

private Collection<ProtoFileListing> discoverCompilableSources(
Expand Down