Coverage Report - org.apache.maven.plugin.antrun.AbstractAntMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAntMojo
77%
41/53
57%
8/14
6
 
 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$
 49  
  */
 50  3
 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  0
         executeTasks( antTasks, mavenProject, null );
 60  0
     }
 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  3
         if ( antTasks == null )
 71  
         {
 72  0
             getLog().info( "No ant tasks defined - SKIPPED" );
 73  0
             return;
 74  
         }
 75  
 
 76  
         try
 77  
         {
 78  
             //TODO refactor - place the manipulation of the expressionEvaluator into a separated class.
 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  
             /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */
 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  
             /* set maven.plugin.classpath with plugin dependencies */
 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  
      * @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  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  
 }