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