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