All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mortbay.jetty.plugin.JettyRunMojo Maven / Gradle / Ivy

There is a newer version: 8.1.16.v20140903
Show newest version
//========================================================================
//$Id$
//Copyright 2000-2009 Mort Bay Consulting Pty. Ltd.
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package org.mortbay.jetty.plugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.jetty.util.Scanner;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 *  

* This goal is used in-situ on a Maven project without first requiring that the project * is assembled into a war, saving time during the development cycle. * The plugin forks a parallel lifecycle to ensure that the "compile" phase has been completed before invoking Jetty. This means * that you do not need to explicity execute a "mvn compile" first. It also means that a "mvn clean jetty:run" will ensure that * a full fresh compile is done before invoking Jetty. *

*

* Once invoked, the plugin can be configured to run continuously, scanning for changes in the project and automatically performing a * hot redeploy when necessary. This allows the developer to concentrate on coding changes to the project using their IDE of choice and have those changes * immediately and transparently reflected in the running web container, eliminating development time that is wasted on rebuilding, reassembling and redeploying. *

*

* You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration. * This can be used, for example, to deploy a static webapp that is not part of your maven build. *

*

* There is a reference guide to the configuration parameters for this plugin, and more detailed information * with examples in the Configuration Guide. *

* * * @goal run * @requiresDependencyResolution test * @execute phase="test-compile" * @description Runs jetty directly from a maven project */ public class JettyRunMojo extends AbstractJettyMojo { public static final String DEFAULT_WEBAPP_SRC = "src"+File.separator+"main"+File.separator+"webapp"; /** * If true, the <testOutputDirectory> * and the dependencies of <scope>test<scope> * will be put first on the runtime classpath. * * @parameter alias="useTestClasspath" default-value="false" */ private boolean useTestScope; /** * The default location of the web.xml file. Will be used * if <webApp><descriptor> is not set. * * @parameter expression="${maven.war.webxml}" * @readonly */ private String webXml; /** * The directory containing generated classes. * * @parameter expression="${project.build.outputDirectory}" * @required * */ private File classesDirectory; /** * The directory containing generated test classes. * * @parameter expression="${project.build.testOutputDirectory}" * @required */ private File testClassesDirectory; /** * Root directory for all html/jsp etc files * * @parameter expression="${maven.war.src}" * */ private File webAppSourceDirectory; /** * List of files or directories to additionally periodically scan for changes. Optional. * @parameter */ private File[] scanTargets; /** * List of directories with ant-style <include> and <exclude> patterns * for extra targets to periodically scan for changes. Can be used instead of, * or in conjunction with <scanTargets>.Optional. * @parameter */ private ScanTargetPattern[] scanTargetPatterns; /** * Extra scan targets as a list */ private List extraScanTargets; /** * Verify the configuration given in the pom. * * @see org.mortbay.jetty.plugin.AbstractJettyMojo#checkPomConfiguration() */ public void checkPomConfiguration () throws MojoExecutionException { // check the location of the static content/jsps etc try { if ((getWebAppSourceDirectory() == null) || !getWebAppSourceDirectory().exists()) { File defaultWebAppSrcDir = new File (project.getBasedir(), DEFAULT_WEBAPP_SRC); getLog().info("webAppSourceDirectory"+(getWebAppSourceDirectory()==null?" not set.":" does not exist.")+" Defaulting to "+defaultWebAppSrcDir.getAbsolutePath()); webAppSourceDirectory = defaultWebAppSrcDir; } else getLog().info( "Webapp source directory = " + getWebAppSourceDirectory().getCanonicalPath()); } catch (IOException e) { throw new MojoExecutionException("Webapp source directory does not exist", e); } // check reload mechanic if ( !"automatic".equalsIgnoreCase( reload ) && !"manual".equalsIgnoreCase( reload ) ) { throw new MojoExecutionException( "invalid reload mechanic specified, must be 'automatic' or 'manual'" ); } else { getLog().info("Reload Mechanic: " + reload ); } // check the classes to form a classpath with try { //allow a webapp with no classes in it (just jsps/html) if (getClassesDirectory() != null) { if (!getClassesDirectory().exists()) getLog().info( "Classes directory "+ getClassesDirectory().getCanonicalPath()+ " does not exist"); else getLog().info("Classes = " + getClassesDirectory().getCanonicalPath()); } else getLog().info("Classes directory not set"); } catch (IOException e) { throw new MojoExecutionException("Location of classesDirectory does not exist"); } setExtraScanTargets(new ArrayList()); if (scanTargets != null) { for (int i=0; i< scanTargets.length; i++) { getLog().info("Added extra scan target:"+ scanTargets[i]); getExtraScanTargets().add(scanTargets[i]); } } if (scanTargetPatterns!=null) { for (int i=0;i files = FileUtils.getFiles(scanTargetPatterns[i].getDirectory(), includes, excludes); itor = files.iterator(); while (itor.hasNext()) getLog().info("Adding extra scan target from pattern: "+itor.next()); List currentTargets = getExtraScanTargets(); if(currentTargets!=null && !currentTargets.isEmpty()) currentTargets.addAll(files); else setExtraScanTargets(files); } catch (IOException e) { throw new MojoExecutionException(e.getMessage()); } } } } public void configureWebApplication() throws Exception { super.configureWebApplication(); //Set up the location of the webapp. //There are 2 parts to this: setWar() and setBaseResource(). On standalone jetty, //the former could be the location of a packed war, while the latter is the location //after any unpacking. With this mojo, you are running an unpacked, unassembled webapp, //so the two locations should be equal. Resource webAppSourceDirectoryResource = Resource.newResource(webAppSourceDirectory.getCanonicalPath()); if (webApp.getWar() == null) webApp.setWar(webAppSourceDirectoryResource.toString()); if (webApp.getBaseResource() == null) webApp.setBaseResource(webAppSourceDirectoryResource); if (getClassesDirectory() != null) webApp.setClasses (getClassesDirectory()); if (useTestScope && (testClassesDirectory != null)) webApp.setTestClasses (testClassesDirectory); webApp.setWebInfLib (getDependencyFiles()); //if we have not already set web.xml location, need to set one up if (webApp.getDescriptor() == null) { //Has an explicit web.xml file been configured to use? if (webXml != null) { Resource r = Resource.newResource(webXml); if (r.exists() && !r.isDirectory()) { webApp.setDescriptor(r.toString()); } } //Still don't have a web.xml file: try the resourceBase of the webapp, if it is set if (webApp.getDescriptor() == null && webApp.getBaseResource() != null) { Resource r = webApp.getBaseResource().addPath("WEB-INF/web.xml"); if (r.exists() && !r.isDirectory()) { webApp.setDescriptor(r.toString()); } } //Still don't have a web.xml file: finally try the configured static resource directory if there is one if (webApp.getDescriptor() == null && (webAppSourceDirectory != null)) { File f = new File (new File (webAppSourceDirectory, "WEB-INF"), "web.xml"); if (f.exists() && f.isFile()) { webApp.setDescriptor(f.getCanonicalPath()); } } } getLog().info( "web.xml file = "+webApp.getDescriptor()); getLog().info("Webapp directory = " + getWebAppSourceDirectory().getCanonicalPath()); } public void configureScanner () throws MojoExecutionException { // start the scanner thread (if necessary) on the main webapp final ArrayList scanList = new ArrayList(); if (webApp.getDescriptor() != null) { try { Resource r = Resource.newResource(webApp.getDescriptor()); scanList.add(r.getFile()); } catch (IOException e) { throw new MojoExecutionException("Problem configuring scanner for web.xml", e); } } if (webApp.getJettyEnvXml() != null) { try { Resource r = Resource.newResource(webApp.getJettyEnvXml()); scanList.add(r.getFile()); } catch (IOException e) { throw new MojoExecutionException("Problem configuring scanner for jetty-env.xml", e); } } if (webApp.getDefaultsDescriptor() != null) { try { if (!WebAppContext.WEB_DEFAULTS_XML.equals(webApp.getDefaultsDescriptor())) { Resource r = Resource.newResource(webApp.getDefaultsDescriptor()); scanList.add(r.getFile()); } } catch (IOException e) { throw new MojoExecutionException("Problem configuring scanner for webdefaults.xml", e); } } if (webApp.getOverrideDescriptor() != null) { try { Resource r = Resource.newResource(webApp.getOverrideDescriptor()); scanList.add(r.getFile()); } catch (IOException e) { throw new MojoExecutionException("Problem configuring scanner for webdefaults.xml", e); } } File jettyWebXmlFile = findJettyWebXmlFile(new File(getWebAppSourceDirectory(),"WEB-INF")); if (jettyWebXmlFile != null) scanList.add(jettyWebXmlFile); scanList.addAll(getExtraScanTargets()); scanList.add(getProject().getFile()); if (webApp.getTestClasses() != null) scanList.add(webApp.getTestClasses()); if (webApp.getClasses() != null) scanList.add(webApp.getClasses()); scanList.addAll(webApp.getWebInfLib()); setScanList(scanList); ArrayList listeners = new ArrayList(); listeners.add(new Scanner.BulkListener() { public void filesChanged (List changes) { try { boolean reconfigure = changes.contains(getProject().getFile().getCanonicalPath()); restartWebApp(reconfigure); } catch (Exception e) { getLog().error("Error reconfiguring/restarting webapp after change in watched files",e); } } }); setScannerListeners(listeners); } public void restartWebApp(boolean reconfigureScanner) throws Exception { getLog().info("restarting "+webApp); getLog().debug("Stopping webapp ..."); webApp.stop(); getLog().debug("Reconfiguring webapp ..."); checkPomConfiguration(); configureWebApplication(); // check if we need to reconfigure the scanner, // which is if the pom changes if (reconfigureScanner) { getLog().info("Reconfiguring scanner after change to pom.xml ..."); scanList.clear(); scanList.add(new File(webApp.getDescriptor())); if (webApp.getJettyEnvXml() != null) scanList.add(new File(webApp.getJettyEnvXml())); scanList.addAll(getExtraScanTargets()); scanList.add(getProject().getFile()); if (webApp.getTestClasses() != null) scanList.add(webApp.getTestClasses()); if (webApp.getClasses() != null) scanList.add(webApp.getClasses()); scanList.addAll(webApp.getWebInfLib()); getScanner().setScanDirs(scanList); } getLog().debug("Restarting webapp ..."); webApp.start(); getLog().info("Restart completed at "+new Date().toString()); } private List getDependencyFiles () { List dependencyFiles = new ArrayList(); List overlays = new ArrayList(); for ( Iterator iter = projectArtifacts.iterator(); iter.hasNext(); ) { Artifact artifact = (Artifact) iter.next(); // Include runtime and compile time libraries, and possibly test libs too if(artifact.getType().equals("war")) { try { Resource r=Resource.newResource("jar:"+Resource.toURL(artifact.getFile()).toString()+"!/"); overlays.add(r); getLog().info("Adding overlay for war project artifact "+artifact.getId()); getExtraScanTargets().add(artifact.getFile()); } catch(Exception e) { throw new RuntimeException(e); } continue; } if (Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) continue; //never add dependencies of scope=provided to the webapp's classpath (see also param) if (Artifact.SCOPE_TEST.equals(artifact.getScope()) && !useTestScope) continue; //only add dependencies of scope=test if explicitly required dependencyFiles.add(artifact.getFile()); getLog().debug( "Adding artifact " + artifact.getFile().getName() + " with scope "+artifact.getScope()+" for WEB-INF/lib " ); } webApp.setOverlays(overlays); return dependencyFiles; } private List setUpClassPath(File webInfClasses, File testClasses, List webInfJars) { List classPathFiles = new ArrayList(); if (webInfClasses != null) classPathFiles.add(webInfClasses); if (testClasses != null) classPathFiles.add(testClasses); classPathFiles.addAll(webInfJars); if (getLog().isDebugEnabled()) { for (int i = 0; i < classPathFiles.size(); i++) { getLog().debug("classpath element: "+ ((File) classPathFiles.get(i)).getName()); } } return classPathFiles; } private List getClassesDirs () { List classesDirs = new ArrayList(); //if using the test classes, make sure they are first //on the list if (useTestScope && (testClassesDirectory != null)) classesDirs.add(testClassesDirectory); if (getClassesDirectory() != null) classesDirs.add(getClassesDirectory()); return classesDirs; } public void execute() throws MojoExecutionException, MojoFailureException { super.execute(); } public String getWebXml() { return this.webXml; } public void setWebXml(String webXml) { this.webXml = webXml; } public File getClassesDirectory() { return this.classesDirectory; } public void setClassesDirectory(File classesDirectory) { this.classesDirectory = classesDirectory; } public File getWebAppSourceDirectory() { return this.webAppSourceDirectory; } public void setWebAppSourceDirectory(File webAppSourceDirectory) { this.webAppSourceDirectory = webAppSourceDirectory; } public List getExtraScanTargets() { return this.extraScanTargets; } public void setExtraScanTargets(List list) { this.extraScanTargets = list; } public boolean isUseTestClasspath() { return useTestScope; } public void setUseTestClasspath(boolean useTestClasspath) { this.useTestScope = useTestClasspath; } public File getTestClassesDirectory() { return testClassesDirectory; } public void setTestClassesDirectory(File testClassesDirectory) { this.testClassesDirectory = testClassesDirectory; } public File[] getScanTargets() { return scanTargets; } public void setScanTargets(File[] scanTargets) { this.scanTargets = scanTargets; } public ScanTargetPattern[] getScanTargetPatterns() { return scanTargetPatterns; } public void setScanTargetPatterns(ScanTargetPattern[] scanTargetPatterns) { this.scanTargetPatterns = scanTargetPatterns; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy