1 package org.apache.maven.script.ant;
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.artifact.Artifact;
23 import org.apache.maven.artifact.DependencyResolutionRequiredException;
24 import org.apache.maven.execution.MavenSession;
25 import org.apache.maven.plugin.AbstractMojo;
26 import org.apache.maven.plugin.ContextEnabled;
27 import org.apache.maven.plugin.MojoExecution;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
30 import org.apache.maven.plugin.descriptor.PluginDescriptor;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.path.PathTranslator;
33 import org.apache.tools.ant.Project;
34 import org.apache.tools.ant.PropertyHelper;
35 import org.apache.tools.ant.types.Path;
36 import org.codehaus.plexus.archiver.ArchiverException;
37 import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
38 import org.codehaus.plexus.component.MapOrientedComponent;
39 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
40 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
41 import org.codehaus.plexus.component.factory.ant.AntComponentExecutionException;
42 import org.codehaus.plexus.component.factory.ant.AntScriptInvoker;
43 import org.codehaus.plexus.component.repository.ComponentRequirement;
44 import org.codehaus.plexus.logging.LogEnabled;
45 import org.codehaus.plexus.logging.Logger;
46 import org.codehaus.plexus.util.StringUtils;
47
48 import java.io.File;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54
55
56
57
58
59 @Deprecated
60 public class AntMojoWrapper
61 extends AbstractMojo
62 implements ContextEnabled, MapOrientedComponent, LogEnabled
63 {
64
65 private Map<String, Object> pluginContext;
66
67 private final AntScriptInvoker scriptInvoker;
68
69 private Project antProject;
70
71 private MavenProject mavenProject;
72
73 private MojoExecution mojoExecution;
74
75 private MavenSession session;
76
77 private PathTranslator pathTranslator;
78
79 private Logger logger;
80
81 private transient List<String> unconstructedParts = new ArrayList<>();
82
83 public AntMojoWrapper( AntScriptInvoker scriptInvoker )
84 {
85 this.scriptInvoker = scriptInvoker;
86 }
87
88 public void execute()
89 throws MojoExecutionException
90 {
91 if ( antProject == null )
92 {
93 antProject = scriptInvoker.getProject();
94 }
95
96 Map<String, Object> allConfig = new HashMap<>();
97 if ( pluginContext != null && !pluginContext.isEmpty() )
98 {
99 allConfig.putAll( pluginContext );
100 }
101
102 @SuppressWarnings( "unchecked" )
103 Map<String, PathTranslator> refs = scriptInvoker.getReferences();
104 if ( refs != null )
105 {
106 allConfig.putAll( refs );
107
108 for ( Map.Entry<String, PathTranslator> entry : refs.entrySet() )
109 {
110 if ( entry.getKey().startsWith( PathTranslator.class.getName() ) )
111 {
112 pathTranslator = entry.getValue();
113 }
114 }
115 }
116
117 mavenProject = (MavenProject) allConfig.get( "project" );
118
119 mojoExecution = (MojoExecution) allConfig.get( "mojoExecution" );
120
121 session = (MavenSession) allConfig.get( "session" );
122
123 unpackFileBasedResources();
124
125 addClasspathReferences();
126
127 if ( logger.isDebugEnabled() && !unconstructedParts.isEmpty() )
128 {
129 StringBuilder buffer = new StringBuilder();
130
131 buffer.append( "The following standard Maven Ant-mojo support objects could not be created:\n\n" );
132
133 for ( String part : unconstructedParts )
134 {
135 buffer.append( "\n- " ).append( part );
136 }
137
138 buffer.append( "\n\nMaven project, session, mojo-execution, or path-translation parameter "
139 + "information is " );
140 buffer.append( "\nmissing from this mojo's plugin descriptor." );
141 buffer.append( "\n\nPerhaps this Ant-based mojo depends on maven-script-ant < 2.1.0, " );
142 buffer.append( "or used maven-plugin-tools-ant < 2.2 during release?\n\n" );
143
144 logger.debug( buffer.toString() );
145 }
146
147 try
148 {
149 scriptInvoker.invoke();
150 }
151 catch ( AntComponentExecutionException e )
152 {
153 throw new MojoExecutionException( "Failed to execute: " + e.getMessage(), e );
154 }
155
156 unconstructedParts.clear();
157 }
158
159 public void setPluginContext( Map pluginContext )
160 {
161 this.pluginContext = pluginContext;
162 }
163
164 public Map getPluginContext()
165 {
166 return pluginContext;
167 }
168
169 public void addComponentRequirement( ComponentRequirement requirementDescriptor, Object requirementValue )
170 throws ComponentConfigurationException
171 {
172 scriptInvoker.addComponentRequirement( requirementDescriptor, requirementValue );
173 }
174
175 public void setComponentConfiguration( Map componentConfiguration )
176 throws ComponentConfigurationException
177 {
178 scriptInvoker.setComponentConfiguration( componentConfiguration );
179 antProject = scriptInvoker.getProject();
180 }
181
182 private void unpackFileBasedResources()
183 throws MojoExecutionException
184 {
185 if ( mojoExecution == null || mavenProject == null )
186 {
187 unconstructedParts.add( "Unpacked Ant build scripts (in Maven build directory)." );
188
189 return;
190 }
191
192
193
194
195
196
197
198 PluginDescriptor pluginDescriptor = mojoExecution.getMojoDescriptor().getPluginDescriptor();
199
200 File pluginJar = pluginDescriptor.getPluginArtifact().getFile();
201
202 String resourcesPath = pluginDescriptor.getArtifactId();
203
204 File outputDirectory = new File( mavenProject.getBuild().getDirectory() );
205
206 try
207 {
208 ZipUnArchiver ua = new ZipUnArchiver( pluginJar );
209
210 ua.extract( resourcesPath, outputDirectory );
211 }
212 catch ( ArchiverException e )
213 {
214 throw new MojoExecutionException( "Error extracting resources from your Ant-based plugin.", e );
215 }
216 }
217
218 private void addClasspathReferences()
219 throws MojoExecutionException
220 {
221 try
222 {
223 if ( mavenProject != null && session != null && pathTranslator != null )
224 {
225 ExpressionEvaluator exprEvaluator =
226 new PluginParameterExpressionEvaluator( session, mojoExecution, pathTranslator, logger,
227 mavenProject, mavenProject.getProperties() );
228
229 PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper( antProject );
230 propertyHelper.setNext( new AntPropertyHelper( exprEvaluator, mavenProject.getArtifacts(), getLog() ) );
231 }
232 else
233 {
234 unconstructedParts.add( "Maven parameter expression evaluator for Ant properties." );
235 }
236
237 @SuppressWarnings( "unchecked" )
238 Map<String, Object> references = scriptInvoker.getReferences();
239
240 if ( mavenProject != null )
241 {
242
243
244 Path p = new Path( antProject );
245
246 p.setPath( StringUtils.join( mavenProject.getCompileClasspathElements().iterator(),
247 File.pathSeparator ) );
248
249
250 references.put( "maven.dependency.classpath", p );
251 antProject.addReference( "maven.dependency.classpath", p );
252
253 references.put( "maven.compile.classpath", p );
254 antProject.addReference( "maven.compile.classpath", p );
255
256
257 p = new Path( antProject );
258
259 p.setPath( StringUtils.join( mavenProject.getRuntimeClasspathElements().iterator(),
260 File.pathSeparator ) );
261
262 references.put( "maven.runtime.classpath", p );
263 antProject.addReference( "maven.runtime.classpath", p );
264
265
266 p = new Path( antProject );
267
268 p.setPath( StringUtils.join( mavenProject.getTestClasspathElements().iterator(),
269 File.pathSeparator ) );
270
271 references.put( "maven.test.classpath", p );
272 antProject.addReference( "maven.test.classpath", p );
273
274 }
275 else
276 {
277 unconstructedParts.add( "Maven standard project-based classpath references." );
278 }
279
280 if ( mojoExecution != null )
281 {
282
283 Path p =
284 getPathFromArtifacts( mojoExecution.getMojoDescriptor().getPluginDescriptor().getArtifacts(),
285 antProject );
286
287 references.put( "maven.plugin.classpath", p );
288 antProject.addReference( "maven.plugin.classpath", p );
289 }
290 else
291 {
292 unconstructedParts.add( "Maven standard plugin-based classpath references." );
293 }
294 }
295 catch ( DependencyResolutionRequiredException e )
296 {
297 throw new MojoExecutionException( "Error creating classpath references for Ant-based plugin scripts.", e );
298 }
299 }
300
301 public Path getPathFromArtifacts( Collection<Artifact> artifacts, Project antProject )
302 throws DependencyResolutionRequiredException
303 {
304 List<String> list = new ArrayList<>( artifacts.size() );
305
306 for ( Artifact a : artifacts )
307 {
308 File file = a.getFile();
309
310 if ( file == null )
311 {
312 throw new DependencyResolutionRequiredException( a );
313 }
314
315 list.add( file.getPath() );
316 }
317
318 Path p = new Path( antProject );
319
320 p.setPath( StringUtils.join( list.iterator(), File.pathSeparator ) );
321
322 return p;
323 }
324
325 public Project getAntProject()
326 {
327 return antProject;
328 }
329
330 public void setAntProject( Project antProject )
331 {
332 this.antProject = antProject;
333 }
334
335 public MavenProject getMavenProject()
336 {
337 return mavenProject;
338 }
339
340 public void setMavenProject( MavenProject mavenProject )
341 {
342 this.mavenProject = mavenProject;
343 }
344
345 public MojoExecution getMojoExecution()
346 {
347 return mojoExecution;
348 }
349
350 public void setMojoExecution( MojoExecution mojoExecution )
351 {
352 this.mojoExecution = mojoExecution;
353 }
354
355 public MavenSession getSession()
356 {
357 return session;
358 }
359
360 public void setSession( MavenSession session )
361 {
362 this.session = session;
363 }
364
365 public PathTranslator getPathTranslator()
366 {
367 return pathTranslator;
368 }
369
370 public void setPathTranslator( PathTranslator pathTranslator )
371 {
372 this.pathTranslator = pathTranslator;
373 }
374
375 public AntScriptInvoker getScriptInvoker()
376 {
377 return scriptInvoker;
378 }
379
380 public void enableLogging( Logger logger )
381 {
382 this.logger = logger;
383 }
384 }