001: //========================================================================
002: //$Id: Jetty6RunWar.java 661 2006-07-06 10:38: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.plugin.util.Scanner;
025:
026: /**
027: * <p>
028: * This goal is used to assemble your webapp into a war and automatically deploy it to Jetty.
029: * </p>
030: * <p>
031: * Once invoked, the plugin can be configured to run continuously, scanning for changes in the project and to the
032: * war file and automatically performing a
033: * hot redeploy when necessary.
034: * </p>
035: * <p>
036: * You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
037: * This can be used, for example, to deploy a static webapp that is not part of your maven build.
038: * </p>
039: * <p>
040: * There is a <a href="run-mojo.html">reference guide</a> to the configuration parameters for this plugin, and more detailed information
041: * with examples in the <a href="howto.html">Configuration How-To</a>.
042: * </p>
043: *
044: * @goal run-war
045: * @requiresDependencyResolution runtime
046: * @execute phase="package"
047: * @description Runs jetty6 on a war file
048: *
049: */
050: public class Jetty6RunWar extends AbstractJetty6Mojo {
051:
052: /**
053: * The location of the war file.
054: * @parameter expression="${project.build.directory}/${project.build.finalName}.war"
055: * @required
056: */
057: private File webApp;
058:
059: /**
060: * @see org.apache.maven.plugin.Mojo#execute()
061: */
062: public void execute() throws MojoExecutionException,
063: MojoFailureException {
064: super .execute();
065: }
066:
067: public void configureWebApplication() throws Exception {
068: super .configureWebApplication();
069:
070: getWebApplication().setWebAppSrcDir(webApp);
071: }
072:
073: /**
074: * @see org.mortbay.jetty.plugin.AbstractJettyMojo#checkPomConfiguration()
075: */
076: public void checkPomConfiguration() throws MojoExecutionException {
077: return;
078: }
079:
080: /* (non-Javadoc)
081: * @see org.mortbay.jetty.plugin.util.AbstractJettyMojo#configureScanner()
082: */
083: public void configureScanner() throws MojoExecutionException {
084: final ArrayList scanList = new ArrayList();
085: scanList.add(getProject().getFile());
086: scanList.add(webApp);
087: setScanList(scanList);
088:
089: ArrayList listeners = new ArrayList();
090: listeners.add(new Scanner.Listener() {
091: public void changesDetected(Scanner scanner, List changes) {
092: try {
093: getLog().info("Restarting webapp ...");
094: getLog().debug("Stopping webapp ...");
095: getWebApplication().stop();
096: getLog().debug("Reconfiguring webapp ...");
097:
098: checkPomConfiguration();
099:
100: // check if we need to reconfigure the scanner,
101: // which is if the pom changes
102: if (changes.contains(getProject().getFile()
103: .getCanonicalPath())) {
104: getLog()
105: .info(
106: "Reconfiguring scanner after change to pom.xml ...");
107: ArrayList scanList = getScanList();
108: scanList.clear();
109: scanList.add(getProject().getFile());
110: scanList.add(webApp);
111: setScanList(scanList);
112: scanner.setRoots(scanList);
113: }
114:
115: getLog().debug("Restarting webapp ...");
116: getWebApplication().start();
117: getLog().info("Restart completed.");
118: } catch (Exception e) {
119: getLog()
120: .error(
121: "Error reconfiguring/restarting webapp after change in watched files",
122: e);
123: }
124: }
125: });
126: setScannerListeners(listeners);
127:
128: }
129:
130: /**
131: * @see org.mortbay.jetty.plugin.AbstractJettyMojo#finishConfigurationBeforeStart()
132: */
133: public void finishConfigurationBeforeStart() {
134: return;
135: }
136:
137: }
|