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