use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRule method findBomClasspath.
/**
* Builds a class path for {@code bomProject}.
*/
private ClassPathResult findBomClasspath(MavenProject bomProject, RepositorySystemSession repositorySystemSession) throws EnforcerRuleException {
ArtifactTypeRegistry artifactTypeRegistry = repositorySystemSession.getArtifactTypeRegistry();
List<org.apache.maven.model.Dependency> managedDependencies = bomProject.getDependencyManagement().getDependencies();
ImmutableList<Artifact> artifacts = managedDependencies.stream().map(dependency -> RepositoryUtils.toDependency(dependency, artifactTypeRegistry)).map(Dependency::getArtifact).filter(artifact -> !Bom.shouldSkipBomMember(artifact)).collect(toImmutableList());
try {
ClassPathResult result = classPathBuilder.resolve(artifacts, false, DependencyMediation.MAVEN);
ImmutableList<UnresolvableArtifactProblem> artifactProblems = result.getArtifactProblems();
if (!artifactProblems.isEmpty()) {
throw new EnforcerRuleException("Failed to collect dependency: " + artifactProblems);
}
return result;
} catch (InvalidVersionSpecificationException ex) {
throw new EnforcerRuleException("Dependency mediation failed due to invalid version", ex);
}
}
use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRule method findProjectClasspath.
/**
* Builds a class path for {@code mavenProject}.
*/
private static ClassPathResult findProjectClasspath(MavenProject mavenProject, RepositorySystemSession session, EnforcerRuleHelper helper) throws EnforcerRuleException {
try {
ProjectDependenciesResolver projectDependenciesResolver = helper.getComponent(ProjectDependenciesResolver.class);
DefaultRepositorySystemSession fullDependencyResolutionSession = new DefaultRepositorySystemSession(session);
// Clear artifact cache. Certain artifacts in the cache have dependencies without
// ${os.detected.classifier} interpolated. They are instantiated before 'verify' phase:
// https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/925
fullDependencyResolutionSession.setCache(new DefaultRepositoryCache());
// For netty-handler referencing its dependencies with ${os.detected.classifier}
// allowing duplicate entries
Map<String, String> properties = new HashMap<>();
properties.putAll(fullDependencyResolutionSession.getSystemProperties());
properties.putAll(OsProperties.detectOsProperties());
fullDependencyResolutionSession.setSystemProperties(properties);
fullDependencyResolutionSession.setDependencySelector(new AndDependencySelector(new NonTestDependencySelector(), new ExclusionDependencySelector(), new OptionalDependencySelector(), new FilteringZipDependencySelector()));
DependencyResolutionRequest dependencyResolutionRequest = new DefaultDependencyResolutionRequest(mavenProject, fullDependencyResolutionSession);
DependencyResolutionResult resolutionResult = projectDependenciesResolver.resolve(dependencyResolutionRequest);
return buildClassPathResult(resolutionResult);
} catch (ComponentLookupException e) {
throw new EnforcerRuleException("Unable to lookup a component " + e.getMessage(), e);
} catch (DependencyResolutionException e) {
return buildClasspathFromException(e);
}
}
use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRule method buildClassPathResult.
private static ClassPathResult buildClassPathResult(DependencyResolutionResult result) throws EnforcerRuleException {
// The root node must have the project's JAR file
DependencyNode root = result.getDependencyGraph();
File rootFile = root.getArtifact().getFile();
if (rootFile == null) {
throw new EnforcerRuleException("The root project artifact is not associated with a file.");
}
List<Dependency> unresolvedDependencies = result.getUnresolvedDependencies();
Set<Artifact> unresolvedArtifacts = unresolvedDependencies.stream().map(Dependency::getArtifact).collect(toImmutableSet());
DependencyGraph dependencyGraph = DependencyGraph.from(root);
AnnotatedClassPath annotatedClassPath = new AnnotatedClassPath();
ImmutableList.Builder<UnresolvableArtifactProblem> problems = ImmutableList.builder();
for (DependencyPath path : dependencyGraph.list()) {
Artifact artifact = path.getLeaf();
if (unresolvedArtifacts.contains(artifact)) {
problems.add(new UnresolvableArtifactProblem(artifact));
} else {
annotatedClassPath.put(new ClassPathEntry(artifact), path);
}
}
return new ClassPathResult(annotatedClassPath, problems.build());
}
use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRuleTest method testArtifactTransferError.
@Test
public void testArtifactTransferError() throws RepositoryException, DependencyResolutionException {
DependencyNode graph = createResolvedDependencyGraph("org.apache.maven:maven-core:jar:3.5.2");
DependencyResolutionResult resolutionResult = mock(DependencyResolutionResult.class);
when(resolutionResult.getDependencyGraph()).thenReturn(graph);
DependencyResolutionException exception = createDummyResolutionException(new DefaultArtifact("aopalliance:aopalliance:1.0"), resolutionResult);
when(mockProjectDependenciesResolver.resolve(any())).thenThrow(exception);
try {
rule.execute(mockRuleHelper);
fail("The rule should throw EnforcerRuleException upon dependency resolution exception");
} catch (EnforcerRuleException expected) {
verify(mockLog).warn("aopalliance:aopalliance:jar:1.0 was not resolved. " + "Dependency path: a:b:jar:0.1 > " + "org.apache.maven:maven-core:jar:3.5.2 (compile) > " + "com.google.inject:guice:jar:no_aop:4.0 (compile) > " + "aopalliance:aopalliance:jar:1.0 (compile)");
}
}
use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRuleTest method testExecute_shouldFailForBadProjectWithBundlePackaging.
@Test
public void testExecute_shouldFailForBadProjectWithBundlePackaging() throws RepositoryException {
try {
// This artifact is known to contain classes missing dependencies
setupMockDependencyResolution("com.google.appengine:appengine-api-1.0-sdk:1.9.64");
org.apache.maven.artifact.DefaultArtifact rootArtifact = new org.apache.maven.artifact.DefaultArtifact("com.google.cloud", "linkage-checker-rule-test", "0.0.1", "compile", // Maven Bundle Plugin uses "bundle" packaging.
"bundle", null, new DefaultArtifactHandler());
rootArtifact.setFile(new File("dummy.jar"));
when(mockProject.getArtifact()).thenReturn(rootArtifact);
rule.execute(mockRuleHelper);
Assert.fail("The rule should raise an EnforcerRuleException for artifacts missing dependencies");
} catch (EnforcerRuleException ex) {
// Java 11 removed javax.activation package. Therefore the number of expected errors differs
// between Java 8 and Java 11.
// https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/1856
int expectedErrorCount = System.getProperty("java.version").startsWith("1.8.") ? 112 : 117;
// pass
verify(mockLog).error(ArgumentMatchers.startsWith("Linkage Checker rule found " + expectedErrorCount + " errors:"));
assertEquals("Failed while checking class path. See above error report.", ex.getMessage());
}
}
Aggregations