001    /**
002     * Copyright 2011-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.maven.plugins.mvn;
017    
018    import java.io.File;
019    import java.util.List;
020    import java.util.Properties;
021    
022    import org.apache.maven.plugin.AbstractMojo;
023    import org.apache.maven.plugin.MojoExecutionException;
024    import org.apache.maven.project.MavenProject;
025    import org.kuali.maven.common.MvnContext;
026    import org.kuali.maven.common.MvnExecutor;
027    
028    /**
029     * Invoke mvn from Maven.
030     *
031     * @goal mvn
032     */
033    public class MvnMojo extends AbstractMojo implements MvnContext {
034            MvnExecutor executor = new MvnExecutor();
035    
036            @Override
037            public Properties getProjectProperties() {
038                    return project.getProperties();
039            }
040    
041            /**
042             * The Maven project object
043             *
044             * @parameter expression="${project}"
045             * @readonly
046             */
047            private MavenProject project;
048    
049            /**
050             * The working directory where the plugin makes a local copy of the pom (if a pom is supplied)
051             *
052             * @parameter expression="${mvn.workingDir}" default-value="${project.build.directory}/mvn"
053             * @required
054             */
055            private File workingDir;
056    
057            /**
058             * The base directory for the new mvn invocation.
059             *
060             * @parameter expression="${mvn.basedir}" default-value="${project.build.directory}/mvn"
061             *
062             * @required
063             */
064            private File basedir;
065    
066            /**
067             * The Maven executable. By default, the executable to use is located via the ${maven.home} system property. This causes the new mvn
068             * invocation to mirror the one that is currently executing (same version, etc). You can override this behavior by supplying your own
069             * executable
070             *
071             * @parameter expression="${mvn.executable}"
072             */
073            private String executable;
074    
075            /**
076             * The pom to supply to the new mvn invocation. This can be a file or any url Spring resource loading can understand
077             *
078             * eg classpath:pom.xml
079             *
080             * @parameter expression="${mvn.pom}"
081             */
082            private String pom;
083    
084            /**
085             * POM's to invoke. If supplied, a new Maven invocation is generated using the same args for each pom
086             *
087             * @parameter
088             */
089            private List<String> poms;
090    
091            /**
092             * If true, the pom will be filtered using properties from the current project before being invoked
093             *
094             * @parameter expression="${mvn.filterPom}" default-value="false"
095             */
096            private boolean filterPom;
097    
098            /**
099             * If supplied, only the listed properties will be used when filtering the pom
100             *
101             * @parameter
102             */
103            private List<String> filterProperties;
104    
105            /**
106             * Arguments to supply to the new mvn invocation eg "clean install"
107             *
108             * @parameter
109             * @required
110             */
111            private List<String> args;
112    
113            /**
114             * List of properties from the current project to propagate to the new mvn invocation
115             *
116             * @parameter
117             */
118            private List<String> properties;
119    
120            /**
121             * If true, the current environment is passed to the new mvn invocation
122             *
123             * @parameter expression="${mvn.addEnvironment}" default-value="false"
124             */
125            private boolean addEnvironment;
126    
127            /**
128             * If true, the environment variable MAVEN_OPTS (if set) is passed to the new mvn invocation
129             *
130             * @parameter expression="${mvn.addMavenOpts}" default-value="true"
131             */
132            private boolean addMavenOpts;
133    
134            /**
135             * If true, the original Maven build will fail if the new mvn invocation returns a non-zero exit value, otherwise the original Maven
136             * build will continue
137             *
138             * @parameter expression="${mvn.failOnError}" default-value="true"
139             * @required
140             */
141            private boolean failOnError;
142    
143            /**
144             * If true, any temp pom copied to <code>basedir</code> will be deleted when the plugin execution is complete
145             *
146             * @parameter expression="${mvn.deleteTempPom}" default-value="true"
147             * @required
148             */
149            private boolean deleteTempPom;
150    
151            /**
152             * If true, logging output is reduced to a minimum
153             *
154             * @parameter expression="${mvn.quiet}" default-value="true"
155             * @required
156             */
157            private boolean quiet;
158    
159            /**
160             * If true, no logging output is generated.
161             *
162             * @parameter expression="${mvn.silent}" default-value="false"
163             * @required
164             */
165            private boolean silent;
166    
167            /**
168             * If true, always execute the mojo, even if packaging is <code>pom</code>. <code>forceMojoExecution</code> overrides <code>skip</code>
169             *
170             * @parameter expression="${mvn.forceMojoExecution}" default-value="false"
171             * @required
172             */
173            private boolean forceMojoExecution;
174    
175            /**
176             * If true, skip executing the mojo
177             *
178             * @parameter expression="${mvn.skip}" default-value="false"
179             * @required
180             */
181            private boolean skip;
182    
183            @Override
184            public void execute() throws MojoExecutionException {
185                    if (isSkip()) {
186                            getLog().info("Skipping execution");
187                            return;
188                    }
189                    try {
190                            executor.execute(this);
191                    } catch (Exception e) {
192                            throw new MojoExecutionException("Error invoking mvn", e);
193                    }
194            }
195    
196            /**
197             * Return <code>true<code> only if packaging equals <code>pom</code> or <code>skip</code> equals <code>true</code> AND
198             * <code>forceMojoExecution</code> is <code>false</code>.
199             */
200            protected boolean isSkip() {
201                    if (forceMojoExecution) {
202                            return false;
203                    }
204                    if (skip) {
205                            return true;
206                    } else {
207                            return project.getPackaging().toLowerCase().equals("pom");
208                    }
209            }
210    
211            @Override
212            public File getWorkingDir() {
213                    return workingDir;
214            }
215    
216            @Override
217            public void setWorkingDir(File workingDir) {
218                    this.workingDir = workingDir;
219            }
220    
221            @Override
222            public boolean isFailOnError() {
223                    return failOnError;
224            }
225    
226            @Override
227            public void setFailOnError(boolean failOnError) {
228                    this.failOnError = failOnError;
229            }
230    
231            public MavenProject getProject() {
232                    return project;
233            }
234    
235            @Override
236            public String getPom() {
237                    return pom;
238            }
239    
240            @Override
241            public void setPom(String pom) {
242                    this.pom = pom;
243            }
244    
245            @Override
246            public List<String> getArgs() {
247                    return args;
248            }
249    
250            @Override
251            public void setArgs(List<String> args) {
252                    this.args = args;
253            }
254    
255            @Override
256            public String getExecutable() {
257                    return executable;
258            }
259    
260            @Override
261            public void setExecutable(String executable) {
262                    this.executable = executable;
263            }
264    
265            @Override
266            public boolean isAddEnvironment() {
267                    return addEnvironment;
268            }
269    
270            @Override
271            public void setAddEnvironment(boolean addSystemEnvironment) {
272                    this.addEnvironment = addSystemEnvironment;
273            }
274    
275            @Override
276            public List<String> getProperties() {
277                    return properties;
278            }
279    
280            @Override
281            public void setProperties(List<String> properties) {
282                    this.properties = properties;
283            }
284    
285            @Override
286            public boolean isFilterPom() {
287                    return filterPom;
288            }
289    
290            @Override
291            public void setFilterPom(boolean filter) {
292                    this.filterPom = filter;
293            }
294    
295            @Override
296            public boolean isAddMavenOpts() {
297                    return addMavenOpts;
298            }
299    
300            @Override
301            public void setAddMavenOpts(boolean addMavenOpts) {
302                    this.addMavenOpts = addMavenOpts;
303            }
304    
305            @Override
306            public File getBasedir() {
307                    return basedir;
308            }
309    
310            @Override
311            public void setBasedir(File basedir) {
312                    this.basedir = basedir;
313            }
314    
315            @Override
316            public boolean isDeleteTempPom() {
317                    return deleteTempPom;
318            }
319    
320            public void setDeleteTempPom(boolean deleteTempPom) {
321                    this.deleteTempPom = deleteTempPom;
322            }
323    
324            @Override
325            public List<String> getPoms() {
326                    return poms;
327            }
328    
329            @Override
330            public void setPoms(List<String> poms) {
331                    this.poms = poms;
332            }
333    
334            @Override
335            public boolean isQuiet() {
336                    return quiet;
337            }
338    
339            @Override
340            public void setQuiet(boolean quiet) {
341                    this.quiet = quiet;
342            }
343    
344            @Override
345            public boolean isSilent() {
346                    return silent;
347            }
348    
349            @Override
350            public void setSilent(boolean silent) {
351                    this.silent = silent;
352            }
353    
354            @Override
355            public List<String> getFilterProperties() {
356                    return filterProperties;
357            }
358    
359            @Override
360            public void setFilterProperties(List<String> filterProperties) {
361                    this.filterProperties = filterProperties;
362            }
363    
364            public boolean isForceMojoExecution() {
365                    return forceMojoExecution;
366            }
367    
368            public void setForceMojoExecution(boolean forceMojoExecution) {
369                    this.forceMojoExecution = forceMojoExecution;
370            }
371    
372            public void setSkip(boolean skip) {
373                    this.skip = skip;
374            }
375    
376    }