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.apache.maven.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.scm.ScmResult;
26  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
27  import org.codehaus.plexus.util.StringUtils;
28  import org.codehaus.plexus.util.cli.CommandLineException;
29  import org.codehaus.plexus.util.cli.CommandLineUtils;
30  import org.codehaus.plexus.util.cli.Commandline;
31  import org.codehaus.plexus.util.cli.DefaultConsumer;
32  import org.codehaus.plexus.util.cli.StreamConsumer;
33  
34  import java.io.File;
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   */
41  @Mojo( name = "bootstrap", requiresProject = false )
42  public class BootstrapMojo
43      extends CheckoutMojo
44  {
45      /**
46       * The goals to run on the clean checkout of a project for the bootstrap goal.
47       * If none are specified, then the default goal for the project is executed.
48       * Multiple goals should be comma separated.
49       */
50      @Parameter( property = "goals" )
51      private String goals;
52  
53      /**
54       * A list of profiles to run with the goals.
55       * Multiple profiles must be comma separated with no spaces.
56       */
57      @Parameter( property = "profiles" )
58      private String profiles;
59  
60      /**
61       * The subdirectory (under the project directory) in which to run the goals.
62       * The project directory is the same as the checkout directory in most cases,
63       * but for some SCMs, it is a subdirectory of the checkout directory.
64       */
65      @Parameter( property = "goalsDirectory", defaultValue = "" )
66      private String goalsDirectory;
67      
68      /**
69       * The path where you maven is installed
70       */
71      @Parameter( property = "mavenHome", defaultValue = "" )
72      private String mavenHome;
73  
74      /** {@inheritDoc} */
75      public void execute()
76          throws MojoExecutionException
77      {
78          super.execute();
79  
80          if ( this.getCheckoutResult() != null )
81          {
82              
83              ScmResult checkoutResult = this.getCheckoutResult();
84              
85              //At the time of useExport feature is requested only SVN and and CVS have export command implemented
86              // we will deal with this as more user using this feature specially clearcase where we need to 
87              // add relativePathProjectDirectory support to ExportScmResult
88              String relativePathProjectDirectory = "";
89              if ( checkoutResult instanceof CheckOutScmResult )
90              {
91                  relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult ).getRelativePathProjectDirectory();
92              }
93  
94              runGoals( relativePathProjectDirectory );
95          }
96      }
97  
98      /**
99       * @param relativePathProjectDirectory the project directory's path relative to the checkout
100      *                                     directory; or "" if they are the same
101      * @throws MojoExecutionException if any
102      */
103     private void runGoals( String relativePathProjectDirectory )
104         throws MojoExecutionException
105     {
106         Commandline cl = new Commandline();
107         try
108         {
109             cl.addSystemEnvironment();
110         }
111         catch ( Exception e )
112         {
113             throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e );
114         }
115         cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
116 
117         if ( "".equals( this.mavenHome ) )
118         {
119             cl.setExecutable( "mvn" );
120         }
121         else
122         {
123             cl.setExecutable( this.mavenHome.concat( "/bin/mvn" ) );
124         }
125 
126         cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(), //
127                                            relativePathProjectDirectory, goalsDirectory ) );
128 
129         if ( this.goals != null )
130         {
131             String[] tokens = StringUtils.split( this.goals, ", " );
132 
133             for ( int i = 0; i < tokens.length; ++i )
134             {
135                 cl.createArg().setValue( tokens[i] );
136             }
137         }
138 
139         if ( ! StringUtils.isEmpty( this.profiles ) )
140         {
141             cl.createArg().setValue( "-P" + this.profiles );
142         }
143 
144         StreamConsumer consumer = new DefaultConsumer();
145 
146         try
147         {
148             int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
149 
150             if ( result != 0 )
151             {
152                 throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." );
153             }
154         }
155         catch ( CommandLineException e )
156         {
157             throw new MojoExecutionException( "Can't run goal " + goals, e );
158         }
159     }
160 
161     /**
162      * Determines the path of the working directory. By default, this is the checkout directory. For some SCMs,
163      * the project root directory is not the checkout directory itself, but a SCM-specific subdirectory. The
164      * build can furthermore optionally be executed in a subdirectory of this project directory, in case.
165      *
166      * @param checkoutDirectory
167      * @param relativePathProjectDirectory
168      * @param goalsDirectory
169      * @return
170      */
171     protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory,
172                                                     String goalsDirectory )
173     {
174         File projectDirectory;
175         if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
176         {
177             projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
178         }
179         else
180         {
181             projectDirectory = checkoutDirectory;
182         }
183 
184         if ( StringUtils.isEmpty( goalsDirectory ) )
185         {
186             return projectDirectory.getPath();
187         }
188 
189         return new File( projectDirectory, goalsDirectory ).getPath();
190     }
191 }