Skip to content

Instantly share code, notes, and snippets.

@AlBaker
Created May 23, 2012 00:54
Show Gist options
  • Save AlBaker/2772611 to your computer and use it in GitHub Desktop.
Save AlBaker/2772611 to your computer and use it in GitHub Desktop.
Executable War Main
// Relevant Gradle snippets
apply plugin: 'war'
configurations {
executableWarDeps
}
dependencies {
// your stuff
executableWarDeps 'org.mortbay.jetty:jetty:6.1.4'
executableWarDeps 'org.codehaus.groovy:groovy-all:1.8.6'
}
war {
from {configurations.executableWarDeps.collect {
it.isDirectory() ? it : project.zipTree(it)
}
}
from "$buildDir/classes/main"
manifest { attributes 'Main-Class': 'mypackage.ExecutableMain' }
}
package mypackage;
import java.net.URL;
import java.security.ProtectionDomain
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
/**
* Simple executable main, with just a dash of Groovy
*
*/
class ExecutableMain {
static main(args) {
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(8888);
connector.setHost("127.0.0.1");
server.addConnector(connector);
server.setStopAtShutdown(true);
WebAppContext context = new WebAppContext();
context.setServer(server);
context.setContextPath("/");
ProtectionDomain protectionDomain = DataImporterMain.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
context.setWar(location.toExternalForm());
println "WAR URL: ${location.toExternalForm()}"
server.addHandler(context);
try {
server.start();
System.in.read();
server.stop();
server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment