コード例 #1
0
 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);
    }
  }