View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.genesis.plugins.maven;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import java.util.Map;
26  import java.util.Iterator;
27  
28  import org.apache.geronimo.genesis.MojoSupport;
29  import org.apache.geronimo.genesis.ant.AntHelper;
30  
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugin.MojoFailureException;
34  
35  import org.codehaus.plexus.util.Os;
36  import org.codehaus.plexus.util.DirectoryScanner;
37  
38  import org.apache.tools.ant.taskdefs.ExecTask;
39  
40  /**
41   * Invoke Maven in a sub-process.
42   *
43   * @goal invoke
44   *
45   * @version $Rev: 479786 $ $Date: 2006-11-27 14:23:36 -0800 (Mon, 27 Nov 2006) $
46   */
47  public class InvokeMavenMojo
48      extends MojoSupport
49  {
50      /**
51       * Defines the set of pom.xml files to invoke.
52       *
53       * @parameter
54       * @required
55       */
56      private DirectoryScanner fileset = null;
57  
58      /**
59       * A set of command-line flags to pass to Maven.
60       *
61       * @parameter
62       */
63      private String[] flags = null;
64  
65      /**
66       * A map of parameters to define via -D
67       *
68       * @parameter
69       */
70      private Map parameters = null;
71  
72      /**
73       * A set of profiles to activate via -P
74       *
75       * @parameter
76       */
77      private String[] profiles = null;
78  
79      /**
80       * A set of goals (or phases) to be invoked.
81       *
82       * @parameter
83       */
84      private String[] goals = null;
85  
86      /**
87       * @component
88       */
89      protected AntHelper ant;
90  
91      //
92      // MojoSupport Hooks
93      //
94  
95      /**
96       * The maven project.
97       *
98       * @parameter expression="${project}"
99       * @required
100      * @readonly
101      */
102     protected MavenProject project = null;
103 
104     protected MavenProject getProject() {
105         return project;
106     }
107 
108     protected void init() throws MojoExecutionException, MojoFailureException {
109         super.init();
110 
111         ant.setProject(getProject());
112     }
113 
114     protected void doExecute() throws Exception {
115         fileset.addDefaultExcludes();
116         fileset.scan();
117 
118         String[] filenames = fileset.getIncludedFiles();
119 
120         if (filenames.length == 0) {
121             throw new MojoExecutionException("At least one pom file must be included");
122         }
123         
124         for (int i=0; i<filenames.length; i++) {
125             invoke(new File(fileset.getBasedir(), filenames[i]));
126         }
127     }
128 
129     private void invoke(final File pom) throws Exception {
130         if (!pom.exists()) {
131             throw new MojoExecutionException("Missing pom file: " + pom);
132         }
133 
134         log.info("Invoking: " + pom);
135         
136         ExecTask exec = (ExecTask)ant.createTask("exec");
137 
138         exec.setExecutable(getMavenExecutable().getAbsolutePath());
139         exec.setFailIfExecutionFails(true);
140         exec.setFailonerror(true);
141 
142         if (flags != null) {
143             for (int i=0; i < flags.length; i++) {
144                 exec.createArg().setValue(flags[i]);
145             }
146         }
147 
148         if (parameters != null) {
149             Iterator iter = parameters.keySet().iterator();
150             while (iter.hasNext()) {
151                 String name = (String)iter.next();
152                 Object value = parameters.get(name);
153                 exec.createArg().setValue("-D" + name + "=" + value);
154             }
155         }
156 
157         if (profiles != null && profiles.length != 0) {
158             StringBuffer buff = new StringBuffer("-P");
159             
160             for (int i=0; i < profiles.length; i++) {
161                 buff.append(profiles[i]);
162                 if (i + 1 < profiles.length) {
163                     buff.append(",");
164                 }
165             }
166 
167             exec.createArg().setValue(buff.toString());
168         }
169 
170         if (goals != null) {
171             for (int i=0; i < goals.length; i++) {
172                 exec.createArg().setValue(goals[i]);
173             }
174         }
175         
176         exec.createArg().setValue("-f");
177         exec.createArg().setFile(pom);
178         
179         // Batch mode to use simple download progress output
180         exec.createArg().setValue("--batch-mode");
181         
182         // Always enable verbose error output
183         exec.createArg().setValue("--errors");
184         
185         exec.execute();
186     }
187 
188     private File getMavenExecutable() throws MojoExecutionException, IOException {
189         String path = System.getProperty("maven.home");
190         if (path == null) {
191             // This should really never happen
192             throw new MojoExecutionException("Missing maven.home system property");
193         }
194 
195         File home = new File(path);
196         File cmd;
197 
198         if (Os.isFamily("windows")) {
199             cmd = new File(home, "bin/mvn.bat");
200         }
201         else {
202             cmd = new File(home, "bin/mvn");
203         }
204 
205         cmd = cmd.getCanonicalFile();
206         if (!cmd.exists()) {
207             throw new MojoExecutionException("Maven executable not found at: " + cmd);
208         }
209 
210         return cmd;
211     }
212 }