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