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 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 super.execute();
78
79 if ( this.getCheckoutResult() != null )
80 {
81 runGoals( this.getCheckoutResult().getRelativePathProjectDirectory() );
82 }
83 }
84
85
86
87
88
89 private void runGoals( String relativePathProjectDirectory )
90 throws MojoExecutionException
91 {
92 Commandline cl = new Commandline();
93
94 try
95 {
96 addSystemEnvironment( cl );
97 }
98 catch ( Exception e )
99 {
100 throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e );
101 }
102
103 cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
104
105 cl.setExecutable( "mvn" );
106
107 cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(),
108 relativePathProjectDirectory, goalsDirectory ) );
109
110 if ( this.goals != null )
111 {
112 String[] tokens = StringUtils.split( this.goals, ", " );
113
114 for ( int i = 0; i < tokens.length; ++i )
115 {
116 cl.createArgument().setValue( tokens[i] );
117 }
118 }
119
120 if ( ! StringUtils.isEmpty( this.profiles ) )
121 {
122 cl.createArgument().setValue( "-P" + this.profiles );
123 }
124
125 StreamConsumer consumer = new DefaultConsumer();
126
127 try
128 {
129 int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
130
131 if ( result != 0 )
132 {
133 throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." );
134 }
135 }
136 catch ( CommandLineException e )
137 {
138 throw new MojoExecutionException( "Can't run goal " + goals, e );
139 }
140 }
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 if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
157 {
158 projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
159 }
160 else
161 {
162 projectDirectory = checkoutDirectory;
163 }
164
165 if ( StringUtils.isEmpty( goalsDirectory ) )
166 {
167 return projectDirectory.getPath();
168 }
169 else
170 {
171 return new File( projectDirectory, goalsDirectory ).getPath();
172 }
173 }
174
175
176
177
178
179 private void addSystemEnvironment( Commandline cl )
180 throws Exception
181 {
182 Properties envVars = getSystemEnvVars();
183
184 for ( Iterator i = envVars.keySet().iterator(); i.hasNext(); )
185 {
186 String key = (String) i.next();
187
188 cl.addEnvironment( key, envVars.getProperty( key ) );
189 }
190 }
191
192 private Properties getSystemEnvVars()
193 throws Exception
194 {
195 Process p = null;
196
197 Properties envVars = new Properties();
198
199 Runtime r = Runtime.getRuntime();
200
201 String os = System.getProperty( "os.name" ).toLowerCase();
202
203
204 if ( os.indexOf( "windows" ) != -1 )
205 {
206 if ( os.indexOf( "95" ) != -1 || os.indexOf( "98" ) != -1 || os.indexOf( "Me" ) != -1 )
207 {
208 p = r.exec( "command.com /c set" );
209 }
210 else
211 {
212 p = r.exec( "cmd.exe /c set" );
213 }
214 }
215 else
216 {
217 p = r.exec( "env" );
218 }
219
220 BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
221
222 String line;
223
224 while ( ( line = br.readLine() ) != null )
225 {
226 int idx = line.indexOf( '=' );
227
228 String key = line.substring( 0, idx );
229
230 String value = line.substring( idx + 1 );
231
232 envVars.setProperty( key, value );
233
234 }
235
236 return envVars;
237 }
238 }