    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.release.plugin.mojos;
    018
    019import org.apache.commons.io.FileUtils;
    020import org.apache.commons.lang3.StringUtils;
    021import org.apache.commons.release.plugin.SharedFunctions;
    022import org.apache.commons.release.plugin.velocity.HeaderHtmlVelocityDelegate;
    023import org.apache.commons.release.plugin.velocity.ReadmeHtmlVelocityDelegate;
    024import org.apache.maven.plugin.AbstractMojo;
    025import org.apache.maven.plugin.MojoExecutionException;
    026import org.apache.maven.plugin.MojoFailureException;
    027import org.apache.maven.plugin.logging.Log;
    028import org.apache.maven.plugins.annotations.Component;
    029import org.apache.maven.plugins.annotations.LifecyclePhase;
    030import org.apache.maven.plugins.annotations.Mojo;
    031import org.apache.maven.plugins.annotations.Parameter;
    032import org.apache.maven.project.MavenProject;
    033import org.apache.maven.scm.ScmException;
    034import org.apache.maven.scm.ScmFileSet;
    035import org.apache.maven.scm.command.add.AddScmResult;
    036import org.apache.maven.scm.command.checkin.CheckInScmResult;
    037import org.apache.maven.scm.command.checkout.CheckOutScmResult;
    038import org.apache.maven.scm.manager.BasicScmManager;
    039import org.apache.maven.scm.manager.ScmManager;
    040import org.apache.maven.scm.provider.ScmProvider;
    041import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
    042import org.apache.maven.scm.provider.svn.svnexe.SvnExeScmProvider;
    043import org.apache.maven.scm.repository.ScmRepository;
    044import org.apache.maven.settings.Settings;
    045import org.apache.maven.settings.crypto.SettingsDecrypter;
    046
    047import java.io.File;
    048import java.io.FileOutputStream;
    049import java.io.IOException;
    050import java.io.OutputStreamWriter;
    051import java.io.Writer;
    052import java.util.ArrayList;
    053import java.util.Arrays;
    054import java.util.List;
    055
    056/**
    057 * This class checks out the dev distribution location, copies the distributions into that directory
    058 * structure under the <code>target/commons-release-plugin/scm</code> directory. Then commits the
    059 * distributions back up to SVN. Also, we include the built and zipped site as well as the RELEASE-NOTES.txt.
    060 *
    061 * @author chtompki
    062 * @since 1.0
    063 */
    064@Mojo(name = "stage-distributions",
    065        defaultPhase = LifecyclePhase.DEPLOY,
    066        threadSafe = true,
    067        aggregator = true)
    068public class CommonsDistributionStagingMojo extends AbstractMojo {
    069
    070    /** The name of file generated from the README.vm velocity template to be checked into the dist svn repo. */
    071    private static final String README_FILE_NAME = "README.html";
    072    /** The name of file generated from the HEADER.vm velocity template to be checked into the dist svn repo. */
    073    private static final String HEADER_FILE_NAME = "HEADER.html";
    074
    075    /**
    076     * The {@link MavenProject} object is essentially the context of the maven build at
    077     * a given time.
    078     */
    079    @Parameter(defaultValue = "${project}", required = true)
    080    private MavenProject project;
    081
    082    /**
    083     * The {@link File} that contains a file to the root directory of the working project. Typically
    084     * this directory is where the <code>pom.xml</code> resides.
    085     */
    086    @Parameter(defaultValue = "${basedir}")
    087    private File baseDir;
    088
    089    /** The location to which the site gets built during running <code>mvn site</code>. */
    090    @Parameter(defaultValue = "${project.build.directory}/site", property = "commons.siteOutputDirectory")
    091    private File siteDirectory;
    092
    093    /**
    094     * The main working directory for the plugin, namely <code>target/commons-release-plugin</code>, but
    095     * that assumes that we're using the default maven <code>${project.build.directory}</code>.
    096     */
    097    @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin", property = "commons.outputDirectory")
    098    private File workingDirectory;
    099
    100    /**
    101     * The location to which to checkout the dist subversion repository under our working directory, which
    102     * was given above.
    103     */
    104    @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin/scm",
    105            property = "commons.distCheckoutDirectory")
    106    private File distCheckoutDirectory;
    107
    108    /**
    109     * The location of the RELEASE-NOTES.txt file such that multi-module builds can configure it.
    110     */
    111    @Parameter(defaultValue = "${basedir}/RELEASE-NOTES.txt", property = "commons.releaseNotesLocation")
    112    private File releaseNotesFile;
    113
    114    /**
    115     * A boolean that determines whether or not we actually commit the files up to the subversion repository.
    116     * If this is set to <code>true</code>, we do all but make the commits. We do checkout the repository in question
    117     * though.
    118     */
    119    @Parameter(property = "commons.release.dryRun", defaultValue = "false")
    120    private Boolean dryRun;
    121
    122    /**
    123     * The url of the subversion repository to which we wish the artifacts to be staged. Typically this would need to
    124     * be of the form: <code>scm:svn:https://dist.apache.org/repos/dist/dev/commons/foo/version-RC#</code>. Note. that
    125     * the prefix to the substring <code>https</code> is a requirement.
    126     */
    127    @Parameter(defaultValue = "", property = "commons.distSvnStagingUrl")
    128    private String distSvnStagingUrl;
    129
    130    /**
    131     * A parameter to generally avoid running unless it is specifically turned on by the consuming module.
    132     */
    133    @Parameter(defaultValue = "false", property = "commons.release.isDistModule")
    134    private Boolean isDistModule;
    135
    136    /**
    137     * The release version of the artifact to be built.
    138     */
    139    @Parameter(property = "commons.release.version")
    140    private String commonsReleaseVersion;
    141
    142    /**
    143     * The RC version of the release. For example the first voted on candidate would be "RC1".
    144     */
    145    @Parameter(property = "commons.rc.version")
    146    private String commonsRcVersion;
    147
    148    /**
    149     * The ID of the server (specified in settings.xml) which should be used for dist authentication.
    150     * This will be used in preference to {@link #username}/{@link #password}.
    151     */
    152    @Parameter(property = "commons.distServer")
    153    private String distServer;
    154
    155    /**
    156     * The username for the distribution subversion repository. This is typically your Apache id.
    157     */
    158    @Parameter(property = "user.name")
    159    private String username;
    160
    161    /**
    162     * The password associated with {@link CommonsDistributionStagingMojo#username}.
    163     */
    164    @Parameter(property = "user.password")
    165    private String password;
    166
    167    /**
    168     * Maven {@link Settings}.
    169     */
    170    @Parameter(defaultValue = "${settings}", readonly = true, required = true)
    171    private Settings settings;
    172
    173    /**
    174     * Maven {@link SettingsDecrypter} component.
    175     */
    176    @Component
    177    private SettingsDecrypter settingsDecrypter;
    178
    179    /**
    180     * A subdirectory of the dist directory into which we are going to stage the release candidate. We
    181     * build this up in the {@link CommonsDistributionStagingMojo#execute()} method. And, for example,
    182     * the directory should look like <code>https://https://dist.apache.org/repos/dist/dev/commons/text/1.4-RC1</code>.
    183     */
    184    private File distVersionRcVersionDirectory;
    185
    186    @Override
    187    public void execute() throws MojoExecutionException, MojoFailureException {
    188        if (!isDistModule) {
    189            getLog().info("This module is marked as a non distribution "
    190                    + "or assembly module, and the plugin will not run.");
    191            return;
    192        }
    193        if (StringUtils.isEmpty(distSvnStagingUrl)) {
    194            getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
    195            return;
    196        }
    197        if (!workingDirectory.exists()) {
    198            getLog().info("Current project contains no distributions. Not executing.");
    199            return;
    200        }
    201        getLog().info("Preparing to stage distributions");
    202        try {
    203            ScmManager scmManager = new BasicScmManager();
    204            scmManager.setScmProvider("svn", new SvnExeScmProvider());
    205            ScmRepository repository = scmManager.makeScmRepository(distSvnStagingUrl);
    206            ScmProvider provider = scmManager.getProviderByRepository(repository);
    207            SvnScmProviderRepository providerRepository = (SvnScmProviderRepository) repository.getProviderRepository();
    208            SharedFunctions.setAuthentication(
    209                    providerRepository,
    210                    distServer,
    211                    settings,
    212                    settingsDecrypter,
    213                    username,
    214                    password
    215            );
    216            distVersionRcVersionDirectory =
    217                    new File(distCheckoutDirectory, commonsReleaseVersion + "-" + commonsRcVersion);
    218            if (!distCheckoutDirectory.exists()) {
    219                SharedFunctions.initDirectory(getLog(), distCheckoutDirectory);
    220            }
    221            ScmFileSet scmFileSet = new ScmFileSet(distCheckoutDirectory);
    222            getLog().info("Checking out dist from: " + distSvnStagingUrl);
    223            final CheckOutScmResult checkOutResult = provider.checkOut(repository, scmFileSet);
    224            if (!checkOutResult.isSuccess()) {
    225                throw new MojoExecutionException("Failed to checkout files from SCM: "
    226                        + checkOutResult.getProviderMessage() + " [" + checkOutResult.getCommandOutput() + "]");
    227            }
    228            File copiedReleaseNotes = copyReleaseNotesToWorkingDirectory();
    229            copyDistributionsIntoScmDirectoryStructureAndAddToSvn(copiedReleaseNotes,
    230                    provider, repository);
    231            List<File> filesToAdd = new ArrayList<>();
    232            listNotHiddenFilesAndDirectories(distCheckoutDirectory, filesToAdd);
    233            if (!dryRun) {
    234                ScmFileSet fileSet = new ScmFileSet(distCheckoutDirectory, filesToAdd);
    235                AddScmResult addResult = provider.add(
    236                        repository,
    237                        fileSet
    238                );
    239                if (!addResult.isSuccess()) {
    240                    throw new MojoExecutionException("Failed to add files to SCM: " + addResult.getProviderMessage()
    241                            + " [" + addResult.getCommandOutput() + "]");
    242                }
    243                getLog().info("Staging release: " + project.getArtifactId() + ", version: " + project.getVersion());
    244                CheckInScmResult checkInResult = provider.checkIn(
    245                        repository,
    246                        fileSet,
    247                        "Staging release: " + project.getArtifactId() + ", version: " + project.getVersion()
    248                );
    249                if (!checkInResult.isSuccess()) {
    250                    getLog().error("Committing dist files failed: " + checkInResult.getCommandOutput());
    251                    throw new MojoExecutionException(
    252                            "Committing dist files failed: " + checkInResult.getCommandOutput()
    253                    );
    254                }
    255                getLog().info("Committed revision " + checkInResult.getScmRevision());
    256            } else {
    257                getLog().info("[Dry run] Would have committed to: " + distSvnStagingUrl);
    258                getLog().info(
    259                        "[Dry run] Staging release: " + project.getArtifactId() + ", version: " + project.getVersion());
    260            }
    261        } catch (ScmException e) {
    262            getLog().error("Could not commit files to dist: " + distSvnStagingUrl, e);
    263            throw new MojoExecutionException("Could not commit files to dist: " + distSvnStagingUrl, e);
    264        }
    265    }
    266
    267    /**
    268     * Lists all directories and files to a flat list.
    269     * @param directory {@link File} containing directory to list
    270     * @param files a {@link List} of {@link File} to which to append the files.
    271     */
    272    private void listNotHiddenFilesAndDirectories(File directory, List<File> files) {
    273        // Get all the files and directories from a directory.
    274        File[] fList = directory.listFiles();
    275        for (File file : fList) {
    276            if (file.isFile() && !file.isHidden()) {
    277                files.add(file);
    278            } else if (file.isDirectory() && !file.isHidden()) {
    279                files.add(file);
    280                listNotHiddenFilesAndDirectories(file, files);
    281            }
    282        }
    283    }
    284
    285    /**
    286     * A utility method that takes the <code>RELEASE-NOTES.txt</code> file from the base directory of the
    287     * project and copies it into {@link CommonsDistributionStagingMojo#workingDirectory}.
    288     *
    289     * @return the RELEASE-NOTES.txt file that exists in the <code>target/commons-release-notes/scm</code>
    290     *         directory for the purpose of adding it to the scm change set in the method
    291     *         {@link CommonsDistributionStagingMojo#copyDistributionsIntoScmDirectoryStructureAndAddToSvn(File,
    292     *         ScmProvider, ScmRepository)}.
    293     * @throws MojoExecutionException if an {@link IOException} occurs as a wrapper so that maven
    294     *                                can properly handle the exception.
    295     */
    296    private File copyReleaseNotesToWorkingDirectory() throws MojoExecutionException {
    297        SharedFunctions.initDirectory(getLog(), distVersionRcVersionDirectory);
    298        getLog().info("Copying RELEASE-NOTES.txt to working directory.");
    299        File copiedReleaseNotes = new File(distVersionRcVersionDirectory, releaseNotesFile.getName());
    300        SharedFunctions.copyFile(getLog(), releaseNotesFile, copiedReleaseNotes);
    301        return copiedReleaseNotes;
    302    }
    303
    304    /**
    305     * Copies the list of files at the root of the {@link CommonsDistributionStagingMojo#workingDirectory} into
    306     * the directory structure of the distribution staging repository. Specifically:
    307     * <ul>
    308     *   <li>root:
    309     *     <ul>
    310     *         <li>site</li>
    311     *         <li>site.zip</li>
    312     *         <li>RELEASE-NOTES.txt</li>
    313     *         <li>source:
    314     *           <ul>
    315     *             <li>-src artifacts....</li>
    316     *           </ul>
    317     *         </li>
    318     *         <li>binaries:
    319     *           <ul>
    320     *             <li>-bin artifacts....</li>
    321     *           </ul>
    322     *         </li>
    323     *     </ul>
    324     *   </li>
    325     * </ul>
    326     *
    327     * @param copiedReleaseNotes is the RELEASE-NOTES.txt file that exists in the
    328     *                           <code>target/commons-release-plugin/scm</code> directory.
    329     * @param provider is the {@link ScmProvider} that we will use for adding the files we wish to commit.
    330     * @param repository is the {@link ScmRepository} that we will use for adding the files that we wish to commit.
    331     * @return a {@link List} of {@link File}'s in the directory for the purpose of adding them to the maven
    332     *         {@link ScmFileSet}.
    333     * @throws MojoExecutionException if an {@link IOException} occurs so that Maven can handle it properly.
    334     */
    335    private List<File> copyDistributionsIntoScmDirectoryStructureAndAddToSvn(File copiedReleaseNotes,
    336                                                                             ScmProvider provider,
    337                                                                             ScmRepository repository)
    338            throws MojoExecutionException {
    339        List<File> workingDirectoryFiles = Arrays.asList(workingDirectory.listFiles());
    340        List<File> filesForMavenScmFileSet = new ArrayList<>();
    341        File scmBinariesRoot = new File(distVersionRcVersionDirectory, "binaries");
    342        File scmSourceRoot = new File(distVersionRcVersionDirectory, "source");
    343        SharedFunctions.initDirectory(getLog(), scmBinariesRoot);
    344        SharedFunctions.initDirectory(getLog(), scmSourceRoot);
    345        File copy;
    346        for (File file : workingDirectoryFiles) {
    347            if (file.getName().contains("src")) {
    348                copy = new File(scmSourceRoot,  file.getName());
    349                SharedFunctions.copyFile(getLog(), file, copy);
    350                filesForMavenScmFileSet.add(file);
    351            } else if (file.getName().contains("bin")) {
    352                copy = new File(scmBinariesRoot,  file.getName());
    353                SharedFunctions.copyFile(getLog(), file, copy);
    354                filesForMavenScmFileSet.add(file);
    355            } else if (StringUtils.containsAny(file.getName(), "scm", "sha256.properties", "sha512.properties")) {
    356                getLog().debug("Not copying scm directory over to the scm directory because it is the scm directory.");
    357                //do nothing because we are copying into scm
    358            } else {
    359                copy = new File(distCheckoutDirectory.getAbsolutePath(),  file.getName());
    360                SharedFunctions.copyFile(getLog(), file, copy);
    361                filesForMavenScmFileSet.add(file);
    362            }
    363        }
    364        filesForMavenScmFileSet.addAll(buildReadmeAndHeaderHtmlFiles());
    365        filesForMavenScmFileSet.addAll(copySiteToScmDirectory());
    366        return filesForMavenScmFileSet;
    367    }
    368
    369    /**
    370     * Copies <code>${basedir}/target/site</code> to <code>${basedir}/target/commons-release-plugin/scm/site</code>.
    371     *
    372     * @return the {@link List} of {@link File}'s contained in
    373     *         <code>${basedir}/target/commons-release-plugin/scm/site</code>, after the copy is complete.
    374     * @throws MojoExecutionException if the site copying fails for some reason.
    375     */
    376    private List<File> copySiteToScmDirectory() throws MojoExecutionException {
    377        if (!siteDirectory.exists()) {
    378            getLog().error("\"mvn site\" was not run before this goal, or a siteDirectory did not exist.");
    379            throw new MojoExecutionException(
    380                    "\"mvn site\" was not run before this goal, or a siteDirectory did not exist."
    381            );
    382        }
    383        File siteInScm = new File(distVersionRcVersionDirectory, "site");
    384        try {
    385            FileUtils.copyDirectory(siteDirectory, siteInScm);
    386        } catch (IOException e) {
    387            throw new MojoExecutionException("Site copying failed", e);
    388        }
    389        return new ArrayList<>(FileUtils.listFiles(siteInScm, null, true));
    390    }
    391
    392    /**
    393     * Builds up <code>README.html</code> and <code>HEADER.html</code> that reside in following.
    394     * <ul>
    395     *     <li>distRoot
    396     *     <ul>
    397     *         <li>binaries/HEADER.html (symlink)</li>
    398     *         <li>binaries/README.html (symlink)</li>
    399     *         <li>source/HEADER.html (symlink)</li>
    400     *         <li>source/README.html (symlink)</li>
    401     *         <li>HEADER.html</li>
    402     *         <li>README.html</li>
    403     *     </ul>
    404     *     </li>
    405     * </ul>
    406     * @return the {@link List} of created files above
    407     * @throws MojoExecutionException if an {@link IOException} occurs in the creation of these
    408     *                                files fails.
    409     */
    410    private List<File> buildReadmeAndHeaderHtmlFiles() throws MojoExecutionException {
    411        List<File> headerAndReadmeFiles = new ArrayList<>();
    412        File headerFile = new File(distVersionRcVersionDirectory, HEADER_FILE_NAME);
    413        //
    414        // HEADER file
    415        //
    416        try (Writer headerWriter = new OutputStreamWriter(new FileOutputStream(headerFile), "UTF-8")) {
    417            HeaderHtmlVelocityDelegate.builder().build().render(headerWriter);
    418        } catch (IOException e) {
    419            final String message = "Could not build HEADER html file " + headerFile;
    420            getLog().error(message, e);
    421            throw new MojoExecutionException(message, e);
    422        }
    423        headerAndReadmeFiles.add(headerFile);
    424        //
    425        // README file
    426        //
    427        File readmeFile = new File(distVersionRcVersionDirectory, README_FILE_NAME);
    428        try (Writer readmeWriter = new OutputStreamWriter(new FileOutputStream(readmeFile), "UTF-8")) {
    429            // @formatter:off
    430            ReadmeHtmlVelocityDelegate readmeHtmlVelocityDelegate = ReadmeHtmlVelocityDelegate.builder()
    431                    .withArtifactId(project.getArtifactId())
    432                    .withVersion(project.getVersion())
    433                    .withSiteUrl(project.getUrl())
    434                    .build();
    435            // @formatter:on
    436            readmeHtmlVelocityDelegate.render(readmeWriter);
    437        } catch (IOException e) {
    438            final String message = "Could not build README html file " + readmeFile;
    439            getLog().error(message, e);
    440            throw new MojoExecutionException(message, e);
    441        }
    442        headerAndReadmeFiles.add(readmeFile);
    443        headerAndReadmeFiles.addAll(copyHeaderAndReadmeToSubdirectories(headerFile, readmeFile));
    444        return headerAndReadmeFiles;
    445    }
    446
    447    /**
    448     * Copies <code>README.html</code> and <code>HEADER.html</code> to the source and binaries
    449     * directories.
    450     *
    451     * @param headerFile The originally created <code>HEADER.html</code> file.
    452     * @param readmeFile The originally created <code>README.html</code> file.
    453     * @return a {@link List} of created files.
    454     * @throws MojoExecutionException if the {@link SharedFunctions#copyFile(Log, File, File)}
    455     *                                fails.
    456     */
    457    private List<File> copyHeaderAndReadmeToSubdirectories(File headerFile, File readmeFile)
    458            throws MojoExecutionException {
    459        List<File> symbolicLinkFiles = new ArrayList<>();
    460        File sourceRoot = new File(distVersionRcVersionDirectory, "source");
    461        File binariesRoot = new File(distVersionRcVersionDirectory, "binaries");
    462        File sourceHeaderFile = new File(sourceRoot, HEADER_FILE_NAME);
    463        File sourceReadmeFile = new File(sourceRoot, README_FILE_NAME);
    464        File binariesHeaderFile = new File(binariesRoot, HEADER_FILE_NAME);
    465        File binariesReadmeFile = new File(binariesRoot, README_FILE_NAME);
    466        SharedFunctions.copyFile(getLog(), headerFile, sourceHeaderFile);
    467        symbolicLinkFiles.add(sourceHeaderFile);
    468        SharedFunctions.copyFile(getLog(), readmeFile, sourceReadmeFile);
    469        symbolicLinkFiles.add(sourceReadmeFile);
    470        SharedFunctions.copyFile(getLog(), headerFile, binariesHeaderFile);
    471        symbolicLinkFiles.add(binariesHeaderFile);
    472        SharedFunctions.copyFile(getLog(), readmeFile, binariesReadmeFile);
    473        symbolicLinkFiles.add(binariesReadmeFile);
    474        return symbolicLinkFiles;
    475    }
    476
    477    /**
    478     * This method is the setter for the {@link CommonsDistributionStagingMojo#baseDir} field, specifically
    479     * for the usage in the unit tests.
    480     *
    481     * @param baseDir is the {@link File} to be used as the project's root directory when this mojo
    482     *                is invoked.
    483     */
    484    protected void setBaseDir(File baseDir) {
    485        this.baseDir = baseDir;
    486    }
    487}