001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.geronimo.genesis.plugins.maven;
021
022 import java.io.File;
023 import java.io.IOException;
024
025 import java.util.Map;
026 import java.util.Iterator;
027
028 import org.codehaus.mojo.pluginsupport.MojoSupport;
029 import org.codehaus.mojo.pluginsupport.ant.AntHelper;
030
031 import org.apache.maven.project.MavenProject;
032 import org.apache.maven.plugin.MojoExecutionException;
033 import org.apache.maven.plugin.MojoFailureException;
034
035 import org.codehaus.plexus.util.Os;
036 import org.codehaus.plexus.util.DirectoryScanner;
037
038 import org.apache.tools.ant.taskdefs.ExecTask;
039
040 /**
041 * Invoke Maven in a sub-process.
042 *
043 * @goal invoke
044 *
045 * @version $Rev: 512096 $ $Date: 2007-02-27 01:58:04 +0100 (Tue, 27 Feb 2007) $
046 */
047 public class InvokeMavenMojo
048 extends MojoSupport
049 {
050 /**
051 * Defines the set of pom.xml files to invoke.
052 *
053 * @parameter
054 * @required
055 */
056 private DirectoryScanner fileset = null;
057
058 /**
059 * A set of command-line flags to pass to Maven.
060 *
061 * @parameter
062 */
063 private String[] flags = null;
064
065 /**
066 * A map of parameters to define via -D
067 *
068 * @parameter
069 */
070 private Map parameters = null;
071
072 /**
073 * A set of profiles to activate via -P
074 *
075 * @parameter
076 */
077 private String[] profiles = null;
078
079 /**
080 * A set of goals (or phases) to be invoked.
081 *
082 * @parameter
083 */
084 private String[] goals = null;
085
086 /**
087 * @component
088 */
089 protected AntHelper ant;
090
091 //
092 // MojoSupport Hooks
093 //
094
095 /**
096 * The maven project.
097 *
098 * @parameter expression="${project}"
099 * @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 }