| 1 |
package org.apache.maven.scm.plugin; |
| 2 |
|
| 3 |
/* |
| 4 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
* or more contributor license agreements. See the NOTICE file |
| 6 |
* distributed with this work for additional information |
| 7 |
* regarding copyright ownership. The ASF licenses this file |
| 8 |
* to you under the Apache License, Version 2.0 (the |
| 9 |
* "License"); you may not use this file except in compliance |
| 10 |
* with the License. You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, |
| 15 |
* software distributed under the License is distributed on an |
| 16 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
import java.io.File; |
| 23 |
import java.io.IOException; |
| 24 |
|
| 25 |
import org.apache.maven.plugin.MojoExecutionException; |
| 26 |
import org.apache.maven.plugins.annotations.Mojo; |
| 27 |
import org.apache.maven.plugins.annotations.Parameter; |
| 28 |
import org.apache.maven.scm.ScmException; |
| 29 |
import org.apache.maven.scm.ScmFileSet; |
| 30 |
import org.apache.maven.scm.command.export.ExportScmResult; |
| 31 |
import org.apache.maven.scm.repository.ScmRepository; |
| 32 |
import org.codehaus.plexus.util.FileUtils; |
| 33 |
|
| 34 |
/** |
| 35 |
* Get a fresh exported copy of the latest source from the configured scm url. |
| 36 |
* |
| 37 |
* @author <a href="mailto:[email protected]">Emmanuel Venisse</a> |
| 38 |
*/ |
| 39 |
@Mojo( name = "export", requiresProject = false ) |
| 40 |
public class ExportMojo |
| 41 |
extends AbstractScmMojo |
| 42 |
{ |
| 43 |
/** |
| 44 |
* The version type (branch/tag/revision) of scmVersion. |
| 45 |
*/ |
| 46 |
@Parameter( property = "scmVersionType" ) |
| 47 |
private String scmVersionType; |
| 48 |
|
| 49 |
/** |
| 50 |
* The version (revision number/branch name/tag name). |
| 51 |
*/ |
| 52 |
@Parameter( property = "scmVersion" ) |
| 53 |
private String scmVersion; |
| 54 |
|
| 55 |
/** |
| 56 |
* The directory to export the sources to. |
| 57 |
*/ |
| 58 |
@Parameter( property = "exportDirectory", defaultValue = "${project.build.directory}/export", required = true ) |
| 59 |
private File exportDirectory; |
| 60 |
|
| 61 |
/** |
| 62 |
* Skip export if exportDirectory exists. |
| 63 |
*/ |
| 64 |
@Parameter( property = "skipExportIfExists", defaultValue = "false" ) |
| 65 |
private boolean skipExportIfExists = false; |
| 66 |
|
| 67 |
|
| 68 |
/** {@inheritDoc} */ |
| 69 |
public void execute() |
| 70 |
throws MojoExecutionException |
| 71 |
{ |
| 72 |
super.execute(); |
| 73 |
|
| 74 |
if ( this.skipExportIfExists && this.exportDirectory.isDirectory() ) |
| 75 |
{ |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
export(); |
| 80 |
} |
| 81 |
|
| 82 |
protected File getExportDirectory() |
| 83 |
{ |
| 84 |
return this.exportDirectory; |
| 85 |
} |
| 86 |
|
| 87 |
public void setExportDirectory( File exportDirectory ) |
| 88 |
{ |
| 89 |
this.exportDirectory = exportDirectory; |
| 90 |
} |
| 91 |
|
| 92 |
protected void export() |
| 93 |
throws MojoExecutionException |
| 94 |
{ |
| 95 |
if ( this.exportDirectory.getPath().contains( "${project.basedir}" ) ) |
| 96 |
{ |
| 97 |
//project.basedir is not set under maven 3.x when run without a project |
| 98 |
this.exportDirectory = new File( this.getBasedir(), "target/export" ); |
| 99 |
} |
| 100 |
try |
| 101 |
{ |
| 102 |
ScmRepository repository = getScmRepository(); |
| 103 |
|
| 104 |
try |
| 105 |
{ |
| 106 |
if ( this.exportDirectory.exists() ) |
| 107 |
{ |
| 108 |
this.getLog().info( "Removing " + this.exportDirectory ); |
| 109 |
|
| 110 |
FileUtils.deleteDirectory( this.exportDirectory ); |
| 111 |
} |
| 112 |
} |
| 113 |
catch ( IOException e ) |
| 114 |
{ |
| 115 |
throw new MojoExecutionException( "Cannot remove " + getExportDirectory() ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( !this.exportDirectory.mkdirs() ) |
| 119 |
{ |
| 120 |
throw new MojoExecutionException( "Cannot create " + this.exportDirectory ); |
| 121 |
} |
| 122 |
|
| 123 |
ExportScmResult result = getScmManager().export( repository, |
| 124 |
new ScmFileSet( this.exportDirectory.getAbsoluteFile() ), |
| 125 |
getScmVersion( scmVersionType, scmVersion ) ); |
| 126 |
|
| 127 |
checkResult( result ); |
| 128 |
|
| 129 |
handleExcludesIncludesAfterCheckoutAndExport( this.exportDirectory ); |
| 130 |
} |
| 131 |
catch ( ScmException e ) |
| 132 |
{ |
| 133 |
throw new MojoExecutionException( "Cannot run export command : ", e ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |