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: 509110 $ $Date: 2007-02-19 01:48:46 -0500 (Mon, 19 Feb 2007) $ 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 || modulePlan != null) { 080 log.info("Using non-artifact based module archive: " + moduleArchive); 081 log.info("Using non-artifact based plan: " + modulePlan); 082 083 TargetModuleID[] ids = distribute(moduleArchive, modulePlan); 084 completed.add(ids); 085 } 086 else if (modules == null || modules.length == 0) { 087 throw new MojoExecutionException("At least one module configuration (or moduleArchive) must be specified"); 088 } 089 090 if (modules != null && modules.length != 0) { 091 log.info("Using artifact based module archive(s)..."); 092 093 for (int i=0; i<modules.length; i++) { 094 TargetModuleID[] ids = distribute(getModuleArchive(modules[i]), modules[i].getPlan()); 095 completed.add(ids); 096 } 097 } 098 099 if (startModules) { 100 log.info("Starting modules..."); 101 102 Iterator iter = completed.iterator(); 103 while (iter.hasNext()) { 104 TargetModuleID[] moduleIds = (TargetModuleID[])iter.next(); 105 for (int i=0; i < moduleIds.length; i++) { 106 String url = moduleIds[i].getWebURL(); 107 log.info("Starting module: " + moduleIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url))); 108 } 109 110 ProgressObject progress = getDeploymentManager().start(moduleIds); 111 DeploymentStatus status = waitFor(progress); 112 113 if (status.isFailed()) { 114 throw new MojoExecutionException("Failed to start modules: " + status.getMessage()); 115 } 116 117 log.info("Started module(s):"); 118 logModules(moduleIds, " "); 119 } 120 } 121 } 122 123 private File getModuleArchive(final ModuleConfig module) throws MojoExecutionException { 124 Artifact artifact = getArtifact(module); 125 126 File file = artifact.getFile(); 127 if (file == null) { 128 throw new MojoExecutionException("Module artifact does not have an attached file: " + module); 129 } 130 131 String type = artifact.getType(); 132 log.debug("Artifact file is: " + file + " (" + type + ")"); 133 134 if ((!"war".equals(type)) && 135 (!"ear".equals(type)) && 136 (!"rar".equals(type)) && 137 (!"jar".equals(type))) 138 { 139 throw new MojoExecutionException("Module does not look like a JavaEE archive: " + module); 140 } 141 142 return file; 143 } 144 145 private TargetModuleID[] distribute(final File file, final File plan) throws Exception { 146 log.info("Distributing module artifact: " + file + " with plan " + plan); 147 148 DeploymentManager manager = getDeploymentManager(); 149 Target[] targets = manager.getTargets(); 150 ProgressObject progress = manager.distribute(targets, file, plan); 151 DeploymentStatus status = waitFor(progress); 152 153 if (status.isFailed()) { 154 // 155 // FIXME: There must be a better way to handle this. 156 // 157 if (status.getMessage().indexOf("already exists") < 0 ) { 158 throw new MojoExecutionException("Distribution failed: " + status.getMessage()); 159 } 160 log.info("Module already exists"); 161 } 162 163 164 return (progress != null) ? progress.getResultTargetModuleIDs() : null; 165 } 166 167 protected String getFullClassName() { 168 return this.getClass().getName(); 169 } 170 }