View Javadoc

1   package org.apache.maven.scm.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Pull the project source from the configured scm and execute the configured goals.
38   *
39   * @author <a href="dantran@gmail.com">Dan T. Tran</a>
40   * @version $Id: BootstrapMojo.java 681929 2008-08-02 02:48:19Z dantran $
41   * @goal bootstrap
42   * @description Boostrap a project
43   * @requiresProject false
44   */
45  public class BootstrapMojo
46      extends CheckoutMojo
47  {
48      /**
49       * The goals to run on the clean checkout of a project for the bootstrap goal.
50       * If none are specified, then the default goal for the project is executed.
51       * Multiple goals should be comma separated.
52       *
53       * @parameter expression="${goals}"
54       */
55      private String goals;
56  
57      /**
58       * A list of profiles to run with the goals.
59       * Multiple profiles must be comma separated with no spaces.
60       *
61       * @parameter expression="${profiles}"
62       */
63      private String profiles;
64  
65      /**
66       * The subdirectory (under the project directory) in which to run the goals.
67       * The project directory is the same as the checkout directory in most cases,
68       * but for some SCMs, it is a subdirectory of the checkout directory.
69       *
70       * @parameter expression="${goalsDirectory}" default-value=""
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       * @param relativePathProjectDirectory the project directory's path relative to the checkout
87       *                                     directory; or "" if they are the same
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      * Determines the path of the working directory. By default, this is the checkout directory. For some SCMs, the project root directory
144      * is not the checkout directory itself, but a SCM-specific subdirectory. The build can furthermore optionally be executed in a
145      * subdirectory of this project directory, in case
146      *
147      * @param checkoutDirectory
148      * @param relativePathProjectDirectory
149      * @param goalsDirectory
150      * @return
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      * Add system environment variables
177      * Moved to plexus-utils 1.0.5
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         //If this is windows set the shell to command.com or cmd.exe with correct arguments.
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             // System.out.println( key + " = " + value );
234         }
235 
236         return envVars;
237     }
238 }