public static void main(String[] args) throws Exception { String userDir = System.getProperty("user.dir") + File.separator + "server.tomcat"; String webappDirLocation = userDir + File.separator +"src/main/webapp/"; Tomcat tomcat = new Tomcat(); String webPort = System.getenv("PORT"); if (webPort == null || webPort.isEmpty()) { webPort = "8080"; } tomcat.setPort(Integer.valueOf(webPort)); System.out.println("configuring app with basedir: " + new File(webappDirLocation).getAbsolutePath()); StandardContext standardContext = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); File additionWebInfClasses = new File(userDir + File.separator + "build/classes"); WebResourceRoot resourceRoot = new StandardRoot(standardContext); resourceRoot.addPreResources(new DirResourceSet(resourceRoot, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); standardContext.setResources(resourceRoot); tomcat.start(); tomcat.getServer().await(); }
@Test public void embeddedJarWithoutAppResources() throws LifecycleException { ContextMock contextMock = new ContextMock(); File file = new File(TARGET + File.separator + TEST_CLASSES + File.separator + "test.jar"); JarWarResourceSet jarWarResourceSet = new JarWarResourceSet(contextMock.getWebResourceRoot(), "/", file.getAbsolutePath(), INTERNAL_JAR, METAINF_RESOURCES); jarWarResourceSet.init(); DirResourceSet dirResourceSet = new DirResourceSet(contextMock.getWebResourceRoot(), TEST, TEST, TEST); contextMock.init(jarWarResourceSet, dirResourceSet); callApplicationEvent(contextMock); assertThat(contextMock.getWebResourceRoot().getCreateWebResourceSetCalls()) .isEqualTo(1); }
@Test public void embeddedJarWithoutAppResources2() throws LifecycleException { ContextMock contextMock = new ContextMock(); File file = new File(TARGET + File.separator + TEST_CLASSES + File.separator + "test.jar"); JarWarResourceSet jarWarResourceSet = new JarWarResourceSet(contextMock.getWebResourceRoot(), "/", file.getAbsolutePath(), INTERNAL_JAR, METAINF_RESOURCES); jarWarResourceSet.init(); DirResourceSet dirResourceSet = new DirResourceSet(contextMock.getWebResourceRoot(), TEST, TEST, TEST); contextMock.init(dirResourceSet, jarWarResourceSet); callApplicationEvent(contextMock); assertThat(contextMock.getWebResourceRoot().getCreateWebResourceSetCalls()) .isEqualTo(1); }
@Test public void embeddedWarWithoutAppResources() throws LifecycleException { ContextMock contextMock = new ContextMock(); File file = new File(TARGET + File.separator + TEST_CLASSES + File.separator + "test.war"); JarWarResourceSet jarWarResourceSet = new JarWarResourceSet(contextMock.getWebResourceRoot(), "/", file.getAbsolutePath(), INTERNAL_JAR, METAINF_RESOURCES); jarWarResourceSet.init(); DirResourceSet dirResourceSet = new DirResourceSet(contextMock.getWebResourceRoot(), TEST, TEST, TEST); contextMock.init(jarWarResourceSet, dirResourceSet); callApplicationEvent(contextMock); assertThat(contextMock.getWebResourceRoot().getCreateWebResourceSetCalls()) .isEqualTo(0); }
/** * Sets up the test environment, generates data to upload, starts a * Tomcat instance which will receive the client requests. * @throws Exception If an error occurred with the servlets */ @BeforeClass public static void setUpClass() throws Exception { server = new Tomcat(); Path base = Paths.get("build/tomcat"); Files.createDirectories(base); server.setPort(8100); server.setBaseDir("build/tomcat"); server.getHost().setAppBase("build/tomcat"); server.getHost().setAutoDeploy(true); server.getHost().setDeployOnStartup(true); StandardContext context = (StandardContext) server.addWebapp("/", base.toAbsolutePath().toString()); Path additionWebInfClasses = Paths.get("build/classes"); WebResourceRoot resources = new StandardRoot(context); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.toAbsolutePath().toString(), "/")); context.setResources(resources); context.getJarScanner().setJarScanFilter((jarScanType, jarName) -> false); server.start(); }
public static void main(String[] args) { Connector connector = new Connector(); connector.setPort(80); Tomcat tomcat = new Tomcat(); tomcat.setPort(80); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); try { Context ctx = tomcat.addWebapp("", Conf.getAbsolutePath("../src/main/webapp")); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", Conf.getAbsolutePath("classes"), "/")); ctx.setResources(resources); Server server = tomcat.getServer(); server.start(); server.setPort(8005); server.await(); server.stop(); } catch (ServletException | LifecycleException e) { Log.e(e); } }
@Test public void testingResources() throws LifecycleException { ContextMock contextMock = new ContextMock(); DirResourceSet dirResourceSet = new DirResourceSet(contextMock.getWebResourceRoot(), TEST, TEST, TEST); contextMock.init(dirResourceSet); callApplicationEvent(contextMock); assertThat(contextMock.getWebResourceRoot().getCreateWebResourceSetCalls()) .isEqualTo(0); }
public static void main(String[] args) throws Exception { String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); //The port that we should run on can be set into an environment variable //Look for that variable and default to 8080 if it isn't there. String webPort = System.getenv("PORT"); if(webPort == null || webPort.isEmpty()) { webPort = "8080"; } tomcat.setPort(Integer.valueOf(webPort)); StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath()); // Declare an alternative location for your "WEB-INF/classes" dir // Servlet 3.0 annotation will work File additionWebInfClasses = new File("target/classes"); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); ctx.setResources(resources); tomcat.start(); tomcat.getServer().await(); }
public static void main(String[] args) { Connector connector = new Connector(); connector.setPort(80); Tomcat tomcat = new Tomcat(); tomcat.setPort(80); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); try { Context ctx = tomcat.addWebapp("", Conf.getAbsolutePath("../src/main/webapp")); // Ensure to Load All Classes in the Same Class Loader ctx.setLoader(new WebappLoader(Startup.class.getClassLoader()) { @Override public ClassLoader getClassLoader() { return Startup.class.getClassLoader(); } }); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", Conf.getAbsolutePath("classes"), "/")); ctx.setResources(resources); Server server = tomcat.getServer(); server.start(); server.setPort(8005); server.await(); server.stop(); } catch (ServletException | LifecycleException e) { Log.e(e); } }