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 org.apache.maven.plugin.MojoExecutionException; |
23 | |
import org.codehaus.plexus.util.StringUtils; |
24 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
25 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
26 | |
import org.codehaus.plexus.util.cli.Commandline; |
27 | |
import org.codehaus.plexus.util.cli.DefaultConsumer; |
28 | |
import org.codehaus.plexus.util.cli.StreamConsumer; |
29 | |
|
30 | |
import java.io.BufferedReader; |
31 | |
import java.io.File; |
32 | |
import java.io.InputStreamReader; |
33 | |
import java.util.Iterator; |
34 | |
import java.util.Properties; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 1 | public class BootstrapMojo |
46 | |
extends CheckoutMojo |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
private String goals; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private String profiles; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private String goalsDirectory; |
73 | |
|
74 | |
public void execute() |
75 | |
throws MojoExecutionException |
76 | |
{ |
77 | 0 | super.execute(); |
78 | |
|
79 | 0 | if ( this.getCheckoutResult() != null ) |
80 | |
{ |
81 | 0 | runGoals( this.getCheckoutResult().getRelativePathProjectDirectory() ); |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private void runGoals( String relativePathProjectDirectory ) |
90 | |
throws MojoExecutionException |
91 | |
{ |
92 | 0 | Commandline cl = new Commandline(); |
93 | |
|
94 | |
try |
95 | |
{ |
96 | 0 | addSystemEnvironment( cl ); |
97 | |
} |
98 | 0 | catch ( Exception e ) |
99 | |
{ |
100 | 0 | throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e ); |
101 | 0 | } |
102 | |
|
103 | 0 | cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" ); |
104 | |
|
105 | 0 | cl.setExecutable( "mvn" ); |
106 | |
|
107 | 0 | cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(), |
108 | |
relativePathProjectDirectory, goalsDirectory ) ); |
109 | |
|
110 | 0 | if ( this.goals != null ) |
111 | |
{ |
112 | 0 | String[] tokens = StringUtils.split( this.goals, ", " ); |
113 | |
|
114 | 0 | for ( int i = 0; i < tokens.length; ++i ) |
115 | |
{ |
116 | 0 | cl.createArgument().setValue( tokens[i] ); |
117 | |
} |
118 | |
} |
119 | |
|
120 | 0 | if ( ! StringUtils.isEmpty( this.profiles ) ) |
121 | |
{ |
122 | 0 | cl.createArgument().setValue( "-P" + this.profiles ); |
123 | |
} |
124 | |
|
125 | 0 | StreamConsumer consumer = new DefaultConsumer(); |
126 | |
|
127 | |
try |
128 | |
{ |
129 | 0 | int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer ); |
130 | |
|
131 | 0 | if ( result != 0 ) |
132 | |
{ |
133 | 0 | throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." ); |
134 | |
} |
135 | |
} |
136 | 0 | catch ( CommandLineException e ) |
137 | |
{ |
138 | 0 | throw new MojoExecutionException( "Can't run goal " + goals, e ); |
139 | 0 | } |
140 | 0 | } |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory, |
153 | |
String goalsDirectory ) |
154 | |
{ |
155 | |
File projectDirectory; |
156 | 9 | if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) ) |
157 | |
{ |
158 | 6 | projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory ); |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | 3 | projectDirectory = checkoutDirectory; |
163 | |
} |
164 | |
|
165 | 9 | if ( StringUtils.isEmpty( goalsDirectory ) ) |
166 | |
{ |
167 | 5 | return projectDirectory.getPath(); |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 4 | return new File( projectDirectory, goalsDirectory ).getPath(); |
172 | |
} |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
private void addSystemEnvironment( Commandline cl ) |
180 | |
throws Exception |
181 | |
{ |
182 | 0 | Properties envVars = getSystemEnvVars(); |
183 | |
|
184 | 0 | for ( Iterator i = envVars.keySet().iterator(); i.hasNext(); ) |
185 | |
{ |
186 | 0 | String key = (String) i.next(); |
187 | |
|
188 | 0 | cl.addEnvironment( key, envVars.getProperty( key ) ); |
189 | 0 | } |
190 | 0 | } |
191 | |
|
192 | |
private Properties getSystemEnvVars() |
193 | |
throws Exception |
194 | |
{ |
195 | 0 | Process p = null; |
196 | |
|
197 | 0 | Properties envVars = new Properties(); |
198 | |
|
199 | 0 | Runtime r = Runtime.getRuntime(); |
200 | |
|
201 | 0 | String os = System.getProperty( "os.name" ).toLowerCase(); |
202 | |
|
203 | |
|
204 | 0 | if ( os.indexOf( "windows" ) != -1 ) |
205 | |
{ |
206 | 0 | if ( os.indexOf( "95" ) != -1 || os.indexOf( "98" ) != -1 || os.indexOf( "Me" ) != -1 ) |
207 | |
{ |
208 | 0 | p = r.exec( "command.com /c set" ); |
209 | |
} |
210 | |
else |
211 | |
{ |
212 | 0 | p = r.exec( "cmd.exe /c set" ); |
213 | |
} |
214 | |
} |
215 | |
else |
216 | |
{ |
217 | 0 | p = r.exec( "env" ); |
218 | |
} |
219 | |
|
220 | 0 | BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); |
221 | |
|
222 | |
String line; |
223 | |
|
224 | 0 | while ( ( line = br.readLine() ) != null ) |
225 | |
{ |
226 | 0 | int idx = line.indexOf( '=' ); |
227 | |
|
228 | 0 | String key = line.substring( 0, idx ); |
229 | |
|
230 | 0 | String value = line.substring( idx + 1 ); |
231 | |
|
232 | 0 | envVars.setProperty( key, value ); |
233 | |
|
234 | 0 | } |
235 | |
|
236 | 0 | return envVars; |
237 | |
} |
238 | |
} |