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 org.apache.maven.plugin.MojoExecutionException; 023import org.apache.maven.plugins.annotations.Mojo; 024import org.apache.maven.plugins.annotations.Parameter; 025import org.apache.maven.scm.ScmResult; 026import org.apache.maven.scm.command.checkout.CheckOutScmResult; 027import org.codehaus.plexus.util.StringUtils; 028import org.codehaus.plexus.util.cli.CommandLineException; 029import org.codehaus.plexus.util.cli.CommandLineUtils; 030import org.codehaus.plexus.util.cli.Commandline; 031import org.codehaus.plexus.util.cli.DefaultConsumer; 032import org.codehaus.plexus.util.cli.StreamConsumer; 033 034import java.io.File; 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 /** 069 * The path where you maven is installed 070 */ 071 @Parameter( property = "mavenHome", defaultValue = "" ) 072 private String mavenHome; 073 074 /** {@inheritDoc} */ 075 public void execute() 076 throws MojoExecutionException 077 { 078 super.execute(); 079 080 if ( this.getCheckoutResult() != null ) 081 { 082 083 ScmResult checkoutResult = this.getCheckoutResult(); 084 085 //At the time of useExport feature is requested only SVN and and CVS have export command implemented 086 // we will deal with this as more user using this feature specially clearcase where we need to 087 // add relativePathProjectDirectory support to ExportScmResult 088 String relativePathProjectDirectory = ""; 089 if ( checkoutResult instanceof CheckOutScmResult ) 090 { 091 relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult ).getRelativePathProjectDirectory(); 092 } 093 094 runGoals( relativePathProjectDirectory ); 095 } 096 } 097 098 /** 099 * @param relativePathProjectDirectory the project directory's path relative to the checkout 100 * directory; or "" if they are the same 101 * @throws MojoExecutionException if any 102 */ 103 private void runGoals( String relativePathProjectDirectory ) 104 throws MojoExecutionException 105 { 106 Commandline cl = new Commandline(); 107 try 108 { 109 cl.addSystemEnvironment(); 110 } 111 catch ( Exception e ) 112 { 113 throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e ); 114 } 115 cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" ); 116 117 if ( "".equals( this.mavenHome ) ) 118 { 119 cl.setExecutable( "mvn" ); 120 } 121 else 122 { 123 cl.setExecutable( this.mavenHome.concat( "/bin/mvn" ) ); 124 } 125 126 cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(), // 127 relativePathProjectDirectory, goalsDirectory ) ); 128 129 if ( this.goals != null ) 130 { 131 String[] tokens = StringUtils.split( this.goals, ", " ); 132 133 for ( int i = 0; i < tokens.length; ++i ) 134 { 135 cl.createArg().setValue( tokens[i] ); 136 } 137 } 138 139 if ( ! StringUtils.isEmpty( this.profiles ) ) 140 { 141 cl.createArg().setValue( "-P" + this.profiles ); 142 } 143 144 StreamConsumer consumer = new DefaultConsumer(); 145 146 try 147 { 148 int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer ); 149 150 if ( result != 0 ) 151 { 152 throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." ); 153 } 154 } 155 catch ( CommandLineException e ) 156 { 157 throw new MojoExecutionException( "Can't run goal " + goals, e ); 158 } 159 } 160 161 /** 162 * Determines the path of the working directory. By default, this is the checkout directory. For some SCMs, 163 * the project root directory is not the checkout directory itself, but a SCM-specific subdirectory. The 164 * build can furthermore optionally be executed in a subdirectory of this project directory, in case. 165 * 166 * @param checkoutDirectory 167 * @param relativePathProjectDirectory 168 * @param goalsDirectory 169 * @return 170 */ 171 protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory, 172 String goalsDirectory ) 173 { 174 File projectDirectory; 175 if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) ) 176 { 177 projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory ); 178 } 179 else 180 { 181 projectDirectory = checkoutDirectory; 182 } 183 184 if ( StringUtils.isEmpty( goalsDirectory ) ) 185 { 186 return projectDirectory.getPath(); 187 } 188 189 return new File( projectDirectory, goalsDirectory ).getPath(); 190 } 191}