Created
February 25, 2015 07:38
-
-
Save linusfoldemo/8fa87969d4a54010c15f to your computer and use it in GitHub Desktop.
Start embedded tomcat
This file contains 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
public static void start(final Environment environment, final int port) { | |
Environment.set(environment); | |
v | |
final Tomcat tomcat = new Tomcat(); | |
tomcat.setPort(port); | |
final File workDir = Files.createTempDir(); | |
final Context context = tomcat.addContext("", workDir.getAbsolutePath()); | |
context.addServletContainerInitializer(new SpringServletContainerInitializer(), | |
ImmutableSet.<Class<?>>of(Application.class)); | |
try { | |
tomcat.start(); | |
} catch (LifecycleException e) { | |
throw new IllegalStateException("Unable to start embedded tomcat", e); | |
} | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
public void run() { | |
try { | |
tomcat.stop(); | |
FileUtils.deleteDirectory(workDir); | |
} catch (IOException | LifecycleException e) { | |
throw new IllegalStateException("Unable to stop tomcat", e); | |
} | |
} | |
}); | |
tomcat.getServer().await(); | |
} | |
public static class RunLocal { | |
public static void main(String[] args) { | |
start(Environment.local, 8080); | |
} | |
} | |
public static class RunDev { | |
public static void main(String[] args) { | |
start(Environment.dev, 8081); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment