View Javadoc

1   package org.apache.maven.plugin.antrun;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Abstract class for the Antrun plugin
45   *
46   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   * @version $Id: AbstractAntMojo.html 816154 2012-05-06 21:49:58Z hboutemy $
49   */
50  public abstract class AbstractAntMojo
51      extends AbstractMojo
52  {
53      /**
54       * @deprecated use {@link AbstractAntMojo#executeTasks(Target,MavenProject,List)}.
55       */
56      protected void executeTasks( Target antTasks, MavenProject mavenProject )
57          throws MojoExecutionException
58      {
59          executeTasks( antTasks, mavenProject, null );
60      }
61  
62      /**
63       * @param antTasks
64       * @param mavenProject
65       * @throws MojoExecutionException
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              //TODO refactor - place the manipulation of the expressionEvaluator into a separated class.
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              /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */
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             /* set maven.plugin.classpath with plugin dependencies */
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      * @param artifacts
141      * @param antProject
142      * @return a path
143      * @throws DependencyResolutionRequiredException
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 }