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
024import org.apache.maven.plugin.MojoExecutionException;
025import org.apache.maven.plugins.annotations.Mojo;
026import org.apache.maven.plugins.annotations.Parameter;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.checkout.CheckOutScmResult;
029import org.codehaus.plexus.util.StringUtils;
030import org.codehaus.plexus.util.cli.CommandLineException;
031import org.codehaus.plexus.util.cli.CommandLineUtils;
032import org.codehaus.plexus.util.cli.Commandline;
033import org.codehaus.plexus.util.cli.DefaultConsumer;
034import org.codehaus.plexus.util.cli.StreamConsumer;
035
036/**
037 * Pull the project source from the configured scm and execute the configured goals.
038 *
039 * @author <a href="dantran@gmail.com">Dan T. Tran</a>
040 */
041@Mojo( name = "bootstrap", requiresProject = false )
042public class BootstrapMojo
043    extends CheckoutMojo
044{
045    /**
046     * The goals to run on the clean checkout of a project for the bootstrap goal.
047     * If none are specified, then the default goal for the project is executed.
048     * Multiple goals should be comma separated.
049     */
050    @Parameter( property = "goals" )
051    private String goals;
052
053    /**
054     * A list of profiles to run with the goals.
055     * Multiple profiles must be comma separated with no spaces.
056     */
057    @Parameter( property = "profiles" )
058    private String profiles;
059
060    /**
061     * The subdirectory (under the project directory) in which to run the goals.
062     * The project directory is the same as the checkout directory in most cases,
063     * but for some SCMs, it is a subdirectory of the checkout directory.
064     */
065    @Parameter( property = "goalsDirectory", defaultValue = "" )
066    private String goalsDirectory;
067
068    /** {@inheritDoc} */
069    public void execute()
070        throws MojoExecutionException
071    {
072        super.execute();
073
074        if ( this.getCheckoutResult() != null )
075        {
076            
077            ScmResult checkoutResult = this.getCheckoutResult();
078            
079            //At the time of useExport feature is requested only SVN and and CVS have export command implemented
080            // we will deal with this as more user using this feature specially clearcase where we need to 
081            // add relativePathProjectDirectory support to ExportScmResult
082            String relativePathProjectDirectory = "";
083            if ( checkoutResult instanceof CheckOutScmResult )
084            {
085                relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult ).getRelativePathProjectDirectory();
086            }
087
088            runGoals( relativePathProjectDirectory );
089        }
090    }
091
092    /**
093     * @param relativePathProjectDirectory the project directory's path relative to the checkout
094     *                                     directory; or "" if they are the same
095     * @throws MojoExecutionException if any
096     */
097    private void runGoals( String relativePathProjectDirectory )
098        throws MojoExecutionException
099    {
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}