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 java.io.File;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
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  /**
35   * Pull the project source from the configured scm and execute the configured goals.
36   *
37   * @author <a href="dantran@gmail.com">Dan T. Tran</a>
38   * @version $Id: BootstrapMojo.java 733515 2009-01-11 20:37:15Z dantran $
39   * @goal bootstrap
40   * @requiresProject false
41   */
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 expression="${goals}"
51       */
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 expression="${profiles}"
59       */
60      private String profiles;
61  
62      /**
63       * The subdirectory (under the project directory) in which to run the goals.
64       * The project directory is the same as the checkout directory in most cases,
65       * but for some SCMs, it is a subdirectory of the checkout directory.
66       *
67       * @parameter expression="${goalsDirectory}" default-value=""
68       */
69      private String goalsDirectory;
70  
71      /** {@inheritDoc} */
72      public void execute()
73          throws MojoExecutionException
74      {
75          super.execute();
76  
77          if ( this.getCheckoutResult() != null )
78          {
79              
80              ScmResult checkoutResult = this.getCheckoutResult();
81              
82              //At the time of useExport feature is requested only SVN and and CVS have export command implemented
83              // we will deal with this as more user using this feature specially clearcase where we need to 
84              // add relativePathProjectDirectory support to ExportScmResult
85              String relativePathProjectDirectory = "";
86              if ( checkoutResult instanceof CheckOutScmResult )
87              {
88                  relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult).getRelativePathProjectDirectory();
89              }
90  
91              runGoals( relativePathProjectDirectory );
92          }
93      }
94  
95      /**
96       * @param relativePathProjectDirectory the project directory's path relative to the checkout
97       *                                     directory; or "" if they are the same
98       * @throws MojoExecutionException if any
99       */
100     private void runGoals( String relativePathProjectDirectory )
101         throws MojoExecutionException
102     {
103         Commandline cl = new Commandline();
104         try
105         {
106             cl.addSystemEnvironment();
107         }
108         catch ( Exception e )
109         {
110             throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e );
111         }
112         cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
113         cl.setExecutable( "mvn" );
114         cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(),
115                                                                relativePathProjectDirectory, goalsDirectory ) );
116 
117         if ( this.goals != null )
118         {
119             String[] tokens = StringUtils.split( this.goals, ", " );
120 
121             for ( int i = 0; i < tokens.length; ++i )
122             {
123                 cl.createArg().setValue( tokens[i] );
124             }
125         }
126 
127         if ( ! StringUtils.isEmpty( this.profiles ) )
128         {
129             cl.createArg().setValue( "-P" + this.profiles );
130         }
131 
132         StreamConsumer consumer = new DefaultConsumer();
133 
134         try
135         {
136             int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
137 
138             if ( result != 0 )
139             {
140                 throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." );
141             }
142         }
143         catch ( CommandLineException e )
144         {
145             throw new MojoExecutionException( "Can't run goal " + goals, e );
146         }
147     }
148 
149     /**
150      * Determines the path of the working directory. By default, this is the checkout directory. For some SCMs,
151      * the project root directory is not the checkout directory itself, but a SCM-specific subdirectory. The
152      * build can furthermore optionally be executed in a subdirectory of this project directory, in case.
153      *
154      * @param checkoutDirectory
155      * @param relativePathProjectDirectory
156      * @param goalsDirectory
157      * @return
158      */
159     protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory,
160                                                     String goalsDirectory )
161     {
162         File projectDirectory;
163         if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
164         {
165             projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
166         }
167         else
168         {
169             projectDirectory = checkoutDirectory;
170         }
171 
172         if ( StringUtils.isEmpty( goalsDirectory ) )
173         {
174             return projectDirectory.getPath();
175         }
176 
177         return new File( projectDirectory, goalsDirectory ).getPath();
178     }
179 }