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 * Checks out the dev distribution location, copies the distributions into that directory 063 * structure under the <code>target/commons-release-plugin/scm</code> directory, and commits the 064 * distributions back to SVN. Includes the built and zipped site as well as the RELEASE-NOTES.txt file. 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 the 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 the 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} representing 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 when 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 check out 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 release 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. 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 the following directory structure: 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 Thrown if an {@link IOException} occurs while creating these files. 218 */ 219 private List<File> buildReadmeAndHeaderHtmlFiles() throws MojoExecutionException { 220 final List<File> headerAndReadmeFiles = new ArrayList<>(); 221 final File headerFile = new File(distRcVersionDirectory, HEADER_FILE_NAME); 222 // 223 // HEADER file 224 // 225 try (Writer headerWriter = new OutputStreamWriter(Files.newOutputStream(headerFile.toPath()), 226 StandardCharsets.UTF_8)) { 227 HeaderHtmlVelocityDelegate.builder().build().render(headerWriter); 228 } catch (final IOException e) { 229 final String message = "Could not build HEADER html file " + headerFile; 230 getLog().error(message, e); 231 throw new MojoExecutionException(message, e); 232 } 233 headerAndReadmeFiles.add(headerFile); 234 // 235 // README file 236 // 237 final File readmeFile = new File(distRcVersionDirectory, README_FILE_NAME); 238 try (Writer readmeWriter = new OutputStreamWriter(Files.newOutputStream(readmeFile.toPath()), 239 StandardCharsets.UTF_8)) { 240 // @formatter:off 241 final ReadmeHtmlVelocityDelegate readmeHtmlVelocityDelegate = ReadmeHtmlVelocityDelegate.builder() 242 .setArtifactId(project.getArtifactId()) 243 .setVersion(project.getVersion()) 244 .setSiteUrl(project.getUrl()) 245 .build(); 246 // @formatter:on 247 readmeHtmlVelocityDelegate.render(readmeWriter); 248 } catch (final IOException e) { 249 final String message = "Could not build README html file " + readmeFile; 250 getLog().error(message, e); 251 throw new MojoExecutionException(message, e); 252 } 253 headerAndReadmeFiles.add(readmeFile); 254 // 255 // signature-validator.sh file copy 256 // 257 headerAndReadmeFiles.addAll(copyHeaderAndReadmeToSubdirectories(headerFile, readmeFile)); 258 return headerAndReadmeFiles; 259 } 260 261 /** 262 * Copies the list of files at the root of the {@link CommonsDistributionStagingMojo#workingDirectory} into 263 * the directory structure of the distribution staging repository. Specifically: 264 * <ul> 265 * <li>root: 266 * <ul> 267 * <li>site</li> 268 * <li>site.zip</li> 269 * <li>RELEASE-NOTES.txt</li> 270 * <li>source: 271 * <ul> 272 * <li>-src artifacts....</li> 273 * </ul> 274 * </li> 275 * <li>binaries: 276 * <ul> 277 * <li>-bin artifacts....</li> 278 * </ul> 279 * </li> 280 * </ul> 281 * </li> 282 * </ul> 283 * 284 * @param copiedReleaseNotes is the RELEASE-NOTES.txt file that exists in the 285 * <code>target/commons-release-plugin/scm</code> directory. 286 * @param provider is the {@link ScmProvider} that we will use for adding the files we wish to commit. 287 * @param repository is the {@link ScmRepository} that we will use for adding the files that we wish to commit. 288 * @return A {@link List} of {@link File} objects in the directory for the purpose of adding them to the Maven 289 * {@link ScmFileSet}. 290 * @throws MojoExecutionException Thrown if an {@link IOException} occurs. 291 */ 292 private List<File> copyDistributionsIntoScmDirectoryStructureAndAddToSvn(final File copiedReleaseNotes, 293 final ScmProvider provider, 294 final ScmRepository repository) 295 throws MojoExecutionException { 296 final List<File> workingDirectoryFiles = Arrays.asList(workingDirectory.listFiles()); 297 final List<File> filesForMavenScmFileSet = new ArrayList<>(); 298 final File scmBinariesRoot = new File(distRcVersionDirectory, "binaries"); 299 final File scmSourceRoot = new File(distRcVersionDirectory, "source"); 300 SharedFunctions.initDirectory(getLog(), scmBinariesRoot); 301 SharedFunctions.initDirectory(getLog(), scmSourceRoot); 302 File copy; 303 for (final File file : workingDirectoryFiles) { 304 if (file.getName().contains("src")) { 305 copy = new File(scmSourceRoot, file.getName()); 306 SharedFunctions.copyFile(getLog(), file, copy); 307 filesForMavenScmFileSet.add(file); 308 } else if (file.getName().contains("bin")) { 309 copy = new File(scmBinariesRoot, file.getName()); 310 SharedFunctions.copyFile(getLog(), file, copy); 311 filesForMavenScmFileSet.add(file); 312 } else if (Strings.CS.containsAny(file.getName(), "scm", "sha256.properties", "sha512.properties")) { 313 getLog().debug("Not copying scm directory over to the scm directory because it is the scm directory."); 314 // Do nothing because we are copying into the SCM directory. 315 } else { 316 copy = new File(distCheckoutDirectory.getAbsolutePath(), file.getName()); 317 SharedFunctions.copyFile(getLog(), file, copy); 318 filesForMavenScmFileSet.add(file); 319 } 320 } 321 filesForMavenScmFileSet.addAll(buildReadmeAndHeaderHtmlFiles()); 322 filesForMavenScmFileSet.add(copySignatureValidatorScriptToScmDirectory()); 323 filesForMavenScmFileSet.addAll(copySiteToScmDirectory()); 324 return filesForMavenScmFileSet; 325 } 326 327 /** 328 * Copies <code>README.html</code> and <code>HEADER.html</code> to the source and binaries 329 * directories. 330 * 331 * @param headerFile The originally created <code>HEADER.html</code> file. 332 * @param readmeFile The originally created <code>README.html</code> file. 333 * @return A {@link List} of created files. 334 * @throws MojoExecutionException Thrown if {@link SharedFunctions#copyFile(Log, File, File)} fails. 335 */ 336 private List<File> copyHeaderAndReadmeToSubdirectories(final File headerFile, final File readmeFile) 337 throws MojoExecutionException { 338 final List<File> symbolicLinkFiles = new ArrayList<>(); 339 final File sourceRoot = new File(distRcVersionDirectory, "source"); 340 final File binariesRoot = new File(distRcVersionDirectory, "binaries"); 341 final File sourceHeaderFile = new File(sourceRoot, HEADER_FILE_NAME); 342 final File sourceReadmeFile = new File(sourceRoot, README_FILE_NAME); 343 final File binariesHeaderFile = new File(binariesRoot, HEADER_FILE_NAME); 344 final File binariesReadmeFile = new File(binariesRoot, README_FILE_NAME); 345 SharedFunctions.copyFile(getLog(), headerFile, sourceHeaderFile); 346 symbolicLinkFiles.add(sourceHeaderFile); 347 SharedFunctions.copyFile(getLog(), readmeFile, sourceReadmeFile); 348 symbolicLinkFiles.add(sourceReadmeFile); 349 SharedFunctions.copyFile(getLog(), headerFile, binariesHeaderFile); 350 symbolicLinkFiles.add(binariesHeaderFile); 351 SharedFunctions.copyFile(getLog(), readmeFile, binariesReadmeFile); 352 symbolicLinkFiles.add(binariesReadmeFile); 353 return symbolicLinkFiles; 354 } 355 356 /** 357 * Copies the configured release notes file into {@link #distRcVersionDirectory}. 358 * 359 * @return The copied release notes file in {@link #distRcVersionDirectory}, for inclusion in the SCM change set in the method 360 * {@link CommonsDistributionStagingMojo#copyDistributionsIntoScmDirectoryStructureAndAddToSvn(File, 361 * ScmProvider, ScmRepository)}. 362 * @throws MojoExecutionException Thrown if an {@link IOException} occurs while copying the file. 363 */ 364 private File copyReleaseNotesToWorkingDirectory() throws MojoExecutionException { 365 SharedFunctions.initDirectory(getLog(), distRcVersionDirectory); 366 getLog().info("Copying RELEASE-NOTES.txt to working directory."); 367 final File copiedReleaseNotes = new File(distRcVersionDirectory, releaseNotesFile.getName()); 368 SharedFunctions.copyFile(getLog(), releaseNotesFile, copiedReleaseNotes); 369 return copiedReleaseNotes; 370 } 371 372 /** 373 * Copies our <code>signature-validator.sh</code> script into 374 * <code>${basedir}/target/commons-release-plugin/scm/signature-validator.sh</code>. 375 * 376 * @return The {@link File} for the signature-validator.sh 377 * @throws MojoExecutionException Thrown if an error occurs while copying the resource. 378 */ 379 private File copySignatureValidatorScriptToScmDirectory() throws MojoExecutionException { 380 final Path scmTargetPath = Paths.get(distRcVersionDirectory.toString(), SIGNATURE_VALIDATOR_NAME); 381 final String name = "/resources/" + SIGNATURE_VALIDATOR_NAME; 382 // The source can be in a local file or inside a JAR file. 383 try { 384 PathUtils.copyFile(getClass().getResource(name), scmTargetPath); 385 } catch (final Exception e) { 386 throw new MojoExecutionException(String.format("Failed to copy '%s' to '%s'", name, scmTargetPath), e); 387 } 388 return scmTargetPath.toFile(); 389 } 390 391 /** 392 * Copies <code>${basedir}/target/site</code> to <code>${basedir}/target/commons-release-plugin/scm/site</code>. 393 * 394 * @return The {@link List} of {@link File} objects contained in 395 * <code>${basedir}/target/commons-release-plugin/scm/site</code>, after the copy is complete. 396 * @throws MojoExecutionException Thrown if an error occurs while copying the site. 397 */ 398 private List<File> copySiteToScmDirectory() throws MojoExecutionException { 399 if (!siteDirectory.exists()) { 400 getLog().error("\"mvn site\" was not run before this goal, or a siteDirectory did not exist."); 401 throw new MojoExecutionException( 402 "\"mvn site\" was not run before this goal, or a siteDirectory did not exist." 403 ); 404 } 405 final File siteInScm = new File(distRcVersionDirectory, "site"); 406 try { 407 FileUtils.copyDirectory(siteDirectory, siteInScm); 408 } catch (final IOException e) { 409 throw new MojoExecutionException("Site copying failed", e); 410 } 411 return new ArrayList<>(FileUtils.listFiles(siteInScm, null, true)); 412 } 413 414 @Override 415 public void execute() throws MojoExecutionException, MojoFailureException { 416 if (!isDistModule) { 417 getLog().info("This module is marked as a non distribution " 418 + "or assembly module, and the plugin will not run."); 419 return; 420 } 421 if (StringUtils.isEmpty(distSvnStagingUrl)) { 422 getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run."); 423 return; 424 } 425 if (!workingDirectory.exists()) { 426 getLog().info("Current project contains no distributions. Not executing."); 427 return; 428 } 429 getLog().info("Preparing to stage distributions"); 430 try { 431 final ScmManager scmManager = new BasicScmManager(); 432 scmManager.setScmProvider("svn", new SvnExeScmProvider()); 433 final ScmRepository repository = scmManager.makeScmRepository(distSvnStagingUrl); 434 final ScmProvider provider = scmManager.getProviderByRepository(repository); 435 final SvnScmProviderRepository providerRepository = (SvnScmProviderRepository) repository 436 .getProviderRepository(); 437 SharedFunctions.setAuthentication( 438 providerRepository, 439 distServer, 440 settings, 441 settingsDecrypter, 442 username, 443 password 444 ); 445 distRcVersionDirectory = 446 new File(distCheckoutDirectory, commonsReleaseVersion + "-" + commonsRcVersion); 447 if (!distCheckoutDirectory.exists()) { 448 SharedFunctions.initDirectory(getLog(), distCheckoutDirectory); 449 } 450 final ScmFileSet scmFileSet = new ScmFileSet(distCheckoutDirectory); 451 getLog().info("Checking out dist from: " + distSvnStagingUrl); 452 final CheckOutScmResult checkOutResult = provider.checkOut(repository, scmFileSet); 453 if (!checkOutResult.isSuccess()) { 454 throw new MojoExecutionException("Failed to checkout files from SCM: " 455 + checkOutResult.getProviderMessage() + " [" + checkOutResult.getCommandOutput() + "]"); 456 } 457 final File copiedReleaseNotes = copyReleaseNotesToWorkingDirectory(); 458 copyDistributionsIntoScmDirectoryStructureAndAddToSvn(copiedReleaseNotes, 459 provider, repository); 460 final List<File> filesToAdd = new ArrayList<>(); 461 listNotHiddenFilesAndDirectories(distCheckoutDirectory, filesToAdd); 462 if (!dryRun) { 463 final ScmFileSet fileSet = new ScmFileSet(distCheckoutDirectory, filesToAdd); 464 final AddScmResult addResult = provider.add( 465 repository, 466 fileSet 467 ); 468 if (!addResult.isSuccess()) { 469 throw new MojoExecutionException("Failed to add files to SCM: " + addResult.getProviderMessage() 470 + " [" + addResult.getCommandOutput() + "]"); 471 } 472 getLog().info("Staging release: " + project.getArtifactId() + ", version: " + project.getVersion()); 473 final CheckInScmResult checkInResult = provider.checkIn( 474 repository, 475 fileSet, 476 "Staging release: " + project.getArtifactId() + ", version: " + project.getVersion() 477 ); 478 if (!checkInResult.isSuccess()) { 479 getLog().error("Committing dist files failed: " + checkInResult.getCommandOutput()); 480 throw new MojoExecutionException( 481 "Committing dist files failed: " + checkInResult.getCommandOutput() 482 ); 483 } 484 getLog().info("Committed revision " + checkInResult.getScmRevision()); 485 } else { 486 getLog().info("[Dry run] Would have committed to: " + distSvnStagingUrl); 487 getLog().info( 488 "[Dry run] Staging release: " + project.getArtifactId() + ", version: " + project.getVersion()); 489 } 490 } catch (final ScmException e) { 491 getLog().error("Could not commit files to dist: " + distSvnStagingUrl, e); 492 throw new MojoExecutionException("Could not commit files to dist: " + distSvnStagingUrl, e); 493 } 494 } 495 496 /** 497 * Lists all directories and files in a flat list. 498 * 499 * @param directory {@link File} containing directory to list 500 * @param files A {@link List} of {@link File} to which to append the files. 501 */ 502 private void listNotHiddenFilesAndDirectories(final File directory, final List<File> files) { 503 // Get all the files and directories from a directory. 504 final File[] fList = directory.listFiles(); 505 for (final File file : fList) { 506 if (file.isFile() && !file.isHidden()) { 507 files.add(file); 508 } else if (file.isDirectory() && !file.isHidden()) { 509 files.add(file); 510 listNotHiddenFilesAndDirectories(file, files); 511 } 512 } 513 } 514 515 /** 516 * Sets the {@link CommonsDistributionStagingMojo#baseDir} field for use in unit tests. 517 * 518 * @param baseDir is the {@link File} to be used as the project's root directory when this mojo 519 * is invoked. 520 */ 521 protected void setBaseDir(final File baseDir) { 522 this.baseDir = baseDir; 523 } 524}