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