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