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.mavenplugins.geronimo.module;
021
022 import java.io.File;
023 import java.util.List;
024 import java.util.ArrayList;
025 import java.util.Iterator;
026
027 import javax.enterprise.deploy.spi.DeploymentManager;
028 import javax.enterprise.deploy.spi.Target;
029 import javax.enterprise.deploy.spi.TargetModuleID;
030 import javax.enterprise.deploy.spi.status.ProgressObject;
031 import javax.enterprise.deploy.spi.status.DeploymentStatus;
032
033 import org.apache.maven.artifact.Artifact;
034 import org.apache.maven.plugin.MojoExecutionException;
035
036 import org.apache.geronimo.mavenplugins.geronimo.ModuleConfig;
037
038 /**
039 * Deploy modules (and optionally starting them) to a Geronimo server.
040 *
041 * @goal deploy-module
042 *
043 * @version $Rev: 450613 $ $Date: 2006-09-27 15:45:46 -0700 (Wed, 27 Sep 2006) $
044 */
045 public class DeployModuleMojo
046 extends ModuleMojoSupport
047 {
048 /**
049 * A file which points to a specific module's jar | war | ear | rar archive.
050 * If this parameter is set, then it will be used instead of from the
051 * modules configuration.
052 *
053 * @parameter expression="${moduleArchive}"
054 * @optional
055 */
056 protected File moduleArchive = null;
057
058 /**
059 * The fully qualified path of the external plan file (geronimo-web.xml).
060 * The application module may already have included in the package a deployment plan or the
061 * application is so simple that may not require any deployment plan.
062 *
063 * @parameter expression="${modulePlan}"
064 * @optional
065 */
066 private File modulePlan = null;
067
068 /**
069 * Flag to indicate if modules should be started after they have been distributed to the server.
070 *
071 * @parameter default-value="true"
072 * @optional
073 */
074 private boolean startModules = false;
075
076 protected void doExecute() throws Exception {
077 List completed = new ArrayList();
078
079 if (moduleArchive != null) {
080 log.info("Using non-artifact based module archive: " + moduleArchive);
081
082 TargetModuleID[] ids = distribute(moduleArchive, modulePlan);
083 completed.add(ids);
084 }
085 else if (modules == null || modules.length == 0) {
086 throw new MojoExecutionException("At least one module configuration (or moduleArchive) must be specified");
087 }
088
089 if (modules != null && modules.length != 0) {
090 log.info("Using artifact based module archive(s)...");
091
092 for (int i=0; i<modules.length; i++) {
093 TargetModuleID[] ids = distribute(getModuleArchive(modules[i]), modules[i].getPlan());
094 completed.add(ids);
095 }
096 }
097
098 if (startModules) {
099 log.info("Starting modules...");
100
101 Iterator iter = completed.iterator();
102 while (iter.hasNext()) {
103 TargetModuleID[] moduleIds = (TargetModuleID[])iter.next();
104 for (int i=0; i < moduleIds.length; i++) {
105 String url = moduleIds[i].getWebURL();
106 log.info("Starting module: " + moduleIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url)));
107 }
108
109 ProgressObject progress = getDeploymentManager().start(moduleIds);
110 DeploymentStatus status = waitFor(progress);
111
112 if (status.isFailed()) {
113 throw new MojoExecutionException("Failed to start modules: " + status.getMessage());
114 }
115
116 log.info("Started module(s):");
117 logModules(moduleIds, " ");
118 }
119 }
120 }
121
122 private File getModuleArchive(final ModuleConfig module) throws MojoExecutionException {
123 Artifact artifact = getArtifact(module);
124
125 File file = artifact.getFile();
126 if (file == null) {
127 throw new MojoExecutionException("Module artifact does not have an attached file: " + module);
128 }
129
130 String type = artifact.getType();
131 log.debug("Artifact file is: " + file + " (" + type + ")");
132
133 if ((!"war".equals(type)) &&
134 (!"ear".equals(type)) &&
135 (!"rar".equals(type)) &&
136 (!"jar".equals(type)))
137 {
138 throw new MojoExecutionException("Module does not look like a J2EE archive: " + module);
139 }
140
141 return file;
142 }
143
144 private TargetModuleID[] distribute(final File file, final File plan) throws Exception {
145 assert file != null;
146
147 log.info("Distributing module artifact: " + file);
148
149 DeploymentManager manager = getDeploymentManager();
150 Target[] targets = manager.getTargets();
151 ProgressObject progress = manager.distribute(targets, file, plan);
152 DeploymentStatus status = waitFor(progress);
153
154 if (status.isFailed()) {
155 throw new MojoExecutionException("Distribution failed: " + status.getMessage());
156 }
157
158 return progress.getResultTargetModuleIDs();
159 }
160
161 protected String getGoalName() {
162 //
163 // FIXME: There has to be way this can be computed instead of hardcoded absolutely.
164 //
165 return "deploy-module";
166 }
167 }