Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //========================================================================
  //$Id: AbstractJettyRunMojo.java 2147 2007-10-23 05:08:49Z gregw $
  //Copyright 2000-2004 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.util.Date;
 import java.util.List;
 
AbstractJettyRunMojo Base class for all jetty versions for the "run" mojo.
 
 public abstract class AbstractJettyRunMojo extends AbstractJettyMojo
 {

    
If true, the <testOutputDirectory> and the dependencies of <scope>test<scope> will be put first on the runtime classpath.

Parameter:
default-value="false"
 
     private boolean useTestClasspath;
    
    
    
The location of a jetty-env.xml file. Optional.

Parameter:
 
     private File jettyEnvXml;
    
    
The location of the web.xml file. If not set then it is assumed it is in ${basedir}/src/main/webapp/WEB-INF

Parameter:
expression="${maven.war.webxml}"
 
     private File 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="${basedir}/src/main/webapp"
Required:
 
     private File webAppSourceDirectory;
    
    

Parameter:
expression="${plugin.artifacts}"
Readonly:
 
     private List pluginArtifacts;
    
    
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;

    
web.xml as a File
    private File webXmlFile;
    
    
    
jetty-env.xml as a File
    private File jettyEnvXmlFile;

    
List of files on the classpath for the webapp
    private List classPathFiles;
    
    
    
Extra scan targets as a list
    private List extraScanTargets;
    public File getWebXml()
    {
        return this.;
    }
    
    public File getJettyEnvXml ()
    {
        return this.;
    }
    public File getClassesDirectory()
    {
        return this.;
    }
    {
        return this.;
    }
    public void setWebXmlFile (File f)
    {
        this. = f;
    }
    
    public File getWebXmlFile ()
    {
        return this.;
    }
    
    public File getJettyEnvXmlFile ()
    {
        return this.;
    }
    
    public void setJettyEnvXmlFile (File f)
    {
        this. = f;
    }
    
    public void setClassPathFiles (List list)
    {
        this. = new ArrayList(list);
    }
    public List getClassPathFiles ()
    {
        return this.;
    }
    public List getExtraScanTargets ()
    {
        return this.;
    }
    
    public void setExtraScanTargets(List list)
    {
        this. = list;
    }

    
    public void execute() throws MojoExecutionExceptionMojoFailureException
    {
       super.execute();
    }
    
    
    
Verify the configuration given in the pom.

    public void checkPomConfiguration () throws MojoExecutionException
    {
        // check the location of the static content/jsps etc
        try
        {
            if ((getWebAppSourceDirectory() == null) || !getWebAppSourceDirectory().exists())
                throw new MojoExecutionException("Webapp source directory "
                        + (getWebAppSourceDirectory() == null ? "null" : getWebAppSourceDirectory().getCanonicalPath())
                        + " does not exist");
            else
                getLog().info"Webapp source directory = "
                        + getWebAppSourceDirectory().getCanonicalPath());
        }
        catch (IOException e)
        {
            throw new MojoExecutionException("Webapp source directory does not exist"e);
        }
        
       
        // get the web.xml file if one has been provided, otherwise assume it is
        // in the webapp src directory
        if (getWebXml() == null )
             = new File(new File(getWebAppSourceDirectory(),"WEB-INF"), "web.xml");
        setWebXmlFile();
        
        try
        {
            if (!getWebXmlFile().exists())
                throw new MojoExecutionException"web.xml does not exist at location "
                        + .getCanonicalPath());
            else
                getLog().info"web.xml file = "
                        + .getCanonicalPath());
        }
        catch (IOException e)
        {
            throw new MojoExecutionException("web.xml does not exist"e);
        }
        
        //check if a jetty-env.xml location has been provided, if so, it must exist
        if  (getJettyEnvXml() != null)
        {
            setJettyEnvXmlFile();
            
            try
            {
                if (!getJettyEnvXmlFile().exists())
                    throw new MojoExecutionException("jetty-env.xml file does not exist at location "+);
                else
                    getLog().info(" jetty-env.xml = "+getJettyEnvXmlFile().getCanonicalPath());
            }
            catch (IOException e)
            {
                throw new MojoExecutionException("jetty-env.xml does not exist");
            }
        }
        
        
        // 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");
        }
        
        
        
        if ( == null)
        else
        {
            ArrayList list = new ArrayList();
            for (int i=0; i.i++)
            {
                getLog().info("Added extra scan target:"[i]);
                list.add([i]);
            }
            setExtraScanTargets(list);
        }
        
        
        if (!=null)
        {
            for (int i=0;i<.i++)
            {
                Iterator itor = [i].getIncludes().iterator();
                StringBuffer strbuff = new StringBuffer();
                while (itor.hasNext())
                {
                    strbuff.append((String)itor.next());
                    if (itor.hasNext())
                        strbuff.append(",");
                }
                String includes = strbuff.toString();
                
                itor = [i].getExcludes().iterator();
                strbuffnew StringBuffer();
                while (itor.hasNext())
                {
                    strbuff.append((String)itor.next());
                    if (itor.hasNext())
                        strbuff.append(",");
                }
                String excludes = strbuff.toString();
                try
                {
                    List files = FileUtils.getFiles([i].getDirectory(), includesexcludes);
                    itor = files.iterator();
                    while (itor.hasNext())
                        getLog().info("Adding extra scan target from pattern: "+itor.next());
                    setExtraScanTargets(files);
                }
                catch (IOException e)
                {
                    throw new MojoExecutionException(e.getMessage());
                }
            }
            
           
        }
    }
   
    public void configureWebApplication() throws Exception
    {
       super.configureWebApplication();
        getLog().info("Webapp directory = " + getWebAppSourceDirectory().getCanonicalPath());
        .configure();
    }
    
    public void configureScanner ()
    {
        // start the scanner thread (if necessary) on the main webapp
        final ArrayList scanList = new ArrayList();
        scanList.add(getWebXmlFile());
        if (getJettyEnvXmlFile() != null)
            scanList.add(getJettyEnvXmlFile());
        File jettyWebXmlFile = findJettyWebXmlFile(new File(getWebAppSourceDirectory(),"WEB-INF"));
        if (jettyWebXmlFile != null)
            scanList.add(jettyWebXmlFile);
        scanList.addAll(getExtraScanTargets());
        scanList.add(getProject().getFile());
        scanList.addAll(getClassPathFiles());
        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 reconfigureScannerthrows Exception 
    {
        getLog().info("restarting "+);
        getLog().debug("Stopping 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 ...");
            .clear();
            .add(getWebXmlFile());
            if (getJettyEnvXmlFile() != null)
                .add(getJettyEnvXmlFile());
            .addAll(getExtraScanTargets());
            .add(getProject().getFile());
            .addAll(getClassPathFiles());
            getScanner().setScanDirs();
        }
        getLog().debug("Restarting webapp ...");
        .start();
        getLog().info("Restart completed at "+new Date().toString());
    }
    
    private List getDependencyFiles ()
    {
        List dependencyFiles = new ArrayList();
        for ( Iterator iter = getProject().getArtifacts().iterator(); iter.hasNext(); )
        {
            Artifact artifact = (Artifactiter.next();
            // Include runtime and compile time libraries, and possibly test libs too
            if (((!..equals(artifact.getScope())) && (!..equalsartifact.getScope()))) 
                    ||
                ( && ..equalsartifact.getScope())))
            {
                dependencyFiles.add(artifact.getFile());
                getLog().debug"Adding artifact " + artifact.getFile().getName() + " for WEB-INF/lib " );   
            }
        }
        return dependencyFiles
    }
    
    
   
    private List setUpClassPath()
    {
        List classPathFiles = new ArrayList();       
        
        //if using the test classes, make sure they are first
        //on the list
        if ( && ( != null))
            classPathFiles.add();
        
        if (getClassesDirectory() != null)
            classPathFiles.add(getClassesDirectory());
        
        //now add all of the dependencies
        classPathFiles.addAll(getDependencyFiles());
        
        if (getLog().isDebugEnabled())
        {
            for (int i = 0; i < classPathFiles.size(); i++)
            {
                getLog().debug("classpath element: "+ ((FileclassPathFiles.get(i)).getName());
            }
        }
        return classPathFiles;
    }
New to GrepCode? Check out our FAQ X