Skip to content

Instantly share code, notes, and snippets.

@slamdev
Created March 22, 2014 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slamdev/9707532 to your computer and use it in GitHub Desktop.
Save slamdev/9707532 to your computer and use it in GitHub Desktop.
Embedded glassfish runner
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Properties;
import java.util.logging.LogManager;
import org.glassfish.embeddable.Deployer;
import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishProperties;
import org.glassfish.embeddable.GlassFishRuntime;
import org.glassfish.embeddable.archive.ScatteredArchive;
public class DevRun {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("handlers", "org.slf4j.bridge.SLF4JBridgeHandler");
ByteArrayOutputStream output = new ByteArrayOutputStream();
properties.store(output, null);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
LogManager.getLogManager().readConfiguration(input);
try {
GlassFishProperties glassfishProperties = new GlassFishProperties();
glassfishProperties.setPort("http-listener", 8080);
glassfishProperties.setPort("https-listener", 8181);
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(glassfishProperties);
glassfish.start();
Deployer deployer = glassfish.getDeployer();
ScatteredArchive archive = new ScatteredArchive(DevRun.class.getSimpleName(), ScatteredArchive.Type.WAR,
new File("src/main/webapp"));
archive.addClassPath(new File("target", "classes"));
archive.addMetadata(new File("src/main/webapp/WEB-INF", "web.xml"));
deployer.deploy(archive.toURI(), "--contextroot=/");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment