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