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 public void execute() throws MojoExecutionException {
96 super.execute();
97
98
99 checkoutResult = null;
100 if (!getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists) {
101 checkoutResult = checkout();
102 }
103 }
104
105 protected File getCheckoutDirectory() {
106 if (this.checkoutDirectory.getPath().contains("${project.basedir}")) {
107
108 this.checkoutDirectory = new File(this.getBasedir(), "target/checkout");
109 }
110 return this.checkoutDirectory;
111 }
112
113 public void setCheckoutDirectory(File checkoutDirectory) {
114 this.checkoutDirectory = checkoutDirectory;
115 }
116
117 protected ScmResult checkout() throws MojoExecutionException {
118 try {
119 ScmRepository repository = getScmRepository();
120
121 this.prepareOutputDirectory(getCheckoutDirectory());
122
123 ScmResult result = null;
124
125 ScmFileSet fileSet = new ScmFileSet(getCheckoutDirectory().getAbsoluteFile());
126 if (useExport) {
127 result = getScmManager().export(repository, fileSet, getScmVersion(scmVersionType, scmVersion));
128 } else {
129 CommandParameters parameters = new CommandParameters();
130 parameters.setString(CommandParameter.RECURSIVE, Boolean.toString(true));
131 parameters.setString(CommandParameter.SHALLOW, Boolean.toString(shallow));
132 result = getScmManager()
133 .getProviderByRepository(repository)
134 .checkOut(repository, fileSet, getScmVersion(scmVersionType, scmVersion), parameters);
135 }
136
137 checkResult(result);
138
139 handleExcludesIncludesAfterCheckoutAndExport(this.checkoutDirectory);
140
141 return result;
142 } catch (ScmException e) {
143 throw new MojoExecutionException("Cannot run checkout command : ", e);
144 }
145 }
146
147 private void prepareOutputDirectory(File ouputDirectory) throws MojoExecutionException {
148 try {
149 this.getLog().info("Removing " + ouputDirectory);
150
151 FileUtils.deleteDirectory(getCheckoutDirectory());
152 } catch (IOException e) {
153 throw new MojoExecutionException("Cannot remove " + ouputDirectory);
154 }
155
156 if (!getCheckoutDirectory().mkdirs()) {
157 throw new MojoExecutionException("Cannot create " + ouputDirectory);
158 }
159 }
160
161 protected ScmResult getCheckoutResult() {
162 return checkoutResult;
163 }
164 }