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