001package org.apache.maven.scm.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023
024/*
025 * Licensed to the Apache Software Foundation (ASF) under one
026 * or more contributor license agreements.  See the NOTICE file
027 * distributed with this work for additional information
028 * regarding copyright ownership.  The ASF licenses this file
029 * to you under the Apache License, Version 2.0 (the
030 * "License"); you may not use this file except in compliance
031 * with the License.  You may obtain a copy of the License at
032 *
033 * http://www.apache.org/licenses/LICENSE-2.0
034 *
035 * Unless required by applicable law or agreed to in writing,
036 * software distributed under the License is distributed on an
037 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
038 * KIND, either express or implied.  See the License for the
039 * specific language governing permissions and limitations
040 * under the License.
041 */
042
043import org.apache.maven.plugin.MojoExecutionException;
044import org.apache.maven.plugins.annotations.Mojo;
045import org.apache.maven.plugins.annotations.Parameter;
046import org.apache.maven.scm.ScmResult;
047import org.apache.maven.scm.command.checkout.CheckOutScmResult;
048import org.codehaus.plexus.util.Os;
049import org.codehaus.plexus.util.StringUtils;
050import org.codehaus.plexus.util.cli.CommandLineException;
051import org.codehaus.plexus.util.cli.CommandLineUtils;
052import org.codehaus.plexus.util.cli.Commandline;
053import org.codehaus.plexus.util.cli.DefaultConsumer;
054import org.codehaus.plexus.util.cli.StreamConsumer;
055
056/**
057 * Pull the project source from the configured scm and execute the configured goals.
058 *
059 * @author <a href="dantran@gmail.com">Dan T. Tran</a>
060 */
061@Mojo( name = "bootstrap", requiresProject = false )
062public class BootstrapMojo
063    extends CheckoutMojo
064{
065    /**
066     * The goals to run on the clean checkout of a project for the bootstrap goal.
067     * If none are specified, then the default goal for the project is executed.
068     * Multiple goals should be comma separated.
069     */
070    @Parameter( property = "goals" )
071    private String goals;
072
073    /**
074     * A list of profiles to run with the goals.
075     * Multiple profiles must be comma separated with no spaces.
076     */
077    @Parameter( property = "profiles" )
078    private String profiles;
079
080    /**
081     * The subdirectory (under the project directory) in which to run the goals.
082     * The project directory is the same as the checkout directory in most cases,
083     * but for some SCMs, it is a subdirectory of the checkout directory.
084     */
085    @Parameter( property = "goalsDirectory" )
086    private String goalsDirectory;
087
088    /**
089     * The path where your maven is installed
090     */
091    @Parameter( property = "mavenHome", defaultValue = "${maven.home}" )
092    private File mavenHome;
093
094    /** {@inheritDoc} */
095    public void execute()
096        throws MojoExecutionException
097    {
098        super.execute();
099
100        if ( this.getCheckoutResult() != null )
101        {
102
103            ScmResult checkoutResult = this.getCheckoutResult();
104
105            //At the time of useExport feature is requested only SVN and and CVS have export command implemented
106            // we will deal with this as more user using this feature specially clearcase where we need to
107            // add relativePathProjectDirectory support to ExportScmResult
108            String relativePathProjectDirectory = "";
109            if ( checkoutResult instanceof CheckOutScmResult )
110            {
111                relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult ).getRelativePathProjectDirectory();
112            }
113
114            runGoals( relativePathProjectDirectory );
115        }
116    }
117
118    /**
119     * @param relativePathProjectDirectory the project directory's path relative to the checkout
120     *                                     directory; or "" if they are the same
121     * @throws MojoExecutionException if any
122     */
123    private void runGoals( String relativePathProjectDirectory )
124        throws MojoExecutionException
125    {
126        Commandline cl = new Commandline();
127        try
128        {
129            cl.addSystemEnvironment();
130        }
131        catch ( Exception e )
132        {
133            throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e );
134        }
135        cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
136
137        if ( this.mavenHome == null )
138        {
139            cl.setExecutable( "mvn" ); // none windows only
140        }
141        else
142        {
143            String mvnPath = this.mavenHome.getAbsolutePath() + "/bin/mvn";
144            if ( Os.isFamily( "windows" ) )
145            {
146                String winMvnPath = mvnPath + ".cmd";
147                if ( !new File( winMvnPath ).exists() )
148                {
149                    winMvnPath = mvnPath + ".bat";
150                }
151                mvnPath = winMvnPath;
152            }
153            cl.setExecutable( mvnPath );
154        }
155
156        cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(), //
157                                           relativePathProjectDirectory, goalsDirectory ) );
158
159        if ( this.goals != null )
160        {
161            String[] tokens = StringUtils.split( this.goals, ", " );
162
163            for ( int i = 0; i < tokens.length; ++i )
164            {
165                cl.createArg().setValue( tokens[i] );
166            }
167        }
168
169        if ( ! StringUtils.isEmpty( this.profiles ) )
170        {
171            cl.createArg().setValue( "-P" + this.profiles );
172        }
173
174        StreamConsumer consumer = new DefaultConsumer();
175
176        try
177        {
178            int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
179
180            if ( result != 0 )
181            {
182                throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." );
183            }
184        }
185        catch ( CommandLineException e )
186        {
187            throw new MojoExecutionException( "Can't run goal " + goals, e );
188        }
189    }
190
191    /**
192     * Determines the path of the working directory. By default, this is the checkout directory. For some SCMs,
193     * the project root directory is not the checkout directory itself, but a SCM-specific subdirectory. The
194     * build can furthermore optionally be executed in a subdirectory of this project directory, in case.
195     *
196     * @param checkoutDirectory
197     * @param relativePathProjectDirectory
198     * @param goalsDirectory
199     * @return
200     */
201    protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory,
202                                                    String goalsDirectory )
203    {
204        File projectDirectory;
205        if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
206        {
207            projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
208        }
209        else
210        {
211            projectDirectory = checkoutDirectory;
212        }
213
214        if ( StringUtils.isEmpty( goalsDirectory ) )
215        {
216            return projectDirectory.getPath();
217        }
218
219        return new File( projectDirectory, goalsDirectory ).getPath();
220    }
221}