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 buildClasspathFromException.
/**
* Returns class path built from partial dependency graph of {@code resolutionException}.
*/
private static ClassPathResult buildClasspathFromException(DependencyResolutionException resolutionException) throws EnforcerRuleException {
DependencyResolutionResult result = resolutionException.getResult();
for (Throwable cause = resolutionException.getCause(); cause != null; cause = cause.getCause()) {
if (cause instanceof ArtifactTransferException) {
DependencyNode root = result.getDependencyGraph();
DependencyGraph graph = new DependencyGraph(root);
ArtifactTransferException artifactException = (ArtifactTransferException) cause;
Artifact artifact = artifactException.getArtifact();
String warning = graph.createUnresolvableArtifactProblem(artifact).toString();
logger.warn(warning);
break;
}
}
if (result.getResolvedDependencies().isEmpty()) {
// Nothing is resolved. Probably failed at collection phase before resolve phase.
throw new EnforcerRuleException("Unable to collect dependencies", resolutionException);
} else {
// The exception is acceptable enough to build a class path.
return buildClassPathResult(result);
}
}
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());
}
}
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 jersey by jersey.
the class FilePatternDoesNotExistRule method execute.
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
if (files == null) {
return;
}
for (File file : files) {
final String filePath = file.getPath();
final String prefix = filePath.substring(0, filePath.indexOf("*"));
final String dirName = prefix.substring(0, prefix.lastIndexOf(File.separator));
final String fileItselfPattern = filePath.substring(dirName.length() + 1);
final File dir = new File(dirName);
if (!dir.isDirectory()) {
continue;
}
final Set<File> matchedFiles = new TreeSet<>();
for (File matchedFile : dir.listFiles((FileFilter) new WildcardFileFilter(fileItselfPattern))) {
matchedFiles.add(matchedFile);
}
if (!matchedFiles.isEmpty()) {
throw new EnforcerRuleException("Files found! " + Arrays.toString(matchedFiles.toArray()));
}
}
}
Aggregations