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 org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
26 import org.apache.maven.scm.repository.ScmRepository;
27 import org.codehaus.plexus.util.FileUtils;
28
29 import java.io.File;
30 import java.io.IOException;
31
32
33
34
35
36
37
38
39
40
41 public class CheckoutMojo
42 extends AbstractScmMojo
43 {
44
45
46
47
48
49 private File checkoutDirectory;
50
51
52
53
54
55
56 private boolean skipCheckoutIfExists = false;
57
58
59
60
61
62
63 private String scmVersionType;
64
65
66
67
68
69
70 private String scmVersion;
71
72
73
74
75 private CheckOutScmResult checkoutResult;
76
77 public void execute()
78 throws MojoExecutionException
79 {
80 super.execute();
81
82
83 checkoutResult = null;
84 if ( !getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists )
85 {
86 checkoutResult = checkout();
87 }
88 }
89
90 protected File getCheckoutDirectory()
91 {
92 return this.checkoutDirectory;
93 }
94
95 public void setCheckoutDirectory( File checkoutDirectory )
96 {
97 this.checkoutDirectory = checkoutDirectory;
98 }
99
100 protected CheckOutScmResult checkout()
101 throws MojoExecutionException
102 {
103 try
104 {
105 ScmRepository repository = getScmRepository();
106
107 try
108 {
109 this.getLog().info( "Removing " + getCheckoutDirectory() );
110
111 FileUtils.deleteDirectory( getCheckoutDirectory() );
112 }
113 catch ( IOException e )
114 {
115 throw new MojoExecutionException( "Cannot remove " + getCheckoutDirectory() );
116 }
117
118 if ( !getCheckoutDirectory().mkdirs() )
119 {
120 throw new MojoExecutionException( "Cannot create " + getCheckoutDirectory() );
121 }
122
123 CheckOutScmResult result = getScmManager().checkOut( repository, new ScmFileSet(
124 getCheckoutDirectory().getAbsoluteFile() ), getScmVersion( scmVersionType, scmVersion ) );
125
126 checkResult( result );
127
128 return result;
129 }
130 catch ( ScmException e )
131 {
132 throw new MojoExecutionException( "Cannot run checkout command : ", e );
133 }
134 }
135
136 protected CheckOutScmResult getCheckoutResult()
137 {
138 return checkoutResult;
139 }
140 }