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 * The {@link MavenProject} object is essentially the context of the maven build at 083 * a given time. 084 */ 085 @Parameter(defaultValue = "${project}", required = true) 086 private MavenProject project; 087 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 @Override 193 public void execute() throws MojoExecutionException, MojoFailureException { 194 if (!isDistModule) { 195 getLog().info("This module is marked as a non distribution " 196 + "or assembly module, and the plugin will not run."); 197 return; 198 } 199 if (StringUtils.isEmpty(distSvnStagingUrl)) { 200 getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run."); 201 return; 202 } 203 if (!workingDirectory.exists()) { 204 getLog().info("Current project contains no distributions. Not executing."); 205 return; 206 } 207 getLog().info("Preparing to stage distributions"); 208 try { 209 final ScmManager scmManager = new BasicScmManager(); 210 scmManager.setScmProvider("svn", new SvnExeScmProvider()); 211 final ScmRepository repository = scmManager.makeScmRepository(distSvnStagingUrl); 212 final ScmProvider provider = scmManager.getProviderByRepository(repository); 213 final SvnScmProviderRepository providerRepository = (SvnScmProviderRepository) repository 214 .getProviderRepository(); 215 SharedFunctions.setAuthentication( 216 providerRepository, 217 distServer, 218 settings, 219 settingsDecrypter, 220 username, 221 password 222 ); 223 distRcVersionDirectory = 224 new File(distCheckoutDirectory, commonsReleaseVersion + "-" + commonsRcVersion); 225 if (!distCheckoutDirectory.exists()) { 226 SharedFunctions.initDirectory(getLog(), distCheckoutDirectory); 227 } 228 final ScmFileSet scmFileSet = new ScmFileSet(distCheckoutDirectory); 229 getLog().info("Checking out dist from: " + distSvnStagingUrl); 230 final CheckOutScmResult checkOutResult = provider.checkOut(repository, scmFileSet); 231 if (!checkOutResult.isSuccess()) { 232 throw new MojoExecutionException("Failed to checkout files from SCM: " 233 + checkOutResult.getProviderMessage() + " [" + checkOutResult.getCommandOutput() + "]"); 234 } 235 final File copiedReleaseNotes = copyReleaseNotesToWorkingDirectory(); 236 copyDistributionsIntoScmDirectoryStructureAndAddToSvn(copiedReleaseNotes, 237 provider, repository); 238 final List<File> filesToAdd = new ArrayList<>(); 239 listNotHiddenFilesAndDirectories(distCheckoutDirectory, filesToAdd); 240 if (!dryRun) { 241 final ScmFileSet fileSet = new ScmFileSet(distCheckoutDirectory, filesToAdd); 242 final AddScmResult addResult = provider.add( 243 repository, 244 fileSet 245 ); 246 if (!addResult.isSuccess()) { 247 throw new MojoExecutionException("Failed to add files to SCM: " + addResult.getProviderMessage() 248 + " [" + addResult.getCommandOutput() + "]"); 249 } 250 getLog().info("Staging release: " + project.getArtifactId() + ", version: " + project.getVersion()); 251 final CheckInScmResult checkInResult = provider.checkIn( 252 repository, 253 fileSet, 254 "Staging release: " + project.getArtifactId() + ", version: " + project.getVersion() 255 ); 256 if (!checkInResult.isSuccess()) { 257 getLog().error("Committing dist files failed: " + checkInResult.getCommandOutput()); 258 throw new MojoExecutionException( 259 "Committing dist files failed: " + checkInResult.getCommandOutput() 260 ); 261 } 262 getLog().info("Committed revision " + checkInResult.getScmRevision()); 263 } else { 264 getLog().info("[Dry run] Would have committed to: " + distSvnStagingUrl); 265 getLog().info( 266 "[Dry run] Staging release: " + project.getArtifactId() + ", version: " + project.getVersion()); 267 } 268 } catch (final ScmException e) { 269 getLog().error("Could not commit files to dist: " + distSvnStagingUrl, e); 270 throw new MojoExecutionException("Could not commit files to dist: " + distSvnStagingUrl, e); 271 } 272 } 273 274 /** 275 * Lists all directories and files to a flat list. 276 * @param directory {@link File} containing directory to list 277 * @param files a {@link List} of {@link File} to which to append the files. 278 */ 279 private void listNotHiddenFilesAndDirectories(final File directory, final List<File> files) { 280 // Get all the files and directories from a directory. 281 final File[] fList = directory.listFiles(); 282 for (final File file : fList) { 283 if (file.isFile() && !file.isHidden()) { 284 files.add(file); 285 } else if (file.isDirectory() && !file.isHidden()) { 286 files.add(file); 287 listNotHiddenFilesAndDirectories(file, files); 288 } 289 } 290 } 291 292 /** 293 * A utility method that takes the <code>RELEASE-NOTES.txt</code> file from the base directory of the 294 * project and copies it into {@link CommonsDistributionStagingMojo#workingDirectory}. 295 * 296 * @return the RELEASE-NOTES.txt file that exists in the <code>target/commons-release-notes/scm</code> 297 * directory for the purpose of adding it to the scm change set in the method 298 * {@link CommonsDistributionStagingMojo#copyDistributionsIntoScmDirectoryStructureAndAddToSvn(File, 299 * ScmProvider, ScmRepository)}. 300 * @throws MojoExecutionException if an {@link IOException} occurs as a wrapper so that maven 301 * can properly handle the exception. 302 */ 303 private File copyReleaseNotesToWorkingDirectory() throws MojoExecutionException { 304 SharedFunctions.initDirectory(getLog(), distRcVersionDirectory); 305 getLog().info("Copying RELEASE-NOTES.txt to working directory."); 306 final File copiedReleaseNotes = new File(distRcVersionDirectory, releaseNotesFile.getName()); 307 SharedFunctions.copyFile(getLog(), releaseNotesFile, copiedReleaseNotes); 308 return copiedReleaseNotes; 309 } 310 311 /** 312 * Copies the list of files at the root of the {@link CommonsDistributionStagingMojo#workingDirectory} into 313 * the directory structure of the distribution staging repository. Specifically: 314 * <ul> 315 * <li>root: 316 * <ul> 317 * <li>site</li> 318 * <li>site.zip</li> 319 * <li>RELEASE-NOTES.txt</li> 320 * <li>source: 321 * <ul> 322 * <li>-src artifacts....</li> 323 * </ul> 324 * </li> 325 * <li>binaries: 326 * <ul> 327 * <li>-bin artifacts....</li> 328 * </ul> 329 * </li> 330 * </ul> 331 * </li> 332 * </ul> 333 * 334 * @param copiedReleaseNotes is the RELEASE-NOTES.txt file that exists in the 335 * <code>target/commons-release-plugin/scm</code> directory. 336 * @param provider is the {@link ScmProvider} that we will use for adding the files we wish to commit. 337 * @param repository is the {@link ScmRepository} that we will use for adding the files that we wish to commit. 338 * @return a {@link List} of {@link File}'s in the directory for the purpose of adding them to the maven 339 * {@link ScmFileSet}. 340 * @throws MojoExecutionException if an {@link IOException} occurs so that Maven can handle it properly. 341 */ 342 private List<File> copyDistributionsIntoScmDirectoryStructureAndAddToSvn(final File copiedReleaseNotes, 343 final ScmProvider provider, 344 final ScmRepository repository) 345 throws MojoExecutionException { 346 final List<File> workingDirectoryFiles = Arrays.asList(workingDirectory.listFiles()); 347 final List<File> filesForMavenScmFileSet = new ArrayList<>(); 348 final File scmBinariesRoot = new File(distRcVersionDirectory, "binaries"); 349 final File scmSourceRoot = new File(distRcVersionDirectory, "source"); 350 SharedFunctions.initDirectory(getLog(), scmBinariesRoot); 351 SharedFunctions.initDirectory(getLog(), scmSourceRoot); 352 File copy; 353 for (final File file : workingDirectoryFiles) { 354 if (file.getName().contains("src")) { 355 copy = new File(scmSourceRoot, file.getName()); 356 SharedFunctions.copyFile(getLog(), file, copy); 357 filesForMavenScmFileSet.add(file); 358 } else if (file.getName().contains("bin")) { 359 copy = new File(scmBinariesRoot, file.getName()); 360 SharedFunctions.copyFile(getLog(), file, copy); 361 filesForMavenScmFileSet.add(file); 362 } else if (StringUtils.containsAny(file.getName(), "scm", "sha256.properties", "sha512.properties")) { 363 getLog().debug("Not copying scm directory over to the scm directory because it is the scm directory."); 364 //do nothing because we are copying into scm 365 } else { 366 copy = new File(distCheckoutDirectory.getAbsolutePath(), file.getName()); 367 SharedFunctions.copyFile(getLog(), file, copy); 368 filesForMavenScmFileSet.add(file); 369 } 370 } 371 filesForMavenScmFileSet.addAll(buildReadmeAndHeaderHtmlFiles()); 372 filesForMavenScmFileSet.add(copySignatureValidatorScriptToScmDirectory()); 373 filesForMavenScmFileSet.addAll(copySiteToScmDirectory()); 374 return filesForMavenScmFileSet; 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 /** 420 * Builds up <code>README.html</code> and <code>HEADER.html</code> that reside in following. 421 * <ul> 422 * <li>distRoot 423 * <ul> 424 * <li>binaries/HEADER.html (symlink)</li> 425 * <li>binaries/README.html (symlink)</li> 426 * <li>source/HEADER.html (symlink)</li> 427 * <li>source/README.html (symlink)</li> 428 * <li>HEADER.html</li> 429 * <li>README.html</li> 430 * </ul> 431 * </li> 432 * </ul> 433 * @return the {@link List} of created files above 434 * @throws MojoExecutionException if an {@link IOException} occurs in the creation of these 435 * files fails. 436 */ 437 private List<File> buildReadmeAndHeaderHtmlFiles() throws MojoExecutionException { 438 final List<File> headerAndReadmeFiles = new ArrayList<>(); 439 final File headerFile = new File(distRcVersionDirectory, HEADER_FILE_NAME); 440 // 441 // HEADER file 442 // 443 try (Writer headerWriter = new OutputStreamWriter(Files.newOutputStream(headerFile.toPath()), 444 StandardCharsets.UTF_8)) { 445 HeaderHtmlVelocityDelegate.builder().build().render(headerWriter); 446 } catch (final IOException e) { 447 final String message = "Could not build HEADER html file " + headerFile; 448 getLog().error(message, e); 449 throw new MojoExecutionException(message, e); 450 } 451 headerAndReadmeFiles.add(headerFile); 452 // 453 // README file 454 // 455 final File readmeFile = new File(distRcVersionDirectory, README_FILE_NAME); 456 try (Writer readmeWriter = new OutputStreamWriter(Files.newOutputStream(readmeFile.toPath()), 457 StandardCharsets.UTF_8)) { 458 // @formatter:off 459 final ReadmeHtmlVelocityDelegate readmeHtmlVelocityDelegate = ReadmeHtmlVelocityDelegate.builder() 460 .withArtifactId(project.getArtifactId()) 461 .withVersion(project.getVersion()) 462 .withSiteUrl(project.getUrl()) 463 .build(); 464 // @formatter:on 465 readmeHtmlVelocityDelegate.render(readmeWriter); 466 } catch (final IOException e) { 467 final String message = "Could not build README html file " + readmeFile; 468 getLog().error(message, e); 469 throw new MojoExecutionException(message, e); 470 } 471 headerAndReadmeFiles.add(readmeFile); 472 // 473 // signature-validator.sh file copy 474 // 475 headerAndReadmeFiles.addAll(copyHeaderAndReadmeToSubdirectories(headerFile, readmeFile)); 476 return headerAndReadmeFiles; 477 } 478 479 /** 480 * Copies <code>README.html</code> and <code>HEADER.html</code> to the source and binaries 481 * directories. 482 * 483 * @param headerFile The originally created <code>HEADER.html</code> file. 484 * @param readmeFile The originally created <code>README.html</code> file. 485 * @return a {@link List} of created files. 486 * @throws MojoExecutionException if the {@link SharedFunctions#copyFile(Log, File, File)} 487 * fails. 488 */ 489 private List<File> copyHeaderAndReadmeToSubdirectories(final File headerFile, final File readmeFile) 490 throws MojoExecutionException { 491 final List<File> symbolicLinkFiles = new ArrayList<>(); 492 final File sourceRoot = new File(distRcVersionDirectory, "source"); 493 final File binariesRoot = new File(distRcVersionDirectory, "binaries"); 494 final File sourceHeaderFile = new File(sourceRoot, HEADER_FILE_NAME); 495 final File sourceReadmeFile = new File(sourceRoot, README_FILE_NAME); 496 final File binariesHeaderFile = new File(binariesRoot, HEADER_FILE_NAME); 497 final File binariesReadmeFile = new File(binariesRoot, README_FILE_NAME); 498 SharedFunctions.copyFile(getLog(), headerFile, sourceHeaderFile); 499 symbolicLinkFiles.add(sourceHeaderFile); 500 SharedFunctions.copyFile(getLog(), readmeFile, sourceReadmeFile); 501 symbolicLinkFiles.add(sourceReadmeFile); 502 SharedFunctions.copyFile(getLog(), headerFile, binariesHeaderFile); 503 symbolicLinkFiles.add(binariesHeaderFile); 504 SharedFunctions.copyFile(getLog(), readmeFile, binariesReadmeFile); 505 symbolicLinkFiles.add(binariesReadmeFile); 506 return symbolicLinkFiles; 507 } 508 509 /** 510 * This method is the setter for the {@link CommonsDistributionStagingMojo#baseDir} field, specifically 511 * for the usage in the unit tests. 512 * 513 * @param baseDir is the {@link File} to be used as the project's root directory when this mojo 514 * is invoked. 515 */ 516 protected void setBaseDir(final File baseDir) { 517 this.baseDir = baseDir; 518 } 519}