001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.plugin; 020 021import javax.inject.Inject; 022 023import java.io.File; 024import java.io.IOException; 025 026import org.apache.maven.plugin.MojoExecutionException; 027import org.apache.maven.plugins.annotations.Mojo; 028import org.apache.maven.plugins.annotations.Parameter; 029import org.apache.maven.scm.ScmException; 030import org.apache.maven.scm.ScmFileSet; 031import org.apache.maven.scm.command.export.ExportScmResult; 032import org.apache.maven.scm.manager.ScmManager; 033import org.apache.maven.scm.repository.ScmRepository; 034import org.apache.maven.settings.crypto.SettingsDecrypter; 035import org.codehaus.plexus.util.FileUtils; 036 037/** 038 * Get a fresh exported copy of the latest source from the configured scm url. 039 * 040 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> 041 */ 042@Mojo(name = "export", requiresProject = false) 043public class ExportMojo extends AbstractScmMojo { 044 /** 045 * The version type (branch/tag/revision) of scmVersion. 046 */ 047 @Parameter(property = "scmVersionType") 048 private String scmVersionType; 049 050 /** 051 * The version (revision number/branch name/tag name). 052 */ 053 @Parameter(property = "scmVersion") 054 private String scmVersion; 055 056 /** 057 * The directory to export the sources to. 058 */ 059 @Parameter(property = "exportDirectory", defaultValue = "${project.build.directory}/export", required = true) 060 private File exportDirectory; 061 062 /** 063 * Skip export if exportDirectory exists. 064 */ 065 @Parameter(property = "skipExportIfExists", defaultValue = "false") 066 private boolean skipExportIfExists = false; 067 068 @Inject 069 public ExportMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) { 070 super(manager, settingsDecrypter); 071 } 072 073 /** 074 * {@inheritDoc} 075 */ 076 public void execute() throws MojoExecutionException { 077 super.execute(); 078 079 if (this.skipExportIfExists && this.exportDirectory.isDirectory()) { 080 return; 081 } 082 083 export(); 084 } 085 086 protected File getExportDirectory() { 087 return this.exportDirectory; 088 } 089 090 public void setExportDirectory(File exportDirectory) { 091 this.exportDirectory = exportDirectory; 092 } 093 094 protected void export() throws MojoExecutionException { 095 if (this.exportDirectory.getPath().contains("${project.basedir}")) { 096 // project.basedir is not set under maven 3.x when run without a project 097 this.exportDirectory = new File(this.getBasedir(), "target/export"); 098 } 099 try { 100 ScmRepository repository = getScmRepository(); 101 102 try { 103 if (this.exportDirectory.exists()) { 104 this.getLog().debug("Removing " + this.exportDirectory); 105 106 FileUtils.deleteDirectory(this.exportDirectory); 107 } 108 } catch (IOException e) { 109 throw new MojoExecutionException("Cannot remove " + getExportDirectory()); 110 } 111 112 if (!this.exportDirectory.mkdirs()) { 113 throw new MojoExecutionException("Cannot create " + this.exportDirectory); 114 } 115 116 ExportScmResult result = getScmManager() 117 .export( 118 repository, 119 new ScmFileSet(this.exportDirectory.getAbsoluteFile()), 120 getScmVersion(scmVersionType, scmVersion)); 121 122 checkResult(result); 123 124 handleExcludesIncludesAfterCheckoutAndExport(this.exportDirectory); 125 } catch (ScmException e) { 126 throw new MojoExecutionException("Cannot run export command : ", e); 127 } 128 } 129}