Coverage Report - org.codehaus.mojo.exec.AbstractExecMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractExecMojo
0%
0/31
0%
0/16
3.4
 
 1  
 package org.codehaus.mojo.exec;
 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.List;
 24  
 
 25  
 import org.apache.maven.plugin.AbstractMojo;
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 import org.apache.maven.project.MavenProject;
 28  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 29  
 
 30  
 /**
 31  
  * This class is used for unifying functionality between the 2 mojo exec plugins ('java' and 'exec'). It handles parsing
 32  
  * the arguments and adding source/test folders.
 33  
  * 
 34  
  * @author Philippe Jacot (PJA)
 35  
  * @author Jerome Lacoste
 36  
  */
 37  0
 public abstract class AbstractExecMojo
 38  
     extends AbstractMojo
 39  
 {
 40  
     /**
 41  
      * The enclosing project.
 42  
      * 
 43  
      * @parameter expression="${project}"
 44  
      * @required
 45  
      * @readonly
 46  
      */
 47  
     protected MavenProject project;
 48  
 
 49  
     /**
 50  
      * This folder is added to the list of those folders containing source to be compiled. Use this if your plugin
 51  
      * generates source code.
 52  
      * 
 53  
      * @parameter expression="${sourceRoot}"
 54  
      */
 55  
     private File sourceRoot;
 56  
 
 57  
     /**
 58  
      * This folder is added to the list of those folders containing source to be compiled for testing. Use this if your
 59  
      * plugin generates test source code.
 60  
      * 
 61  
      * @parameter expression="${testSourceRoot}"
 62  
      */
 63  
     private File testSourceRoot;
 64  
 
 65  
     /**
 66  
      * Arguments for the executed program
 67  
      * 
 68  
      * @parameter expression="${exec.args}"
 69  
      */
 70  
     private String commandlineArgs;
 71  
 
 72  
     /**
 73  
      * Defines the scope of the classpath passed to the plugin. Set to compile,test,runtime or system depending on your
 74  
      * needs. Since 1.1.2, the default value is 'runtime' instead of 'compile'.
 75  
      * 
 76  
      * @parameter expression="${exec.classpathScope}" default-value="runtime"
 77  
      */
 78  
     protected String classpathScope;
 79  
 
 80  
     /**
 81  
      * Skip the execution.
 82  
      * 
 83  
      * @parameter expression="${skip}" default-value="false"
 84  
      * @since 1.0.1
 85  
      */
 86  
     private boolean skip;
 87  
 
 88  
     /**
 89  
      * Collects the project artifacts in the specified List and the project specific classpath (build output and build
 90  
      * test output) Files in the specified List, depending on the plugin classpathScope value.
 91  
      * 
 92  
      * @param artifacts the list where to collect the scope specific artifacts
 93  
      * @param theClasspathFiles the list where to collect the scope specific output directories
 94  
      */
 95  
     protected void collectProjectArtifactsAndClasspath( List artifacts, List theClasspathFiles )
 96  
     {
 97  
 
 98  0
         if ( "compile".equals( classpathScope ) )
 99  
         {
 100  0
             artifacts.addAll( project.getCompileArtifacts() );
 101  0
             theClasspathFiles.add( new File( project.getBuild().getOutputDirectory() ) );
 102  
         }
 103  0
         else if ( "test".equals( classpathScope ) )
 104  
         {
 105  0
             artifacts.addAll( project.getTestArtifacts() );
 106  0
             theClasspathFiles.add( new File( project.getBuild().getTestOutputDirectory() ) );
 107  0
             theClasspathFiles.add( new File( project.getBuild().getOutputDirectory() ) );
 108  
         }
 109  0
         else if ( "runtime".equals( classpathScope ) )
 110  
         {
 111  0
             artifacts.addAll( project.getRuntimeArtifacts() );
 112  0
             theClasspathFiles.add( new File( project.getBuild().getOutputDirectory() ) );
 113  
         }
 114  0
         else if ( "system".equals( classpathScope ) )
 115  
         {
 116  0
             artifacts.addAll( project.getSystemArtifacts() );
 117  
         }
 118  
         else
 119  
         {
 120  0
             throw new IllegalStateException( "Invalid classpath scope: " + classpathScope );
 121  
         }
 122  
 
 123  0
         getLog().debug( "Collected project artifacts " + artifacts );
 124  0
         getLog().debug( "Collected project classpath " + theClasspathFiles );
 125  0
     }
 126  
 
 127  
     /**
 128  
      * Parses the argument string given by the user. Strings are recognized as everything between STRING_WRAPPER.
 129  
      * PARAMETER_DELIMITER is ignored inside a string. STRING_WRAPPER and PARAMETER_DELIMITER can be escaped using
 130  
      * ESCAPE_CHAR.
 131  
      * 
 132  
      * @return Array of String representing the arguments
 133  
      * @throws MojoExecutionException for wrong formatted arguments
 134  
      */
 135  
     protected String[] parseCommandlineArgs()
 136  
         throws MojoExecutionException
 137  
     {
 138  0
         if ( commandlineArgs == null )
 139  
         {
 140  0
             return null;
 141  
         }
 142  
         else
 143  
         {
 144  
             try
 145  
             {
 146  0
                 return CommandLineUtils.translateCommandline( commandlineArgs );
 147  
             }
 148  0
             catch ( Exception e )
 149  
             {
 150  0
                 throw new MojoExecutionException( e.getMessage() );
 151  
             }
 152  
         }
 153  
     }
 154  
 
 155  
     /**
 156  
      * @return true of the mojo has command line arguments
 157  
      */
 158  
     protected boolean hasCommandlineArgs()
 159  
     {
 160  0
         return ( commandlineArgs != null );
 161  
     }
 162  
 
 163  
     /**
 164  
      * Register compile and compile tests source roots if necessary
 165  
      */
 166  
     protected void registerSourceRoots()
 167  
     {
 168  0
         if ( sourceRoot != null )
 169  
         {
 170  0
             getLog().info( "Registering compile source root " + sourceRoot );
 171  0
             project.addCompileSourceRoot( sourceRoot.toString() );
 172  
         }
 173  
 
 174  0
         if ( testSourceRoot != null )
 175  
         {
 176  0
             getLog().info( "Registering compile test source root " + testSourceRoot );
 177  0
             project.addTestCompileSourceRoot( testSourceRoot.toString() );
 178  
         }
 179  0
     }
 180  
 
 181  
     /**
 182  
      * Check if the execution should be skipped
 183  
      * 
 184  
      * @return true to skip
 185  
      */
 186  
     protected boolean isSkip()
 187  
     {
 188  0
         return skip;
 189  
     }
 190  
 }