1 package org.apache.maven.shared.invoker;
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 import java.io.InputStream;
24
25 import org.codehaus.plexus.util.cli.CommandLineException;
26 import org.codehaus.plexus.util.cli.CommandLineUtils;
27 import org.codehaus.plexus.util.cli.Commandline;
28
29
30
31
32
33
34
35 public class DefaultInvoker
36 implements Invoker
37 {
38
39 public static final String ROLE_HINT = "default";
40
41 private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger();
42
43 private static final InvocationOutputHandler DEFAULT_OUTPUT_HANDLER = new SystemOutHandler();
44
45 private File localRepositoryDirectory;
46
47 private InvokerLogger logger = DEFAULT_LOGGER;
48
49 private File workingDirectory;
50
51 private File mavenHome;
52
53 private InvocationOutputHandler outputHandler = DEFAULT_OUTPUT_HANDLER;
54
55 private InputStream inputStream;
56
57 private InvocationOutputHandler errorHandler = DEFAULT_OUTPUT_HANDLER;
58
59 public InvocationResult execute( InvocationRequest request )
60 throws MavenInvocationException
61 {
62 MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder();
63
64 InvokerLogger logger = getLogger();
65 if ( logger != null )
66 {
67 cliBuilder.setLogger( getLogger() );
68 }
69
70 File localRepo = getLocalRepositoryDirectory();
71 if ( localRepo != null )
72 {
73 cliBuilder.setLocalRepositoryDirectory( getLocalRepositoryDirectory() );
74 }
75
76 File mavenHome = getMavenHome();
77 if ( mavenHome != null )
78 {
79 cliBuilder.setMavenHome( getMavenHome() );
80 }
81
82 File workingDirectory = getWorkingDirectory();
83 if ( workingDirectory != null )
84 {
85 cliBuilder.setWorkingDirectory( getWorkingDirectory() );
86 }
87
88 Commandline cli;
89 try
90 {
91 cli = cliBuilder.build( request );
92 }
93 catch ( CommandLineConfigurationException e )
94 {
95 throw new MavenInvocationException( "Error configuring command-line. Reason: " + e.getMessage(), e );
96 }
97
98 DefaultInvocationResult result = new DefaultInvocationResult();
99
100 try
101 {
102 int exitCode = executeCommandLine( cli, request );
103
104 result.setExitCode( exitCode );
105 }
106 catch ( CommandLineException e )
107 {
108 result.setExecutionException( e );
109 }
110
111 return result;
112 }
113
114 private int executeCommandLine( Commandline cli, InvocationRequest request )
115 throws CommandLineException
116 {
117 int result = Integer.MIN_VALUE;
118
119 InputStream inputStream = request.getInputStream( this.inputStream );
120 InvocationOutputHandler outputHandler = request.getOutputHandler( this.outputHandler );
121 InvocationOutputHandler errorHandler = request.getErrorHandler( this.errorHandler );
122
123 if ( getLogger().isDebugEnabled() )
124 {
125 getLogger().debug( "Executing: " + cli );
126 }
127 if ( request.isInteractive() )
128 {
129 if ( inputStream == null )
130 {
131 getLogger().warn(
132 "Maven will be executed in interactive mode"
133 + ", but no input stream has been configured for this MavenInvoker instance." );
134
135 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler );
136 }
137 else
138 {
139 result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler );
140 }
141 }
142 else
143 {
144 if ( inputStream != null )
145 {
146 getLogger().info( "Executing in batch mode. The configured input stream will be ignored." );
147 }
148
149 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler );
150 }
151
152 return result;
153 }
154
155 public File getLocalRepositoryDirectory()
156 {
157 return localRepositoryDirectory;
158 }
159
160 public InvokerLogger getLogger()
161 {
162 return logger;
163 }
164
165 public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
166 {
167 this.localRepositoryDirectory = localRepositoryDirectory;
168 return this;
169 }
170
171 public Invoker setLogger( InvokerLogger logger )
172 {
173 this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
174 return this;
175 }
176
177 public File getWorkingDirectory()
178 {
179 return workingDirectory;
180 }
181
182 public Invoker setWorkingDirectory( File workingDirectory )
183 {
184 this.workingDirectory = workingDirectory;
185 return this;
186 }
187
188 public File getMavenHome()
189 {
190 return mavenHome;
191 }
192
193 public Invoker setMavenHome( File mavenHome )
194 {
195 this.mavenHome = mavenHome;
196
197 return this;
198 }
199
200 public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
201 {
202 this.errorHandler = errorHandler;
203 return this;
204 }
205
206 public Invoker setInputStream( InputStream inputStream )
207 {
208 this.inputStream = inputStream;
209 return this;
210 }
211
212 public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
213 {
214 this.outputHandler = outputHandler;
215 return this;
216 }
217
218 }