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