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
23 import org.apache.commons.lang3.StringUtils;
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.ScmResult;
28 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
29 import org.codehaus.plexus.util.Os;
30 import org.codehaus.plexus.util.cli.CommandLineException;
31 import org.codehaus.plexus.util.cli.CommandLineUtils;
32 import org.codehaus.plexus.util.cli.Commandline;
33 import org.codehaus.plexus.util.cli.DefaultConsumer;
34 import org.codehaus.plexus.util.cli.StreamConsumer;
35
36
37
38
39
40
41 @Mojo(name = "bootstrap", requiresProject = false)
42 public class BootstrapMojo extends CheckoutMojo {
43
44
45
46
47
48 @Parameter(property = "goals")
49 private String goals;
50
51
52
53
54
55 @Parameter(property = "profiles")
56 private String profiles;
57
58
59
60
61
62
63 @Parameter(property = "goalsDirectory")
64 private String goalsDirectory;
65
66
67
68
69 @Parameter(property = "mavenHome", defaultValue = "${maven.home}")
70 private File mavenHome;
71
72
73 public void execute() throws MojoExecutionException {
74 super.execute();
75
76 if (this.getCheckoutResult() != null) {
77
78 ScmResult checkoutResult = this.getCheckoutResult();
79
80
81
82 String relativePathProjectDirectory = "";
83 if (checkoutResult instanceof CheckOutScmResult) {
84 relativePathProjectDirectory = ((CheckOutScmResult) checkoutResult).getRelativePathProjectDirectory();
85 }
86
87 runGoals(relativePathProjectDirectory);
88 }
89 }
90
91
92
93
94
95
96 private void runGoals(String relativePathProjectDirectory) throws MojoExecutionException {
97 Commandline cl = new Commandline();
98 try {
99 cl.addSystemEnvironment();
100 } catch (Exception e) {
101 throw new MojoExecutionException("Can't add system environment variables to mvn command line.", e);
102 }
103 cl.addEnvironment("MAVEN_TERMINATE_CMD", "on");
104
105 if (this.mavenHome == null) {
106 cl.setExecutable("mvn");
107 } else {
108 String mvnPath = this.mavenHome.getAbsolutePath() + "/bin/mvn";
109 if (Os.isFamily("windows")) {
110 String winMvnPath = mvnPath + ".cmd";
111 if (!new File(winMvnPath).exists()) {
112 winMvnPath = mvnPath + ".bat";
113 }
114 mvnPath = winMvnPath;
115 }
116 cl.setExecutable(mvnPath);
117 }
118
119 cl.setWorkingDirectory(determineWorkingDirectoryPath(
120 this.getCheckoutDirectory(),
121 relativePathProjectDirectory,
122 goalsDirectory));
123
124 if (this.goals != null) {
125 String[] tokens = StringUtils.split(this.goals, ", ");
126
127 for (int i = 0; i < tokens.length; ++i) {
128 cl.createArg().setValue(tokens[i]);
129 }
130 }
131
132 if (!(this.profiles == null || this.profiles.isEmpty())) {
133 cl.createArg().setValue("-P" + this.profiles);
134 }
135
136 StreamConsumer consumer = new DefaultConsumer();
137
138 try {
139 int result = CommandLineUtils.executeCommandLine(cl, consumer, consumer);
140
141 if (result != 0) {
142 throw new MojoExecutionException("Result of mvn execution is: \'" + result + "\'. Release failed.");
143 }
144 } catch (CommandLineException e) {
145 throw new MojoExecutionException("Can't run goal " + goals, e);
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158
159 protected String determineWorkingDirectoryPath(
160 File checkoutDirectory, String relativePathProjectDirectory, String goalsDirectory) {
161 File projectDirectory;
162 if (relativePathProjectDirectory != null && !relativePathProjectDirectory.isEmpty()) {
163 projectDirectory = new File(checkoutDirectory, relativePathProjectDirectory);
164 } else {
165 projectDirectory = checkoutDirectory;
166 }
167
168 if (goalsDirectory == null || goalsDirectory.isEmpty()) {
169 return projectDirectory.getPath();
170 }
171
172 return new File(projectDirectory, goalsDirectory).getPath();
173 }
174 }