Created
October 4, 2014 15:11
-
-
Save eeichinger/5dcde72c883edf406908 to your computer and use it in GitHub Desktop.
Custom Embedded Jetty Runner for easier debugging - run xml free webapps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
based on | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-runner</artifactId> | |
<version>9.2.3.v20140905</version> | |
</dependency> | |
*/ | |
import org.eclipse.jetty.annotations.AnnotationConfiguration; | |
import org.eclipse.jetty.plus.webapp.EnvConfiguration; | |
import org.eclipse.jetty.plus.webapp.PlusConfiguration; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.ServerConnector; | |
import org.eclipse.jetty.server.handler.HandlerCollection; | |
import org.eclipse.jetty.server.handler.RequestLogHandler; | |
import org.eclipse.jetty.util.ConcurrentHashSet; | |
import org.eclipse.jetty.util.resource.Resource; | |
import org.eclipse.jetty.webapp.*; | |
import org.springframework.web.WebApplicationInitializer; | |
public class RunJetty { | |
public static void main(String[] args) throws Exception { | |
Server server = new Server(); | |
ServerConnector scc = new ServerConnector( server ); | |
scc.setPort( Integer.parseInt( System.getProperty( "jetty.port", "8080" ) ) ); | |
server.addConnector( scc ); | |
WebAppContext webApp = new WebAppContext(); | |
webApp.setServer( server ); | |
webApp.setContextPath( "/" ); | |
webApp.setResourceBase( "src/main/webapp" ); | |
/* | |
webApp.setConfigurations( new Configuration[]{ | |
new WebInfConfiguration(), | |
// new WebXmlConfiguration(), | |
new MetaInfConfiguration(), | |
new FragmentConfiguration(), | |
new EnvConfiguration(), | |
new PlusConfiguration(), | |
new AnnotationConfiguration() { | |
@Override | |
public void preConfigure(WebAppContext context) throws Exception { | |
// when excluding WebXmlConfiguration, we need to explicitly load & apply webdefault.xml | |
String defaultDescriptor = context.getDefaultsDescriptor(); | |
if (defaultDescriptor != null && defaultDescriptor.length() > 0) { | |
Resource dftResource = Resource.newSystemResource(defaultDescriptor); | |
if (dftResource != null) { | |
context.getMetaData().setDefaults( dftResource ); | |
} | |
} | |
ClassInheritanceMap cimap = new ClassInheritanceMap(); | |
cimap.put( WebApplicationInitializer.class.getName(), new ConcurrentHashSet<String>() {{ | |
// add( MyServicesWebAppInitializer.class.getName() ); | |
}} ); | |
context.setAttribute( CLASS_INHERITANCE_MAP, cimap ); | |
} | |
}, | |
new JettyWebXmlConfiguration(), | |
} ); | |
*/ | |
/* | |
ResourceHandler resource_handler = new ResourceHandler(); | |
resource_handler.setDirectoriesListed(true); | |
resource_handler.setWelcomeFiles(new String[]{ "index.html" }); | |
resource_handler.setResourceBase("src/main/webapp"); | |
*/ | |
HandlerCollection handlers = new HandlerCollection(); | |
// handlers.addHandler( new DefaultHandler() ); | |
handlers.addHandler( new RequestLogHandler() ); | |
handlers.addHandler( webApp ); | |
server.setHandler( handlers ); | |
try { | |
System.out.println( ">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP" ); | |
System.out.println( String.format( ">>> open http://localhost:%s/", scc.getPort() ) ); | |
server.start(); | |
while (System.in.available() == 0) { | |
Thread.sleep( 5000 ); | |
} | |
server.stop(); | |
server.join(); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
System.exit( 100 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment