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.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
35
36
37
38
39
40
41
42 public class BootstrapMojo
43 extends CheckoutMojo
44 {
45
46
47
48
49
50
51
52 private String goals;
53
54
55
56
57
58
59
60 private String profiles;
61
62
63
64
65
66
67
68
69 private String goalsDirectory;
70
71
72 public void execute()
73 throws MojoExecutionException
74 {
75 super.execute();
76
77 if ( this.getCheckoutResult() != null )
78 {
79
80 ScmResult checkoutResult = this.getCheckoutResult();
81
82
83
84
85 String relativePathProjectDirectory = "";
86 if ( checkoutResult instanceof CheckOutScmResult )
87 {
88 relativePathProjectDirectory = ( (CheckOutScmResult) checkoutResult).getRelativePathProjectDirectory();
89 }
90
91 runGoals( relativePathProjectDirectory );
92 }
93 }
94
95
96
97
98
99
100 private void runGoals( String relativePathProjectDirectory )
101 throws MojoExecutionException
102 {
103 Commandline cl = new Commandline();
104 try
105 {
106 cl.addSystemEnvironment();
107 }
108 catch ( Exception e )
109 {
110 throw new MojoExecutionException( "Can't add system environment variables to mvn command line.", e );
111 }
112 cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
113 cl.setExecutable( "mvn" );
114 cl.setWorkingDirectory( determineWorkingDirectoryPath( this.getCheckoutDirectory(),
115 relativePathProjectDirectory, goalsDirectory ) );
116
117 if ( this.goals != null )
118 {
119 String[] tokens = StringUtils.split( this.goals, ", " );
120
121 for ( int i = 0; i < tokens.length; ++i )
122 {
123 cl.createArg().setValue( tokens[i] );
124 }
125 }
126
127 if ( ! StringUtils.isEmpty( this.profiles ) )
128 {
129 cl.createArg().setValue( "-P" + this.profiles );
130 }
131
132 StreamConsumer consumer = new DefaultConsumer();
133
134 try
135 {
136 int result = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
137
138 if ( result != 0 )
139 {
140 throw new MojoExecutionException( "Result of mvn execution is: \'" + result + "\'. Release failed." );
141 }
142 }
143 catch ( CommandLineException e )
144 {
145 throw new MojoExecutionException( "Can't run goal " + goals, e );
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158
159 protected String determineWorkingDirectoryPath( File checkoutDirectory, String relativePathProjectDirectory,
160 String goalsDirectory )
161 {
162 File projectDirectory;
163 if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
164 {
165 projectDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
166 }
167 else
168 {
169 projectDirectory = checkoutDirectory;
170 }
171
172 if ( StringUtils.isEmpty( goalsDirectory ) )
173 {
174 return projectDirectory.getPath();
175 }
176
177 return new File( projectDirectory, goalsDirectory ).getPath();
178 }
179 }