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