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