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.scm.ScmException;
27 import org.apache.maven.scm.ScmFileSet;
28 import org.apache.maven.scm.ScmResult;
29 import org.apache.maven.scm.repository.ScmRepository;
30 import org.codehaus.plexus.util.FileUtils;
31
32
33
34
35
36
37
38
39
40 public class CheckoutMojo
41 extends AbstractScmMojo
42 {
43
44
45
46
47
48 private boolean useExport;
49
50
51
52
53
54
55 private File checkoutDirectory;
56
57
58
59
60
61
62 private boolean skipCheckoutIfExists = false;
63
64
65
66
67
68
69 private String scmVersionType;
70
71
72
73
74
75
76 private String scmVersion;
77
78
79
80
81 private ScmResult checkoutResult;
82
83
84 public void execute()
85 throws MojoExecutionException
86 {
87 super.execute();
88
89
90 checkoutResult = null;
91 if ( !getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists )
92 {
93 checkoutResult = checkout();
94 }
95 }
96
97 protected File getCheckoutDirectory()
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
131 handleExcludesIncludesAfterCheckoutAndExport( this.checkoutDirectory );
132
133 return result;
134 }
135 catch ( ScmException e )
136 {
137 throw new MojoExecutionException( "Cannot run checkout command : ", e );
138 }
139 }
140
141 private void prepareOutputDirectory( File ouputDirectory )
142 throws MojoExecutionException
143 {
144 try
145 {
146 this.getLog().info( "Removing " + ouputDirectory );
147
148 FileUtils.deleteDirectory( getCheckoutDirectory() );
149 }
150 catch ( IOException e )
151 {
152 throw new MojoExecutionException( "Cannot remove " + ouputDirectory );
153 }
154
155 if ( !getCheckoutDirectory().mkdirs() )
156 {
157 throw new MojoExecutionException( "Cannot create " + ouputDirectory );
158 }
159 }
160
161 protected ScmResult getCheckoutResult()
162 {
163 return checkoutResult;
164 }
165
166
167 }