/** * Uses the {@link EnforcerRuleHelper} to populate the values of the {@link * DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, * ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} factory method. <br> * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has * to do. * * @param helper * @return a Dependency Node which is the root of the project's dependency tree * @throws EnforcerRuleException when the build should fail */ private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException { try { MavenProject project = (MavenProject) helper.evaluate("${project}"); DependencyTreeBuilder dependencyTreeBuilder = (DependencyTreeBuilder) helper.getComponent(DependencyTreeBuilder.class); ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}"); ArtifactFactory factory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); ArtifactMetadataSource metadataSource = (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class); ArtifactCollector collector = (ArtifactCollector) helper.getComponent(ArtifactCollector.class); ArtifactFilter filter = null; // we need to evaluate all scopes DependencyNode node = dependencyTreeBuilder.buildDependencyTree( project, repository, factory, metadataSource, filter, collector); return node; } catch (ExpressionEvaluationException e) { throw new EnforcerRuleException( "Unable to lookup an expression " + e.getLocalizedMessage(), e); } catch (ComponentLookupException e) { throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); } catch (DependencyTreeBuilderException e) { throw new EnforcerRuleException( "Could not build dependency tree " + e.getLocalizedMessage(), e); } }
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { if (log == null) { log = helper.getLog(); } try { if (i18n == null) { i18n = (I18N) helper.getComponent(I18N.class); } DependencyNode node = getNode(helper); RequireUpperBoundDepsVisitor visitor = new RequireUpperBoundDepsVisitor(); node.accept(visitor); List<String> errorMessages = buildErrorMessages(visitor.getConflicts()); if (errorMessages.size() > 0) { throw new EnforcerRuleException( "Failed while enforcing RequireUpperBoundDeps. The error(s) are " + errorMessages); } } catch (ComponentLookupException e) { throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); } catch (Exception e) { throw new EnforcerRuleException(e.getLocalizedMessage(), e); } }
@Override public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException { final MavenProject project; try { project = (MavenProject) helper.evaluate("${project}"); } catch (ExpressionEvaluationException e) { throw new EnforcerRuleException("Failed to access ${project} variable", e); } final String type = project.getArtifact().getType(); if (!AbstractEnforcerRule.JAR_ARTIFACT_TYPE.equals(type)) { helper .getLog() .debug("Skipping non " + AbstractEnforcerRule.JAR_ARTIFACT_TYPE + " artifact."); return; } final Artifact previousArtifact; final Artifact currentArtifact = project.getArtifact(); validateArtifact(currentArtifact); final Version current = Version.parse(currentArtifact.getVersion()); final File currentJar = currentArtifact.getFile(); try { final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}"); final String version; if (this.previousVersion != null) { version = this.previousVersion; helper.getLog().info("Version specified as <" + version + ">"); } else { final ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class); final List<ArtifactVersion> availableVersions = getAvailableReleasedVersions(artifactMetadataSource, project, localRepository); final List<ArtifactVersion> availablePreviousVersions = filterNonPreviousVersions(availableVersions, current); if (availablePreviousVersions.isEmpty()) { helper .getLog() .warn("No previously released version. Backward compatibility check not performed."); return; } version = availablePreviousVersions.iterator().next().toString(); helper .getLog() .info( "Version deduced as <" + version + "> (among all availables: " + availablePreviousVersions + ")"); } final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); previousArtifact = artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), version, null, type); final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class); resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository); validateArtifact(previousArtifact); } catch (Exception e) { helper.getLog().warn("Exception while accessing artifacts; skipping check.", e); return; } final Version previous = Version.parse(previousArtifact.getVersion()); final File previousJar = previousArtifact.getFile(); helper.getLog().info("Using <" + previousJar + "> as previous JAR"); helper.getLog().info("Using <" + currentJar + "> as current JAR"); try { final Comparer comparer = new Comparer( previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); final Delta delta = comparer.diff(); enforce(helper, delta, previous, current); } catch (IOException e) { throw new EnforcerRuleException("Exception while checking compatibility: " + e.toString(), e); } }