View Javadoc

1   package org.codehaus.mojo.exec;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
5    * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
7    * License. You may obtain a copy of the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13   * specific language governing permissions and limitations under the License.
14   */
15  
16  import java.io.File;
17  import java.util.List;
18  
19  import org.apache.maven.plugin.AbstractMojo;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.project.MavenProject;
22  import org.codehaus.plexus.util.cli.CommandLineUtils;
23  
24  /**
25   * This class is used for unifying functionality between the 2 mojo exec plugins ('java' and 'exec'). It handles parsing
26   * the arguments and adding source/test folders.
27   * 
28   * @author Philippe Jacot (PJA)
29   * @author Jerome Lacoste
30   */
31  public abstract class AbstractExecMojo extends AbstractMojo {
32      /**
33       * The enclosing project.
34       * 
35       * @parameter expression="${project}"
36       * @required
37       * @readonly
38       */
39      protected MavenProject project;
40  
41      /**
42       * This folder is added to the list of those folders containing source to be compiled. Use this if your plugin
43       * generates source code.
44       */
45      private File sourceRoot;
46  
47      /**
48       * This folder is added to the list of those folders containing source to be compiled for testing. Use this if your
49       * plugin generates test source code.
50       */
51      private File testSourceRoot;
52  
53      /**
54       * Arguments for the executed program
55       */
56      private String commandlineArgs;
57  
58      /**
59       * Defines the scope of the classpath passed to the plugin. Set to compile,test,runtime or system depending on your
60       * needs. Since 1.1.2, the default value is 'runtime' instead of 'compile'.
61       */
62      protected String classpathScope;
63  
64      /**
65       * Skip the execution.
66       * 
67       * @parameter expression="${skip}" default-value="false"
68       * @since 1.0.1
69       */
70      private boolean skip;
71  
72      /**
73       * Collects the project artifacts in the specified List and the project specific classpath (build output and build
74       * test output) Files in the specified List, depending on the plugin classpathScope value.
75       * 
76       * @param artifacts
77       *            the list where to collect the scope specific artifacts
78       * @param theClasspathFiles
79       *            the list where to collect the scope specific output directories
80       */
81      protected void collectProjectArtifactsAndClasspath(List artifacts, List theClasspathFiles) {
82  
83          if ("compile".equals(classpathScope)) {
84              artifacts.addAll(project.getCompileArtifacts());
85              theClasspathFiles.add(new File(project.getBuild().getOutputDirectory()));
86          } else if ("test".equals(classpathScope)) {
87              artifacts.addAll(project.getTestArtifacts());
88              theClasspathFiles.add(new File(project.getBuild().getTestOutputDirectory()));
89              theClasspathFiles.add(new File(project.getBuild().getOutputDirectory()));
90          } else if ("runtime".equals(classpathScope)) {
91              artifacts.addAll(project.getRuntimeArtifacts());
92              theClasspathFiles.add(new File(project.getBuild().getOutputDirectory()));
93          } else if ("system".equals(classpathScope)) {
94              artifacts.addAll(project.getSystemArtifacts());
95          } else {
96              throw new IllegalStateException("Invalid classpath scope: " + classpathScope);
97          }
98  
99          getLog().debug("Collected project artifacts " + artifacts);
100         getLog().debug("Collected project classpath " + theClasspathFiles);
101     }
102 
103     /**
104      * Parses the argument string given by the user. Strings are recognized as everything between STRING_WRAPPER.
105      * PARAMETER_DELIMITER is ignored inside a string. STRING_WRAPPER and PARAMETER_DELIMITER can be escaped using
106      * ESCAPE_CHAR.
107      * 
108      * @return Array of String representing the arguments
109      * @throws MojoExecutionException
110      *             for wrong formatted arguments
111      */
112     protected String[] parseCommandlineArgs() throws MojoExecutionException {
113         if (commandlineArgs == null) {
114             return null;
115         } else {
116             try {
117                 return CommandLineUtils.translateCommandline(commandlineArgs);
118             } catch (Exception e) {
119                 throw new MojoExecutionException(e.getMessage());
120             }
121         }
122     }
123 
124     /**
125      * @return true of the mojo has command line arguments
126      */
127     protected boolean hasCommandlineArgs() {
128         return (commandlineArgs != null);
129     }
130 
131     /**
132      * Register compile and compile tests source roots if necessary
133      */
134     protected void registerSourceRoots() {
135         if (sourceRoot != null) {
136             getLog().info("Registering compile source root " + sourceRoot);
137             project.addCompileSourceRoot(sourceRoot.toString());
138         }
139 
140         if (testSourceRoot != null) {
141             getLog().info("Registering compile test source root " + testSourceRoot);
142             project.addTestCompileSourceRoot(testSourceRoot.toString());
143         }
144     }
145 
146     /**
147      * Check if the execution should be skipped
148      * 
149      * @return true to skip
150      */
151     protected boolean isSkip() {
152         return skip;
153     }
154 }