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.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.StringUtils;
30  import org.codehaus.plexus.util.cli.CommandLineException;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  import org.codehaus.plexus.util.cli.DefaultConsumer;
34  import org.codehaus.plexus.util.cli.StreamConsumer;
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      /** {@inheritDoc} */
69      public void execute()
70          throws MojoExecutionException
71      {
72          super.execute();
73  
74          if ( this.getCheckoutResult() != null )
75          {
76              
77              ScmResult checkoutResult = this.getCheckoutResult();
78              
79              //At the time of useExport feature is requested only SVN and and CVS have export command implemented
80              // we will deal with this as more user using this feature specially clearcase where we need to 
81              // add relativePathProjectDirectory support to ExportScmResult
82              String relativePathProjectDirectory = "";
83              if ( checkoutResult instanceof CheckOutScmResult )
84              {
85                  relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult).getRelativePathProjectDirectory();
86              }
87  
88              runGoals( relativePathProjectDirectory );
89          }
90      }
91  
92      /**
93       * @param relativePathProjectDirectory the project directory's path relative to the checkout
94       *                                     directory; or "" if they are the same
95       * @throws MojoExecutionException if any
96       */
97      private void runGoals( String relativePathProjectDirectory )
98          throws MojoExecutionException
99      {
100         Commandline cl = new Commandline();
101         try
102         {
103             cl.addSystemEnvironment();
104         }
105         catch ( Exception e )
106         {
107             throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e );
108         }
109         cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
110         cl.setExecutable( "mvn" );
111         cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(),
112                                                                relativePathProjectDirectory, goalsDirectory ) );
113 
114         if ( this.goals != null )
115         {
116             String[] tokens = StringUtils.split( this.goals, ", " );
117 
118             for ( int i = 0; i < tokens.length; ++i )
119             {
120                 cl.createArg().setValue( tokens[i] );
121             }
122         }
123 
124         if ( ! StringUtils.isEmpty( this.profiles ) )
125         {
126             cl.createArg().setValue( "-P" + this.profiles );
127         }
128 
129         StreamConsumer consumer = new DefaultConsumer();
130 
131         try
132         {
133             int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
134 
135             if ( result != 0 )
136             {
137                 throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." );
138             }
139         }
140         catch ( CommandLineException e )
141         {
142             throw new MojoExecutionException( "Can't run goal " + goals, e );
143         }
144     }
145 
146     /**
147      * Determines the path of the working directory. By default, this is the checkout directory. For some SCMs,
148      * the project root directory is not the checkout directory itself, but a SCM-specific subdirectory. The
149      * build can furthermore optionally be executed in a subdirectory of this project directory, in case.
150      *
151      * @param checkoutDirectory
152      * @param relativePathProjectDirectory
153      * @param goalsDirectory
154      * @return
155      */
156     protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory,
157                                                     String goalsDirectory )
158     {
159         File projectDirectory;
160         if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
161         {
162             projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
163         }
164         else
165         {
166             projectDirectory = checkoutDirectory;
167         }
168 
169         if ( StringUtils.isEmpty( goalsDirectory ) )
170         {
171             return projectDirectory.getPath();
172         }
173 
174         return new File( projectDirectory, goalsDirectory ).getPath();
175     }
176 }