View Javadoc
1   package org.apache.maven.scm.plugin;
2   
3   import java.io.File;
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   * http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
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.StringUtils;
31  import org.codehaus.plexus.util.cli.CommandLineException;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  import org.codehaus.plexus.util.cli.DefaultConsumer;
35  import org.codehaus.plexus.util.cli.StreamConsumer;
36  
37  /**
38   * Pull the project source from the configured scm and execute the configured goals.
39   *
40   * @author <a href="dantran@gmail.com">Dan T. Tran</a>
41   */
42  @Mojo( name = "bootstrap", requiresProject = false )
43  public class BootstrapMojo
44      extends CheckoutMojo
45  {
46      /**
47       * The goals to run on the clean checkout of a project for the bootstrap goal.
48       * If none are specified, then the default goal for the project is executed.
49       * Multiple goals should be comma separated.
50       */
51      @Parameter( property = "goals" )
52      private String goals;
53  
54      /**
55       * A list of profiles to run with the goals.
56       * Multiple profiles must be comma separated with no spaces.
57       */
58      @Parameter( property = "profiles" )
59      private String profiles;
60  
61      /**
62       * The subdirectory (under the project directory) in which to run the goals.
63       * The project directory is the same as the checkout directory in most cases,
64       * but for some SCMs, it is a subdirectory of the checkout directory.
65       */
66      @Parameter( property = "goalsDirectory" )
67      private String goalsDirectory;
68  
69      /**
70       * The path where your maven is installed
71       */
72      @Parameter( property = "mavenHome", defaultValue="${maven.home}")
73      private File mavenHome;
74  
75      /** {@inheritDoc} */
76      public void execute()
77          throws MojoExecutionException
78      {
79          super.execute();
80  
81          if ( this.getCheckoutResult() != null )
82          {
83  
84              ScmResult checkoutResult = this.getCheckoutResult();
85  
86              //At the time of useExport feature is requested only SVN and and CVS have export command implemented
87              // we will deal with this as more user using this feature specially clearcase where we need to
88              // add relativePathProjectDirectory support to ExportScmResult
89              String relativePathProjectDirectory = "";
90              if ( checkoutResult instanceof CheckOutScmResult )
91              {
92                  relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult ).getRelativePathProjectDirectory();
93              }
94  
95              runGoals( relativePathProjectDirectory );
96          }
97      }
98  
99      /**
100      * @param relativePathProjectDirectory the project directory's path relative to the checkout
101      *                                     directory; or "" if they are the same
102      * @throws MojoExecutionException if any
103      */
104     private void runGoals( String relativePathProjectDirectory )
105         throws MojoExecutionException
106     {
107         Commandline cl = new Commandline();
108         try
109         {
110             cl.addSystemEnvironment();
111         }
112         catch ( Exception e )
113         {
114             throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e );
115         }
116         cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
117 
118         if ( this.mavenHome == null )
119         {
120             cl.setExecutable( "mvn" );//none windows only
121         }
122         else
123         {
124             String mvnPath = this.mavenHome.getAbsolutePath() + "/bin/mvn";
125             if ( Os.isFamily( "windows" ) )
126             {
127                 String winMvnPath = mvnPath + ".cmd";
128                 if ( !new File( winMvnPath ).exists() )
129                 {
130                     winMvnPath = mvnPath + ".bat";
131                 }
132                 mvnPath = winMvnPath;
133             }
134             cl.setExecutable( mvnPath );
135         }
136 
137         cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(), //
138                                            relativePathProjectDirectory, goalsDirectory ) );
139 
140         if ( this.goals != null )
141         {
142             String[] tokens = StringUtils.split( this.goals, ", " );
143 
144             for ( int i = 0; i < tokens.length; ++i )
145             {
146                 cl.createArg().setValue( tokens[i] );
147             }
148         }
149 
150         if ( ! StringUtils.isEmpty( this.profiles ) )
151         {
152             cl.createArg().setValue( "-P" + this.profiles );
153         }
154 
155         StreamConsumer consumer = new DefaultConsumer();
156 
157         try
158         {
159             int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
160 
161             if ( result != 0 )
162             {
163                 throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." );
164             }
165         }
166         catch ( CommandLineException e )
167         {
168             throw new MojoExecutionException( "Can't run goal " + goals, e );
169         }
170     }
171 
172     /**
173      * Determines the path of the working directory. By default, this is the checkout directory. For some SCMs,
174      * the project root directory is not the checkout directory itself, but a SCM-specific subdirectory. The
175      * build can furthermore optionally be executed in a subdirectory of this project directory, in case.
176      *
177      * @param checkoutDirectory
178      * @param relativePathProjectDirectory
179      * @param goalsDirectory
180      * @return
181      */
182     protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory,
183                                                     String goalsDirectory )
184     {
185         File projectDirectory;
186         if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
187         {
188             projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
189         }
190         else
191         {
192             projectDirectory = checkoutDirectory;
193         }
194 
195         if ( StringUtils.isEmpty( goalsDirectory ) )
196         {
197             return projectDirectory.getPath();
198         }
199 
200         return new File( projectDirectory, goalsDirectory ).getPath();
201     }
202 }