Skip to content

Commit 1f63feb

Browse files
adambkaplanLogFlames
authored andcommitted
🐛 fix: Remove Plugin Test Dependencies
Use an ArtifactFilter to only include compile and runtime scope dependencies. This will exclude all test scope dependencies in the plugin dependency graph. Maven requires plugins to have their compile and runtime dependency tree present in the local Maven repository in order to function. Assisted-by: Cursor Signed-off-by: Adam Kaplan <adam.kaplan@redhat.com>
1 parent 3f838f9 commit 1f63feb

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/LockFileFacade.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.apache.maven.artifact.Artifact;
2525
import org.apache.maven.artifact.DefaultArtifact;
2626
import org.apache.maven.artifact.factory.ArtifactFactory;
27+
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
28+
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
2729
import org.apache.maven.execution.MavenSession;
2830
import org.apache.maven.project.DefaultProjectBuildingRequest;
2931
import org.apache.maven.project.MavenProject;
@@ -283,7 +285,11 @@ private static Set<io.github.chains_project.maven_lockfile.graph.DependencyNode>
283285
dependencyBuildingRequest.setProject(pluginProject);
284286
dependencyBuildingRequest.setRemoteRepositories(project.getPluginArtifactRepositories());
285287

286-
var rootNode = dependencyCollectorBuilder.collectDependencyGraph(dependencyBuildingRequest, null);
288+
// Filter artifacts to "compile+runtime" scopes. Maven plugins require their runtime
289+
// scope dependencies to be present alongside any compile-time dependencies.
290+
// Test scope dependencies of plugins should be excluded.
291+
ArtifactFilter filter = new ScopeArtifactFilter("compile+runtime");
292+
var rootNode = dependencyCollectorBuilder.collectDependencyGraph(dependencyBuildingRequest, filter);
287293

288294
int rootChildren =
289295
rootNode.getChildren() != null ? rootNode.getChildren().size() : 0;

maven_plugin/src/test/java/it/IntegrationTestsIT.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.fail;
56

67
import com.google.common.collect.Ordering;
78
import com.soebes.itf.jupiter.extension.MavenJupiterExtension;
@@ -11,6 +12,7 @@
1112
import io.github.chains_project.maven_lockfile.data.Classifier;
1213
import io.github.chains_project.maven_lockfile.data.GroupId;
1314
import io.github.chains_project.maven_lockfile.data.LockFile;
15+
import io.github.chains_project.maven_lockfile.data.MavenScope;
1416
import io.github.chains_project.maven_lockfile.data.RepositoryId;
1517
import io.github.chains_project.maven_lockfile.data.ResolvedUrl;
1618
import io.github.chains_project.maven_lockfile.data.VersionNumber;
@@ -86,6 +88,7 @@ public void singleDependencyCheckMustFail(MavenExecutionResult result) throws Ex
8688
}
8789

8890
@MavenTest
91+
@SuppressWarnings("null")
8992
public void pluginProject(MavenExecutionResult result) throws Exception {
9093
// contract: if including maven plugins the lockfile should contain these and be able to calculate checksums for
9194
// them. Plugin dependencies should also be resolved and recorded.
@@ -105,6 +108,29 @@ public void pluginProject(MavenExecutionResult result) throws Exception {
105108
assertThat(lockFile.getMavenPlugins())
106109
.allMatch(
107110
p -> p.getDependencies() != null && !p.getDependencies().isEmpty());
111+
112+
lockFile.getMavenPlugins().forEach(plugin -> {
113+
plugin.getDependencies().forEach(dep -> {
114+
var scope = dep.getScope();
115+
if (scope == null) {
116+
fail(String.format(
117+
"scope is null for dependency %s:%s:%s",
118+
dep.getGroupId().getValue(),
119+
dep.getArtifactId().getValue(),
120+
dep.getVersion().getValue()));
121+
return;
122+
}
123+
// Log the offending plugin that includes the unexpected scope.
124+
// Null check warnings are suppressed because the .as() method always generates such a warning.
125+
assertThat(scope)
126+
.as(
127+
"Scope of plugin dependency %s:%s:%s",
128+
dep.getGroupId().getValue(),
129+
dep.getArtifactId().getValue(),
130+
dep.getVersion().getValue())
131+
.isNotEqualTo(MavenScope.TEST);
132+
});
133+
});
108134
}
109135

110136
@MavenTest

0 commit comments

Comments
 (0)