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 java.io.File;
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.StringUtils;
30 import org.codehaus.plexus.util.cli.CommandLineException;
31 import org.codehaus.plexus.util.cli.CommandLineUtils;
32 import org.codehaus.plexus.util.cli.Commandline;
33 import org.codehaus.plexus.util.cli.DefaultConsumer;
34 import org.codehaus.plexus.util.cli.StreamConsumer;
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 public void execute()
70 throws MojoExecutionException
71 {
72 super.execute();
73
74 if ( this.getCheckoutResult() != null )
75 {
76
77 ScmResult checkoutResult = this.getCheckoutResult();
78
79
80
81
82 String relativePathProjectDirectory = "";
83 if ( checkoutResult instanceof CheckOutScmResult )
84 {
85 relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult ).getRelativePathProjectDirectory();
86 }
87
88 runGoals( relativePathProjectDirectory );
89 }
90 }
91
92
93
94
95
96
97 private void runGoals( String relativePathProjectDirectory )
98 throws MojoExecutionException
99 {
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
148
149
150
151
152
153
154
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 }