1 package org.apache.maven.scm.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.ScmResult;
31 import org.apache.maven.scm.repository.ScmRepository;
32 import org.codehaus.plexus.util.FileUtils;
33
34
35
36
37
38
39 @Mojo( name = "checkout", requiresProject = false )
40 public class CheckoutMojo
41 extends AbstractScmMojo
42 {
43
44
45
46 @Parameter( property = "useExport", defaultValue = "false" )
47 private boolean useExport;
48
49
50
51
52 @Parameter( property = "checkoutDirectory", defaultValue = "${project.build.directory}/checkout" )
53 private File checkoutDirectory;
54
55
56
57
58 @Parameter( property = "skipCheckoutIfExists", defaultValue = "false" )
59 private boolean skipCheckoutIfExists = false;
60
61
62
63
64 @Parameter( property = "scmVersionType" )
65 private String scmVersionType;
66
67
68
69
70 @Parameter( property = "scmVersion" )
71 private String scmVersion;
72
73
74
75
76 private ScmResult checkoutResult;
77
78
79 public void execute()
80 throws MojoExecutionException
81 {
82 super.execute();
83
84
85 checkoutResult = null;
86 if ( !getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists )
87 {
88 checkoutResult = checkout();
89 }
90 }
91
92 protected File getCheckoutDirectory()
93 {
94 if ( this.checkoutDirectory.getPath().contains( "${project.basedir}" ) )
95 {
96
97 this.checkoutDirectory = new File( this.getBasedir(), "target/checkout" );
98 }
99 return this.checkoutDirectory;
100 }
101
102 public void setCheckoutDirectory( File checkoutDirectory )
103 {
104 this.checkoutDirectory = checkoutDirectory;
105 }
106
107 protected ScmResult checkout()
108 throws MojoExecutionException
109 {
110 try
111 {
112 ScmRepository repository = getScmRepository();
113
114 this.prepareOutputDirectory( getCheckoutDirectory() );
115
116 ScmResult result = null;
117
118 ScmFileSet fileSet = new ScmFileSet( getCheckoutDirectory().getAbsoluteFile() );
119 if ( useExport )
120 {
121 result = getScmManager().export( repository, fileSet, getScmVersion( scmVersionType, scmVersion ) );
122 }
123 else
124 {
125 result = getScmManager().checkOut( repository, fileSet, getScmVersion( scmVersionType, scmVersion ) );
126 }
127
128 checkResult( result );
129
130 handleExcludesIncludesAfterCheckoutAndExport( this.checkoutDirectory );
131
132 return result;
133 }
134 catch ( ScmException e )
135 {
136 throw new MojoExecutionException( "Cannot run checkout command : ", e );
137 }
138 }
139
140 private void prepareOutputDirectory( File ouputDirectory )
141 throws MojoExecutionException
142 {
143 try
144 {
145 this.getLog().info( "Removing " + ouputDirectory );
146
147 FileUtils.deleteDirectory( getCheckoutDirectory() );
148 }
149 catch ( IOException e )
150 {
151 throw new MojoExecutionException( "Cannot remove " + ouputDirectory );
152 }
153
154 if ( !getCheckoutDirectory().mkdirs() )
155 {
156 throw new MojoExecutionException( "Cannot create " + ouputDirectory );
157 }
158 }
159
160 protected ScmResult getCheckoutResult()
161 {
162 return checkoutResult;
163 }
164
165
166 }