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 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 if ( "compile".equals( classpathScope ) )
99 {
100 artifacts.addAll( project.getCompileArtifacts() );
101 theClasspathFiles.add( new File( project.getBuild().getOutputDirectory() ) );
102 }
103 else if ( "test".equals( classpathScope ) )
104 {
105 artifacts.addAll( project.getTestArtifacts() );
106 theClasspathFiles.add( new File( project.getBuild().getTestOutputDirectory() ) );
107 theClasspathFiles.add( new File( project.getBuild().getOutputDirectory() ) );
108 }
109 else if ( "runtime".equals( classpathScope ) )
110 {
111 artifacts.addAll( project.getRuntimeArtifacts() );
112 theClasspathFiles.add( new File( project.getBuild().getOutputDirectory() ) );
113 }
114 else if ( "system".equals( classpathScope ) )
115 {
116 artifacts.addAll( project.getSystemArtifacts() );
117 }
118 else
119 {
120 throw new IllegalStateException( "Invalid classpath scope: " + classpathScope );
121 }
122
123 getLog().debug( "Collected project artifacts " + artifacts );
124 getLog().debug( "Collected project classpath " + theClasspathFiles );
125 }
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 if ( commandlineArgs == null )
139 {
140 return null;
141 }
142 else
143 {
144 try
145 {
146 return CommandLineUtils.translateCommandline( commandlineArgs );
147 }
148 catch ( Exception e )
149 {
150 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 return ( commandlineArgs != null );
161 }
162
163 /**
164 * Register compile and compile tests source roots if necessary
165 */
166 protected void registerSourceRoots()
167 {
168 if ( sourceRoot != null )
169 {
170 getLog().info( "Registering compile source root " + sourceRoot );
171 project.addCompileSourceRoot( sourceRoot.toString() );
172 }
173
174 if ( testSourceRoot != null )
175 {
176 getLog().info( "Registering compile test source root " + testSourceRoot );
177 project.addTestCompileSourceRoot( testSourceRoot.toString() );
178 }
179 }
180
181 /**
182 * Check if the execution should be skipped
183 *
184 * @return true to skip
185 */
186 protected boolean isSkip()
187 {
188 return skip;
189 }
190 }