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