Eclipse + Tomcat - Serve webapp directly from workspace

后端 未结 9 641
旧时难觅i
旧时难觅i 2021-02-04 10:13

What is the ideal way to configure Tomcat to serve directly from my project\'s directory inside of my workspace? (related)

I want my static web resources

相关标签:
9条回答
  • 2021-02-04 10:44

    Use grizzly webserver. It's completely implemented in java, hence you have full plattform independency and you can start it directly from your workspace without configuring any external programs. It's easy to deploy resources statically. You just have to derive javax.ws.rs.core.Application and add your resources as in the example.

    import java.util.HashSet;
    import java.util.Set;
    
    import javax.ws.rs.core.Application;
    
    /**
     * Application class that contains resources for the RESTful web service.
     *
     */
    public class MyApplication extends Application {
        public Set<Class<?>> getClasses() {
            Set<Class<?>> s = new HashSet<Class<?>>();
            s.add(com.rest.test.SomeClass.class);
            return s;
        }
    }
    

    This is needed to configure the servlet adapter. It's also possible to add resources dynamically. But I can't tell you how fast the updating of the resources is with the dynamic approach. Anyway there is enough documentation available online.

    private static GrizzlyWebServer getServer(int port,
        String webResourcesPath, String contextPath) {
        GrizzlyWebServer gws = new GrizzlyWebServer(port, webResourcesPath);
    
        ServletAdapter sa = new ServletAdapter();
    
        /* here they are added statically */
        sa.addInitParameter("javax.ws.rs.Application", "com.rest.MyApplication");
    
    
        sa.setContextPath(contextPath);
        sa.setServletInstance(new ServletContainer());
        sa.setProperty("load-on-startup", 1);
        gws.addGrizzlyAdapter(sa, new String[] { contextPath });
    
        return gws;
    

    }

    0 讨论(0)
  • 2021-02-04 10:48

    By default it shares only the apps located in $CATALINA_HOME/webapps. You can change it in the $CATALINA_BASE/conf/server.xml in the <Host />.

    Check the documentation for attribute appBase.

    Hope it helped.

    0 讨论(0)
  • 2021-02-04 10:52

    Normally webapplications are hosted under webapps directory, but its also possible to configure an external directory as a webapplication host directory in tomcat. You can simply set your eclipse workspace project output directory as an application base. This can be done either by using Tomcat's manager application to deploy an application from an external directory or by simply editing server.xml (which is under conf directory) to define your application like below:

    ....
    
    <Context docBase="D:\your\eclipse\workspace\project\WebContent" path="/projectbaseurl"   reloadable="true"/>
    
    </Host>
    </Engine>
    </Service>
    </Server>
    
    0 讨论(0)
  • 2021-02-04 10:54

    I suggest using sbt together with its web plugin.

    The key feature of sbt is the continuous integration mode: This basically means, sbt fires a command, if a file changes.

    So normal sbt commands (related with the web plugin) are

    container:start
    container:stop
    container:reload / ... container:reload

    but using it in the continuous integration mode (a command starting with ~)

    ~;container:start; container:reload /

    it will automatically reload a web application when source code or any other files have changed.

    So I just save servlets, Java sources, Scala sources whatever, and hit reload in the browser, and that really saves a lot of time.

    This is the Grails feeling, but with Java as the main programming language.

    Additional information

    • I have sbt running in a terminal (I am not aware if there is a plugin for Eclipse to start it from within Eclipse)
    • sbt can compile both Java and Scala projects out of the box.
    • The web plugin is using jetty (I am not aware if it's possible to embed tomcat as well)
    • There is no need to know Scala to configure sbt, but you should read the tutorial.
    • This solution is not tied to the IDE; so it works without an IDE as well (just from the command line).
    0 讨论(0)
  • 2021-02-04 10:57

    I am pretty sure that Maven can do this kind of stuff. Maybe there are even Archetypes for Tomcat which have this kind of behaviour already configured.

    I am using a Maven Archetype together with JBoss AS. When I change my .css or .xhtml file or any other static resources and save it in Eclipse, the resources are immediatally synchronzied with my deployment.

    Edit:

    Here is a quick tutorial for my solution:

    What I am using:

    • JBoss AS 7.1.1
    • Eclipse Juno for Java EE developers

    Older or newer versions should also do the work.

    1. Install JBoss Tools Plug-In over the Eclipse Marketplace in Eclipse IDE
      This will install several Plug-Ins to your IDE, like the JBoss AS Server Connector or Maven.
    2. Add JBoss Repositories to your Maven configuration
      Window -> Preferences -> Maven -> User Settings -> open file
      Add the repositories from JBoss Maven Getting started
    3. Add a JBoss Server Runtime
      Window -> Preferences -> Servers -> Runtime Environments
      Follow the Wizard, pretty standard
    4. Add a Serverinstance from the Eclipse Servers Tab
    5. Create a new Maven Project
      Take care that the checkbox Create a simple project(skip archetype) is NOT checked
      Choose the Archetype jboss-javaee6-webapp-blank-archetype 7.1.3.CR7

    Archetype

    Now your Project is ready to go:)

    0 讨论(0)
  • 2021-02-04 11:00

    I've never really liked the WTP plugin that gets bundled with Eclipse.

    In the past I've had a lot of success with the Sysdeo Tomcat plugin for Eclipse.

    It uses the compiled classes that Eclipse builds for you, so when you make an interface-compatible change (like changing some stuff inside a method), this gets deployed immediately without requiring a restart. Changing method signatures or adding new methods within a class do require a restart, but since there's no lengthy WAR-building step, the whole build/deploy cycle is reduced anyway.

    Additionally, the Sysdeo plugin uses the static assets from your workspace, so no need to copy or deploy these. Just make a change, refresh your browser, and you see the change right away.

    Unfortunately it looks like development of the plugin ground to a halt a couple of years ago. The latest supported version of Eclipse, according to the matrix on their website, is 3.6. According to this page however, the plugin still works with Eclipse 4.2 (Juno).

    Hope this helps. Using Sysdeo is really a much nicer experience than WTP!

    0 讨论(0)
提交回复
热议问题