1 package org.apache.maven.plugin.antrun;
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.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.DependencyResolutionRequiredException;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.antrun.components.AntTargetConverter;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.DefaultLogger;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.PropertyHelper;
38 import org.apache.tools.ant.Target;
39 import org.apache.tools.ant.types.Path;
40 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
41 import org.codehaus.plexus.util.StringUtils;
42
43
44
45
46
47
48
49
50 public abstract class AbstractAntMojo
51 extends AbstractMojo
52 {
53
54
55
56 protected void executeTasks( Target antTasks, MavenProject mavenProject )
57 throws MojoExecutionException
58 {
59 executeTasks( antTasks, mavenProject, null );
60 }
61
62
63
64
65
66
67 protected void executeTasks( Target antTasks, MavenProject mavenProject, List pluginArtifacts )
68 throws MojoExecutionException
69 {
70 if ( antTasks == null )
71 {
72 getLog().info( "No ant tasks defined - SKIPPED" );
73 return;
74 }
75
76 try
77 {
78
79 ExpressionEvaluator exprEvaluator = (ExpressionEvaluator) antTasks.getProject()
80 .getReference( AntTargetConverter.MAVEN_EXPRESSION_EVALUATOR_ID );
81
82 Project antProject = antTasks.getProject();
83
84 PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper( antProject );
85 propertyHelper.setNext( new AntPropertyHelper( exprEvaluator, mavenProject.getArtifacts(), getLog() ) );
86
87 DefaultLogger antLogger = new DefaultLogger();
88 antLogger.setOutputPrintStream( System.out );
89 antLogger.setErrorPrintStream( System.err );
90 antLogger.setMessageOutputLevel( getLog().isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO );
91
92 antProject.addBuildListener( antLogger );
93 antProject.setBaseDir( mavenProject.getBasedir() );
94
95 Path p = new Path( antProject );
96 p.setPath( StringUtils.join( mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator ) );
97
98
99 antProject.addReference( "maven.dependency.classpath", p );
100 antProject.addReference( "maven.compile.classpath", p );
101
102 p = new Path( antProject );
103 p.setPath( StringUtils.join( mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator ) );
104 antProject.addReference( "maven.runtime.classpath", p );
105
106 p = new Path( antProject );
107 p.setPath( StringUtils.join( mavenProject.getTestClasspathElements().iterator(), File.pathSeparator ) );
108 antProject.addReference( "maven.test.classpath", p );
109
110
111 antProject.addReference( "maven.plugin.classpath", getPathFromArtifacts( pluginArtifacts, antProject ) );
112
113 if ( getLog().isInfoEnabled() )
114 {
115 getLog().info( "Executing tasks" );
116 }
117
118 antTasks.execute();
119
120 if ( getLog().isInfoEnabled() )
121 {
122 getLog().info( "Executed tasks" );
123 }
124 }
125 catch ( DependencyResolutionRequiredException e )
126 {
127 throw new MojoExecutionException( "DependencyResolutionRequiredException: " + e.getMessage(), e );
128 }
129 catch ( BuildException e )
130 {
131 throw new MojoExecutionException( "An Ant BuildException has occured: " + e.getMessage(), e );
132 }
133 catch ( Exception e )
134 {
135 throw new MojoExecutionException( "Error executing ant tasks: " + e.getMessage(), e );
136 }
137 }
138
139
140
141
142
143
144
145 public Path getPathFromArtifacts( Collection artifacts, Project antProject )
146 throws DependencyResolutionRequiredException
147 {
148 if ( artifacts == null )
149 {
150 return new Path( antProject );
151 }
152
153 List list = new ArrayList( artifacts.size() );
154 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
155 {
156 Artifact a = (Artifact) i.next();
157 File file = a.getFile();
158 if ( file == null )
159 {
160 throw new DependencyResolutionRequiredException( a );
161 }
162 list.add( file.getPath() );
163 }
164
165 Path p = new Path( antProject );
166 p.setPath( StringUtils.join( list.iterator(), File.pathSeparator ) );
167
168 return p;
169 }
170
171 }