diff options
author | Jan Bartel | 2014-07-10 07:28:09 +0000 |
---|---|---|
committer | Jan Bartel | 2014-07-10 07:29:03 +0000 |
commit | c674c410031fdcc19aea67e9375f9ecfae1b4f67 (patch) | |
tree | 7538a131ded82c3b8a5d14f9898d2cf52549b14e /jetty-maven-plugin | |
parent | b13859c64163c5086aa787c0b5bb20ec4fe67abd (diff) | |
download | org.eclipse.jetty.project-c674c410031fdcc19aea67e9375f9ecfae1b4f67.tar.gz org.eclipse.jetty.project-c674c410031fdcc19aea67e9375f9ecfae1b4f67.tar.xz org.eclipse.jetty.project-c674c410031fdcc19aea67e9375f9ecfae1b4f67.zip |
438895 Add mvn jetty:effective-web-xml goal
Diffstat (limited to 'jetty-maven-plugin')
3 files changed, 191 insertions, 18 deletions
diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java new file mode 100644 index 0000000000..6c607c85df --- /dev/null +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java @@ -0,0 +1,157 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + + +package org.eclipse.jetty.maven.plugin; + +import java.io.File; +import java.io.IOException; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.eclipse.jetty.annotations.AnnotationConfiguration; +import org.eclipse.jetty.util.IO; +import org.eclipse.jetty.util.resource.Resource; +import org.eclipse.jetty.util.thread.QueuedThreadPool; + +/** + * JettyEffectiveWebXml + * + * @goal effective-web-xml + * @requiresDependencyResolution test + * @execute phase="test-compile" + * @description Runs jetty on the unassembled webapp to generate the effective web.xml + */ +public class JettyEffectiveWebXml extends JettyRunMojo +{ + /** + * The target directory + * + * @parameter expression="${project.build.directory}" + * @required + * @readonly + */ + protected File target; + + /** + * The target directory + * + * @parameter + */ + protected File effectiveWebXml; + + + protected boolean deleteOnExit = true; + + + /** + * @see org.apache.maven.plugin.Mojo#execute() + */ + public void execute() throws MojoExecutionException, MojoFailureException + { + super.execute(); + } + + + @Override + public void startJetty() throws MojoExecutionException + { + //Only do enough setup to be able to produce a quickstart-web.xml file to + //pass onto the forked process to run + + //if the user didn't nominate a file to generate into, pick the name and + //make sure that it is deleted on exit + if (effectiveWebXml == null) + { + deleteOnExit = true; + effectiveWebXml = new File(target, "effective-web.xml"); + effectiveWebXml.deleteOnExit(); + } + + Resource descriptor = Resource.newResource(effectiveWebXml); + + QueuedThreadPool tpool = null; + + try + { + printSystemProperties(); + + //apply any config from a jetty.xml file first to our "fake" server instance + //TODO probably not necessary + applyJettyXml (); + + + server.configureHandlers(); + + //ensure config of the webapp based on settings in plugin + configureWebApplication(); + + + //set the webapp up to do very little other than generate the quickstart-web.xml + webApp.setCopyWebDir(false); + webApp.setCopyWebInf(false); + webApp.setGenerateQuickStart(true); + + if (!effectiveWebXml.getParentFile().exists()) + effectiveWebXml.getParentFile().mkdirs(); + if (!effectiveWebXml.exists()) + effectiveWebXml.createNewFile(); + + webApp.setQuickStartWebDescriptor(descriptor); + + server.addWebApplication(webApp); + + //if our server has a thread pool associated we can do any annotation scanning multithreaded, + //otherwise scanning will be single threaded + tpool = server.getBean(QueuedThreadPool.class); + if (tpool != null) + tpool.start(); + else + webApp.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE.toString()); + + webApp.start(); //just enough to generate the quickstart + + } + catch (Exception e) + { + throw new MojoExecutionException("Effective web.xml generation failed", e); + } + finally + { + try {webApp.stop();}catch (Exception x) {}; + + try {if (tpool != null) tpool.stop();} catch (Exception x) {}; + } + + + if (deleteOnExit) + { + try + { + //just show the result in the log + getLog().info(IO.toString(descriptor.getInputStream())); + } + catch (IOException e) + { + throw new MojoExecutionException("Unable to output effective web.xml", e); + } + + } + + } +} diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java index 663749d033..aa73a33860 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java @@ -43,6 +43,7 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.eclipse.jetty.annotations.AnnotationConfiguration; +import org.eclipse.jetty.quickstart.QuickStartDescriptorGenerator; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceCollection; @@ -98,6 +99,13 @@ public class JettyRunForkedMojo extends JettyRunMojo */ protected File target; + /** + * The file into which to generate the quickstart web xml for the forked process to use + * + * @parameter expression="${project.build.directory}/fork-web.xml" + */ + protected File forkWebXml; + /** * Arbitrary jvm args to pass to the forked process @@ -229,7 +237,11 @@ public class JettyRunForkedMojo extends JettyRunMojo public void startJetty() throws MojoExecutionException { //Only do enough setup to be able to produce a quickstart-web.xml file to - //pass onto the forked process to run + //pass onto the forked process to run + + if (forkWebXml == null) + forkWebXml = new File (target, "fork-web.xml"); + try { printSystemProperties(); @@ -251,7 +263,13 @@ public class JettyRunForkedMojo extends JettyRunMojo webApp.setCopyWebDir(false); webApp.setCopyWebInf(false); webApp.setGenerateQuickStart(true); - webApp.setQuickStartDir(target); + + if (!forkWebXml.getParentFile().exists()) + forkWebXml.getParentFile().mkdirs(); + if (!forkWebXml.exists()) + forkWebXml.createNewFile(); + + webApp.setQuickStartWebDescriptor(Resource.newResource(forkWebXml)); server.addWebApplication(webApp); diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java index 4b8a76bc7d..4ba0c8b88c 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.maven.plugin; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; @@ -125,7 +126,7 @@ public class JettyWebAppContext extends WebAppContext private boolean _isGenerateQuickStart; private PreconfigureDescriptorProcessor _preconfigProcessor; - private File _quickStartDir; + @@ -274,15 +275,7 @@ public class JettyWebAppContext extends WebAppContext return _isGenerateQuickStart; } - public void setQuickStartDir (File dir) - { - _quickStartDir = dir; - } - - public File getQuickStartDir() - { - return _quickStartDir; - } + @Override @@ -290,9 +283,14 @@ public class JettyWebAppContext extends WebAppContext { if (isGenerateQuickStart()) { - QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, (getQuickStartDir()==null?getTempDirectory():getQuickStartDir()), _preconfigProcessor.getXML()); - File f = generator.generateQuickStartWebXml(); - setQuickStartWebDescriptor(Resource.newResource(f)); + if (getQuickStartWebDescriptor() == null) + throw new IllegalStateException ("No location to generate quickstart descriptor"); + + QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, _preconfigProcessor.getXML()); + try (FileOutputStream fos = new FileOutputStream(getQuickStartWebDescriptor().getFile())) + { + generator.generateQuickStartWebXml(fos); + } } else super.startWebapp(); @@ -303,7 +301,9 @@ public class JettyWebAppContext extends WebAppContext public void doStart () throws Exception { //choose if this will be a quickstart or normal start - if (getQuickStartWebDescriptor() == null) + if (!isGenerateQuickStart() && getQuickStartWebDescriptor() != null) + setConfigurations(_quickStartConfigurations); + else { setConfigurations(_defaultConfigurations); if (isGenerateQuickStart()) @@ -312,8 +312,6 @@ public class JettyWebAppContext extends WebAppContext getMetaData().addDescriptorProcessor(_preconfigProcessor); } } - else - setConfigurations(_quickStartConfigurations); //inject configurations with config from maven plugin |