@Test public void testArtifactTransferError_missingArtifactNotInGraph() throws URISyntaxException, DependencyResolutionException, EnforcerRuleException { DefaultDependencyNode child2 = new DefaultDependencyNode( new Dependency( createArtifactWithDummyFile("com.google.foo:child2:1.0.0"), "compile", true)); DefaultDependencyNode child1 = new DefaultDependencyNode( new Dependency(createArtifactWithDummyFile("com.google.foo:child1:1.0.0"), "provided")); child1.setChildren(ImmutableList.of(child2)); DefaultDependencyNode root = new DefaultDependencyNode(createArtifactWithDummyFile("com.google.foo:project:1.0.0")); root.setChildren(ImmutableList.of(child1)); DependencyResolutionResult resolutionResult = mock(DependencyResolutionResult.class); when(resolutionResult.getDependencyGraph()).thenReturn(root); when(resolutionResult.getResolvedDependencies()) .thenReturn(ImmutableList.of(child1.getDependency(), child2.getDependency())); DefaultDependencyNode missingArtifactNode = new DefaultDependencyNode( new Dependency( createArtifactWithDummyFile("xerces:xerces-impl:jar:2.6.2"), "compile", true)); DependencyResolutionException exception = createDummyResolutionException(missingArtifactNode.getArtifact(), resolutionResult); when(mockProjectDependenciesResolver.resolve(any())).thenThrow(exception); rule.execute(mockRuleHelper); verify(mockLog) .warn("xerces:xerces-impl:jar:2.6.2 was not resolved. Dependency path is unknown."); }
@Override public void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException { logger = helper.getLog(); try { MavenProject project = (MavenProject) helper.evaluate("${project}"); MavenSession session = (MavenSession) helper.evaluate("${session}"); MojoExecution execution = (MojoExecution) helper.evaluate("${mojoExecution}"); RepositorySystemSession repositorySystemSession = session.getRepositorySession(); ImmutableList<String> repositoryUrls = project.getRemoteProjectRepositories().stream() .map(RemoteRepository::getUrl) .collect(toImmutableList()); DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(repositoryUrls); classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder); boolean readingDependencyManagementSection = dependencySection == DependencySection.DEPENDENCY_MANAGEMENT; if (readingDependencyManagementSection && (project.getDependencyManagement() == null || project.getDependencyManagement().getDependencies() == null || project.getDependencyManagement().getDependencies().isEmpty())) { logger.warn("The rule is set to read dependency management section but it is empty."); } String projectType = project.getArtifact().getType(); if (readingDependencyManagementSection) { if (!"pom".equals(projectType)) { logger.warn("A BOM should have packaging pom"); return; } } else { if (UNSUPPORTED_NONBOM_PACKAGING.contains(projectType)) { return; } if (!"verify".equals(execution.getLifecyclePhase())) { throw new EnforcerRuleException( "To run the check on the compiled class files, the linkage checker enforcer rule" + " should be bound to the 'verify' phase. Current phase: " + execution.getLifecyclePhase()); } if (project.getArtifact().getFile() == null) { return; } } ClassPathResult classPathResult = readingDependencyManagementSection ? findBomClasspath(project, repositorySystemSession) : findProjectClasspath(project, repositorySystemSession, helper); ImmutableList<ClassPathEntry> classPath = classPathResult.getClassPath(); if (classPath.isEmpty()) { logger.warn("Class path is empty."); return; } List<org.apache.maven.model.Dependency> dependencies = project.getDependencies(); long projectDependencyCount = dependencies.stream().filter(dependency -> !"test".equals(dependency.getScope())).count(); List<ClassPathEntry> entryPoints = classPath.subList(0, (int) projectDependencyCount + 1); try { Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile); LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile); ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems(); if (reportOnlyReachable) { ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph(); linkageProblems = linkageProblems.stream() .filter( entry -> classReferenceGraph.isReachable(entry.getSourceClass().getBinaryName())) .collect(toImmutableSet()); } if (classPathResult != null) { LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems); } long errorCount = linkageProblems.stream().map(LinkageProblem::formatSymbolProblem).distinct().count(); String foundError = reportOnlyReachable ? "reachable error" : "error"; if (errorCount > 1) { foundError += "s"; } if (errorCount > 0) { String message = String.format( "Linkage Checker rule found %d %s:\n%s", errorCount, foundError, LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult)); if (getLevel() == WARN) { logger.warn(message); } else { logger.error(message); logger.info( "For the details of the linkage errors, see " + "https: throw new EnforcerRuleException( "Failed while checking class path. See above error report."); } } else { logger.info("No " + foundError + " found"); } } catch (IOException ex) { logger.warn("Failed to run Linkage Checker", ex); return; } } catch (ExpressionEvaluationException ex) { throw new EnforcerRuleException("Unable to lookup an expression " + ex.getMessage(), ex); } }
LinkageCheckerRule extends AbstractNonCacheableEnforcerRule { @Override public void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException { logger = helper.getLog(); try { MavenProject project = (MavenProject) helper.evaluate("${project}"); MavenSession session = (MavenSession) helper.evaluate("${session}"); MojoExecution execution = (MojoExecution) helper.evaluate("${mojoExecution}"); RepositorySystemSession repositorySystemSession = session.getRepositorySession(); ImmutableList<String> repositoryUrls = project.getRemoteProjectRepositories().stream() .map(RemoteRepository::getUrl) .collect(toImmutableList()); DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(repositoryUrls); classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder); boolean readingDependencyManagementSection = dependencySection == DependencySection.DEPENDENCY_MANAGEMENT; if (readingDependencyManagementSection && (project.getDependencyManagement() == null || project.getDependencyManagement().getDependencies() == null || project.getDependencyManagement().getDependencies().isEmpty())) { logger.warn("The rule is set to read dependency management section but it is empty."); } String projectType = project.getArtifact().getType(); if (readingDependencyManagementSection) { if (!"pom".equals(projectType)) { logger.warn("A BOM should have packaging pom"); return; } } else { if (UNSUPPORTED_NONBOM_PACKAGING.contains(projectType)) { return; } if (!"verify".equals(execution.getLifecyclePhase())) { throw new EnforcerRuleException( "To run the check on the compiled class files, the linkage checker enforcer rule" + " should be bound to the 'verify' phase. Current phase: " + execution.getLifecyclePhase()); } if (project.getArtifact().getFile() == null) { return; } } ClassPathResult classPathResult = readingDependencyManagementSection ? findBomClasspath(project, repositorySystemSession) : findProjectClasspath(project, repositorySystemSession, helper); ImmutableList<ClassPathEntry> classPath = classPathResult.getClassPath(); if (classPath.isEmpty()) { logger.warn("Class path is empty."); return; } List<org.apache.maven.model.Dependency> dependencies = project.getDependencies(); long projectDependencyCount = dependencies.stream().filter(dependency -> !"test".equals(dependency.getScope())).count(); List<ClassPathEntry> entryPoints = classPath.subList(0, (int) projectDependencyCount + 1); try { Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile); LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile); ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems(); if (reportOnlyReachable) { ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph(); linkageProblems = linkageProblems.stream() .filter( entry -> classReferenceGraph.isReachable(entry.getSourceClass().getBinaryName())) .collect(toImmutableSet()); } if (classPathResult != null) { LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems); } long errorCount = linkageProblems.stream().map(LinkageProblem::formatSymbolProblem).distinct().count(); String foundError = reportOnlyReachable ? "reachable error" : "error"; if (errorCount > 1) { foundError += "s"; } if (errorCount > 0) { String message = String.format( "Linkage Checker rule found %d %s:\n%s", errorCount, foundError, LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult)); if (getLevel() == WARN) { logger.warn(message); } else { logger.error(message); logger.info( "For the details of the linkage errors, see " + "https: throw new EnforcerRuleException( "Failed while checking class path. See above error report."); } } else { logger.info("No " + foundError + " found"); } } catch (IOException ex) { logger.warn("Failed to run Linkage Checker", ex); return; } } catch (ExpressionEvaluationException ex) { throw new EnforcerRuleException("Unable to lookup an expression " + ex.getMessage(), ex); } } }
LinkageCheckerRule extends AbstractNonCacheableEnforcerRule { @Override public void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException { logger = helper.getLog(); try { MavenProject project = (MavenProject) helper.evaluate("${project}"); MavenSession session = (MavenSession) helper.evaluate("${session}"); MojoExecution execution = (MojoExecution) helper.evaluate("${mojoExecution}"); RepositorySystemSession repositorySystemSession = session.getRepositorySession(); ImmutableList<String> repositoryUrls = project.getRemoteProjectRepositories().stream() .map(RemoteRepository::getUrl) .collect(toImmutableList()); DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(repositoryUrls); classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder); boolean readingDependencyManagementSection = dependencySection == DependencySection.DEPENDENCY_MANAGEMENT; if (readingDependencyManagementSection && (project.getDependencyManagement() == null || project.getDependencyManagement().getDependencies() == null || project.getDependencyManagement().getDependencies().isEmpty())) { logger.warn("The rule is set to read dependency management section but it is empty."); } String projectType = project.getArtifact().getType(); if (readingDependencyManagementSection) { if (!"pom".equals(projectType)) { logger.warn("A BOM should have packaging pom"); return; } } else { if (UNSUPPORTED_NONBOM_PACKAGING.contains(projectType)) { return; } if (!"verify".equals(execution.getLifecyclePhase())) { throw new EnforcerRuleException( "To run the check on the compiled class files, the linkage checker enforcer rule" + " should be bound to the 'verify' phase. Current phase: " + execution.getLifecyclePhase()); } if (project.getArtifact().getFile() == null) { return; } } ClassPathResult classPathResult = readingDependencyManagementSection ? findBomClasspath(project, repositorySystemSession) : findProjectClasspath(project, repositorySystemSession, helper); ImmutableList<ClassPathEntry> classPath = classPathResult.getClassPath(); if (classPath.isEmpty()) { logger.warn("Class path is empty."); return; } List<org.apache.maven.model.Dependency> dependencies = project.getDependencies(); long projectDependencyCount = dependencies.stream().filter(dependency -> !"test".equals(dependency.getScope())).count(); List<ClassPathEntry> entryPoints = classPath.subList(0, (int) projectDependencyCount + 1); try { Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile); LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile); ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems(); if (reportOnlyReachable) { ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph(); linkageProblems = linkageProblems.stream() .filter( entry -> classReferenceGraph.isReachable(entry.getSourceClass().getBinaryName())) .collect(toImmutableSet()); } if (classPathResult != null) { LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems); } long errorCount = linkageProblems.stream().map(LinkageProblem::formatSymbolProblem).distinct().count(); String foundError = reportOnlyReachable ? "reachable error" : "error"; if (errorCount > 1) { foundError += "s"; } if (errorCount > 0) { String message = String.format( "Linkage Checker rule found %d %s:\n%s", errorCount, foundError, LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult)); if (getLevel() == WARN) { logger.warn(message); } else { logger.error(message); logger.info( "For the details of the linkage errors, see " + "https: throw new EnforcerRuleException( "Failed while checking class path. See above error report."); } } else { logger.info("No " + foundError + " found"); } } catch (IOException ex) { logger.warn("Failed to run Linkage Checker", ex); return; } } catch (ExpressionEvaluationException ex) { throw new EnforcerRuleException("Unable to lookup an expression " + ex.getMessage(), ex); } }  }
LinkageCheckerRule extends AbstractNonCacheableEnforcerRule { @Override public void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException { logger = helper.getLog(); try { MavenProject project = (MavenProject) helper.evaluate("${project}"); MavenSession session = (MavenSession) helper.evaluate("${session}"); MojoExecution execution = (MojoExecution) helper.evaluate("${mojoExecution}"); RepositorySystemSession repositorySystemSession = session.getRepositorySession(); ImmutableList<String> repositoryUrls = project.getRemoteProjectRepositories().stream() .map(RemoteRepository::getUrl) .collect(toImmutableList()); DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(repositoryUrls); classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder); boolean readingDependencyManagementSection = dependencySection == DependencySection.DEPENDENCY_MANAGEMENT; if (readingDependencyManagementSection && (project.getDependencyManagement() == null || project.getDependencyManagement().getDependencies() == null || project.getDependencyManagement().getDependencies().isEmpty())) { logger.warn("The rule is set to read dependency management section but it is empty."); } String projectType = project.getArtifact().getType(); if (readingDependencyManagementSection) { if (!"pom".equals(projectType)) { logger.warn("A BOM should have packaging pom"); return; } } else { if (UNSUPPORTED_NONBOM_PACKAGING.contains(projectType)) { return; } if (!"verify".equals(execution.getLifecyclePhase())) { throw new EnforcerRuleException( "To run the check on the compiled class files, the linkage checker enforcer rule" + " should be bound to the 'verify' phase. Current phase: " + execution.getLifecyclePhase()); } if (project.getArtifact().getFile() == null) { return; } } ClassPathResult classPathResult = readingDependencyManagementSection ? findBomClasspath(project, repositorySystemSession) : findProjectClasspath(project, repositorySystemSession, helper); ImmutableList<ClassPathEntry> classPath = classPathResult.getClassPath(); if (classPath.isEmpty()) { logger.warn("Class path is empty."); return; } List<org.apache.maven.model.Dependency> dependencies = project.getDependencies(); long projectDependencyCount = dependencies.stream().filter(dependency -> !"test".equals(dependency.getScope())).count(); List<ClassPathEntry> entryPoints = classPath.subList(0, (int) projectDependencyCount + 1); try { Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile); LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile); ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems(); if (reportOnlyReachable) { ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph(); linkageProblems = linkageProblems.stream() .filter( entry -> classReferenceGraph.isReachable(entry.getSourceClass().getBinaryName())) .collect(toImmutableSet()); } if (classPathResult != null) { LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems); } long errorCount = linkageProblems.stream().map(LinkageProblem::formatSymbolProblem).distinct().count(); String foundError = reportOnlyReachable ? "reachable error" : "error"; if (errorCount > 1) { foundError += "s"; } if (errorCount > 0) { String message = String.format( "Linkage Checker rule found %d %s:\n%s", errorCount, foundError, LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult)); if (getLevel() == WARN) { logger.warn(message); } else { logger.error(message); logger.info( "For the details of the linkage errors, see " + "https: throw new EnforcerRuleException( "Failed while checking class path. See above error report."); } } else { logger.info("No " + foundError + " found"); } } catch (IOException ex) { logger.warn("Failed to run Linkage Checker", ex); return; } } catch (ExpressionEvaluationException ex) { throw new EnforcerRuleException("Unable to lookup an expression " + ex.getMessage(), ex); } }  @Override void execute(@Nonnull EnforcerRuleHelper helper); }
LinkageCheckerRule extends AbstractNonCacheableEnforcerRule { @Override public void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException { logger = helper.getLog(); try { MavenProject project = (MavenProject) helper.evaluate("${project}"); MavenSession session = (MavenSession) helper.evaluate("${session}"); MojoExecution execution = (MojoExecution) helper.evaluate("${mojoExecution}"); RepositorySystemSession repositorySystemSession = session.getRepositorySession(); ImmutableList<String> repositoryUrls = project.getRemoteProjectRepositories().stream() .map(RemoteRepository::getUrl) .collect(toImmutableList()); DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(repositoryUrls); classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder); boolean readingDependencyManagementSection = dependencySection == DependencySection.DEPENDENCY_MANAGEMENT; if (readingDependencyManagementSection && (project.getDependencyManagement() == null || project.getDependencyManagement().getDependencies() == null || project.getDependencyManagement().getDependencies().isEmpty())) { logger.warn("The rule is set to read dependency management section but it is empty."); } String projectType = project.getArtifact().getType(); if (readingDependencyManagementSection) { if (!"pom".equals(projectType)) { logger.warn("A BOM should have packaging pom"); return; } } else { if (UNSUPPORTED_NONBOM_PACKAGING.contains(projectType)) { return; } if (!"verify".equals(execution.getLifecyclePhase())) { throw new EnforcerRuleException( "To run the check on the compiled class files, the linkage checker enforcer rule" + " should be bound to the 'verify' phase. Current phase: " + execution.getLifecyclePhase()); } if (project.getArtifact().getFile() == null) { return; } } ClassPathResult classPathResult = readingDependencyManagementSection ? findBomClasspath(project, repositorySystemSession) : findProjectClasspath(project, repositorySystemSession, helper); ImmutableList<ClassPathEntry> classPath = classPathResult.getClassPath(); if (classPath.isEmpty()) { logger.warn("Class path is empty."); return; } List<org.apache.maven.model.Dependency> dependencies = project.getDependencies(); long projectDependencyCount = dependencies.stream().filter(dependency -> !"test".equals(dependency.getScope())).count(); List<ClassPathEntry> entryPoints = classPath.subList(0, (int) projectDependencyCount + 1); try { Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile); LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile); ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems(); if (reportOnlyReachable) { ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph(); linkageProblems = linkageProblems.stream() .filter( entry -> classReferenceGraph.isReachable(entry.getSourceClass().getBinaryName())) .collect(toImmutableSet()); } if (classPathResult != null) { LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems); } long errorCount = linkageProblems.stream().map(LinkageProblem::formatSymbolProblem).distinct().count(); String foundError = reportOnlyReachable ? "reachable error" : "error"; if (errorCount > 1) { foundError += "s"; } if (errorCount > 0) { String message = String.format( "Linkage Checker rule found %d %s:\n%s", errorCount, foundError, LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult)); if (getLevel() == WARN) { logger.warn(message); } else { logger.error(message); logger.info( "For the details of the linkage errors, see " + "https: throw new EnforcerRuleException( "Failed while checking class path. See above error report."); } } else { logger.info("No " + foundError + " found"); } } catch (IOException ex) { logger.warn("Failed to run Linkage Checker", ex); return; } } catch (ExpressionEvaluationException ex) { throw new EnforcerRuleException("Unable to lookup an expression " + ex.getMessage(), ex); } }  @Override void execute(@Nonnull EnforcerRuleHelper helper);  }