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