1 package org.apache.maven.scm.plugin;
2
3 import java.io.File;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugins.annotations.Mojo;
26 import org.apache.maven.plugins.annotations.Parameter;
27 import org.apache.maven.scm.ScmResult;
28 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
29 import org.codehaus.plexus.util.Os;
30 import org.codehaus.plexus.util.StringUtils;
31 import org.codehaus.plexus.util.cli.CommandLineException;
32 import org.codehaus.plexus.util.cli.CommandLineUtils;
33 import org.codehaus.plexus.util.cli.Commandline;
34 import org.codehaus.plexus.util.cli.DefaultConsumer;
35 import org.codehaus.plexus.util.cli.StreamConsumer;
36
37
38
39
40
41
42 @Mojo( name = "bootstrap", requiresProject = false )
43 public class BootstrapMojo
44 extends CheckoutMojo
45 {
46
47
48
49
50
51 @Parameter( property = "goals" )
52 private String goals;
53
54
55
56
57
58 @Parameter( property = "profiles" )
59 private String profiles;
60
61
62
63
64
65
66 @Parameter( property = "goalsDirectory" )
67 private String goalsDirectory;
68
69
70
71
72 @Parameter( property = "mavenHome", defaultValue="${maven.home}")
73 private File mavenHome;
74
75
76 public void execute()
77 throws MojoExecutionException
78 {
79 super.execute();
80
81 if ( this.getCheckoutResult() != null )
82 {
83
84 ScmResult checkoutResult = this.getCheckoutResult();
85
86
87
88
89 String relativePathProjectDirectory = "";
90 if ( checkoutResult instanceof CheckOutScmResult )
91 {
92 relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult ).getRelativePathProjectDirectory();
93 }
94
95 runGoals( relativePathProjectDirectory );
96 }
97 }
98
99
100
101
102
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" );
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
174
175
176
177
178
179
180
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 }