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