001: //========================================================================
002: //$Id: Jetty6RunWarExploded.java 1246 2006-11-18 10:40:27Z janb $
003: //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.plugin;
017:
018: import java.io.File;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.maven.plugin.MojoExecutionException;
023: import org.apache.maven.plugin.MojoFailureException;
024: import org.mortbay.jetty.Connector;
025: import org.mortbay.jetty.plugin.util.Scanner;
026: import org.mortbay.jetty.security.UserRealm;
027:
028: /**
029: *
030: * <p>
031: * This goal is used to assemble your webapp into an exploded war and automatically deploy it to Jetty.
032: * </p>
033: * <p>
034: * Once invoked, the plugin can be configured to run continuously, scanning for changes in the pom.xml and
035: * to WEB-INF/web.xml, WEB-INF/classes or WEB-INF/lib and hot redeploy when a change is detected.
036: * </p>
037: * <p>
038: * You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
039: * This can be used, for example, to deploy a static webapp that is not part of your maven build.
040: * </p>
041: * <p>
042: * There is a <a href="run-mojo.html">reference guide</a> to the configuration parameters for this plugin, and more detailed information
043: * with examples in the <a href="howto.html">Configuration How-To</a>.
044: * </p>
045: *
046: *@goal run-exploded
047: *@execute phase=package
048: */
049: public class Jetty6RunWarExploded extends AbstractJetty6Mojo {
050:
051: /**
052: * The location of the war file.
053: * @parameter expression="${project.build.directory}/${project.build.finalName}"
054: * @required
055: */
056: private File webApp;
057:
058: /**
059: *
060: * @see org.mortbay.jetty.plugin.AbstractJettyMojo#checkPomConfiguration()
061: */
062: public void checkPomConfiguration() throws MojoExecutionException {
063: return;
064: }
065:
066: /**
067: * @see org.mortbay.jetty.plugin.AbstractJettyMojo#configureScanner()
068: */
069: public void configureScanner() throws MojoExecutionException {
070: final ArrayList scanList = new ArrayList();
071: scanList.add(getProject().getFile());
072: File webInfDir = new File(webApp, "WEB-INF");
073: scanList.add(new File(webInfDir, "web.xml"));
074: File jettyWebXmlFile = findJettyWebXmlFile(webInfDir);
075: if (jettyWebXmlFile != null)
076: scanList.add(jettyWebXmlFile);
077: File jettyEnvXmlFile = new File(webInfDir, "jetty-env.xml");
078: if (jettyEnvXmlFile.exists())
079: scanList.add(jettyEnvXmlFile);
080: scanList.add(new File(webInfDir, "classes"));
081: scanList.add(new File(webInfDir, "lib"));
082: setScanList(scanList);
083:
084: ArrayList listeners = new ArrayList();
085: listeners.add(new Scanner.Listener() {
086: public void changesDetected(Scanner scanner, List changes) {
087: try {
088: getLog().info("Restarting webapp");
089: getLog().debug("Stopping webapp ...");
090: getWebApplication().stop();
091: getLog().debug("Reconfiguring webapp ...");
092:
093: checkPomConfiguration();
094:
095: // check if we need to reconfigure the scanner,
096: // which is if the pom changes
097: if (changes.contains(getProject().getFile()
098: .getCanonicalPath())) {
099: getLog()
100: .info(
101: "Reconfiguring scanner after change to pom.xml ...");
102: ArrayList scanList = getScanList();
103: scanList.clear();
104: scanList.add(getProject().getFile());
105: File webInfDir = new File(webApp, "WEB-INF");
106: scanList.add(new File(webInfDir, "web.xml"));
107: File jettyWebXmlFile = findJettyWebXmlFile(webInfDir);
108: if (jettyWebXmlFile != null)
109: scanList.add(jettyWebXmlFile);
110: File jettyEnvXmlFile = new File(webInfDir,
111: "jetty-env.xml");
112: if (jettyEnvXmlFile.exists())
113: scanList.add(jettyEnvXmlFile);
114: scanList.add(new File(webInfDir, "classes"));
115: scanList.add(new File(webInfDir, "lib"));
116: setScanList(scanList);
117: scanner.setRoots(scanList);
118: }
119:
120: getLog().debug("Restarting webapp ...");
121: getWebApplication().start();
122: getLog().info("Restart completed.");
123: } catch (Exception e) {
124: getLog()
125: .error(
126: "Error reconfiguring/restarting webapp after change in watched files",
127: e);
128: }
129: }
130: });
131: setScannerListeners(listeners);
132: }
133:
134: /* (non-Javadoc)
135: * @see org.mortbay.jetty.plugin.util.AbstractJettyMojo#finishConfigurationBeforeStart()
136: */
137: public void finishConfigurationBeforeStart() throws Exception {
138: return;
139: }
140:
141: public void configureWebApplication() throws Exception {
142: super .configureWebApplication();
143: getWebApplication().setWebAppSrcDir(webApp);
144: }
145:
146: public void execute() throws MojoExecutionException,
147: MojoFailureException {
148: super.execute();
149: }
150:
151: }
|