CommonsStagingCleanupMojo.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.release.plugin.mojos;

  18. import java.io.File;
  19. import java.util.Arrays;
  20. import java.util.List;

  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.commons.release.plugin.SharedFunctions;
  23. import org.apache.maven.plugin.AbstractMojo;
  24. import org.apache.maven.plugin.MojoExecutionException;
  25. import org.apache.maven.plugin.MojoFailureException;
  26. import org.apache.maven.plugins.annotations.Component;
  27. import org.apache.maven.plugins.annotations.LifecyclePhase;
  28. import org.apache.maven.plugins.annotations.Mojo;
  29. import org.apache.maven.plugins.annotations.Parameter;
  30. import org.apache.maven.project.MavenProject;
  31. import org.apache.maven.scm.ScmException;
  32. import org.apache.maven.scm.ScmFileSet;
  33. import org.apache.maven.scm.command.checkin.CheckInScmResult;
  34. import org.apache.maven.scm.command.checkout.CheckOutScmResult;
  35. import org.apache.maven.scm.command.remove.RemoveScmResult;
  36. import org.apache.maven.scm.manager.BasicScmManager;
  37. import org.apache.maven.scm.manager.ScmManager;
  38. import org.apache.maven.scm.provider.ScmProvider;
  39. import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
  40. import org.apache.maven.scm.provider.svn.svnexe.SvnExeScmProvider;
  41. import org.apache.maven.scm.repository.ScmRepository;
  42. import org.apache.maven.settings.Settings;
  43. import org.apache.maven.settings.crypto.SettingsDecrypter;

  44. /**
  45.  * This class checks out the dev distribution location, checks whether anything exists in the
  46.  * distribution location, and if it is non-empty it deletes all the resources there.
  47.  *
  48.  * @since 1.6
  49.  */
  50. @Mojo(name = "clean-staging",
  51.         defaultPhase = LifecyclePhase.POST_CLEAN,
  52.         threadSafe = true,
  53.         aggregator = true)
  54. public class CommonsStagingCleanupMojo extends AbstractMojo {

  55.     /**
  56.      * The {@link MavenProject} object is essentially the context of the maven build at
  57.      * a given time.
  58.      */
  59.     @Parameter(defaultValue = "${project}", required = true)
  60.     private MavenProject project;

  61.     /**
  62.      * The main working directory for the plugin, namely <code>target/commons-release-plugin</code>, but
  63.      * that assumes that we're using the default maven <code>${project.build.directory}</code>.
  64.      */
  65.     @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin", property = "commons.outputDirectory")
  66.     private File workingDirectory;

  67.     /**
  68.      * The location to which to checkout the dist subversion repository under our working directory, which
  69.      * was given above. We then do an SVN delete on all the directories in this repository.
  70.      */
  71.     @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin/scm-cleanup",
  72.             property = "commons.distCleanupDirectory")
  73.     private File distCleanupDirectory;

  74.     /**
  75.      * A boolean that determines whether or not we actually commit the files up to the subversion repository.
  76.      * If this is set to {@code true}, we do all but make the commits. We do checkout the repository in question
  77.      * though.
  78.      */
  79.     @Parameter(property = "commons.release.dryRun", defaultValue = "false")
  80.     private Boolean dryRun;

  81.     /**
  82.      * The url of the subversion repository to which we wish the artifacts to be staged. Typically this would need to
  83.      * be of the form: <code>scm:svn:https://dist.apache.org/repos/dist/dev/commons/foo/version-RC#</code>. Note. that
  84.      * the prefix to the substring <code>https</code> is a requirement.
  85.      */
  86.     @Parameter(defaultValue = "", property = "commons.distSvnStagingUrl")
  87.     private String distSvnStagingUrl;

  88.     /**
  89.      * A parameter to generally avoid running unless it is specifically turned on by the consuming module.
  90.      */
  91.     @Parameter(defaultValue = "false", property = "commons.release.isDistModule")
  92.     private Boolean isDistModule;

  93.     /**
  94.      * The ID of the server (specified in settings.xml) which should be used for dist authentication.
  95.      * This will be used in preference to {@link #username}/{@link #password}.
  96.      */
  97.     @Parameter(property = "commons.distServer")
  98.     private String distServer;

  99.     /**
  100.      * The username for the distribution subversion repository. This is typically your Apache id.
  101.      */
  102.     @Parameter(property = "user.name")
  103.     private String username;

  104.     /**
  105.      * The password associated with {@link CommonsDistributionStagingMojo#username}.
  106.      */
  107.     @Parameter(property = "user.password")
  108.     private String password;

  109.     /**
  110.      * Maven {@link Settings}.
  111.      */
  112.     @Parameter(defaultValue = "${settings}", readonly = true, required = true)
  113.     private Settings settings;

  114.     /**
  115.      * Maven {@link SettingsDecrypter} component.
  116.      */
  117.     @Component
  118.     private SettingsDecrypter settingsDecrypter;

  119.     @Override
  120.     public void execute() throws MojoExecutionException, MojoFailureException {
  121.         if (!isDistModule) {
  122.             getLog().info("This module is marked as a non distribution "
  123.                     + "or assembly module, and the plugin will not run.");
  124.             return;
  125.         }
  126.         if (StringUtils.isEmpty(distSvnStagingUrl)) {
  127.             getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
  128.             return;
  129.         }
  130.         if (!workingDirectory.exists()) {
  131.             SharedFunctions.initDirectory(getLog(), workingDirectory);
  132.         }
  133.         try {
  134.             final ScmManager scmManager = new BasicScmManager();
  135.             scmManager.setScmProvider("svn", new SvnExeScmProvider());
  136.             final ScmRepository repository = scmManager.makeScmRepository(distSvnStagingUrl);
  137.             final ScmProvider provider = scmManager.getProviderByRepository(repository);
  138.             final SvnScmProviderRepository providerRepository = (SvnScmProviderRepository) repository
  139.                     .getProviderRepository();
  140.             SharedFunctions.setAuthentication(
  141.                     providerRepository,
  142.                     distServer,
  143.                     settings,
  144.                     settingsDecrypter,
  145.                     username,
  146.                     password
  147.             );
  148.             getLog().info("Checking out dist from: " + distSvnStagingUrl);
  149.             final ScmFileSet scmFileSet = new ScmFileSet(distCleanupDirectory);
  150.             final CheckOutScmResult checkOutResult = provider.checkOut(repository, scmFileSet);
  151.             if (!checkOutResult.isSuccess()) {
  152.                 throw new MojoExecutionException("Failed to checkout files from SCM: "
  153.                         + checkOutResult.getProviderMessage() + " [" + checkOutResult.getCommandOutput() + "]");
  154.             }
  155.             final List<File> filesToRemove = Arrays.asList(distCleanupDirectory.listFiles());
  156.             if (filesToRemove.size() == 1) {
  157.                 getLog().info("No files to delete");
  158.                 return;
  159.             }
  160.             if (!dryRun) {
  161.                 final ScmFileSet fileSet = new ScmFileSet(distCleanupDirectory, filesToRemove);
  162.                 final RemoveScmResult removeScmResult = provider.remove(repository, fileSet,
  163.                         "Cleaning up staging area");
  164.                 if (!removeScmResult.isSuccess()) {
  165.                     throw new MojoFailureException("Failed to remove files from SCM: "
  166.                             + removeScmResult.getProviderMessage()
  167.                             + " [" + removeScmResult.getCommandOutput() + "]");
  168.                 }
  169.                 getLog().info("Cleaning distribution area for: " + project.getArtifactId());
  170.                 final CheckInScmResult checkInResult = provider.checkIn(
  171.                         repository,
  172.                         fileSet,
  173.                         "Cleaning distribution area for: " + project.getArtifactId()
  174.                 );
  175.                 if (!checkInResult.isSuccess()) {
  176.                     throw new MojoFailureException("Failed to commit files: " + removeScmResult.getProviderMessage()
  177.                             + " [" + removeScmResult.getCommandOutput() + "]");
  178.                 }
  179.             } else {
  180.                 getLog().info("Would have attempted to delete files from: " + distSvnStagingUrl);
  181.             }
  182.         } catch (final ScmException e) {
  183.             throw new MojoFailureException(e.getMessage());
  184.         }

  185.     }
  186. }