Running web app in both Jetty and Tomcat

2019-03-26 05:00发布

问题:

I have a web app which in production I run on Tomcat. It uses the MySQL connector, however it is not bundled up with the war, rather it is included under Tomcat's common lib directory, so that I can access the data source through JNDI.

I would like to do something similar with Jetty (while developing), and more precisely Jetty + Maven. Is there a way for me to include the mysql-connector jar in the classpath when running Jetty through Maven (i.e. not have it bundled in the war file)?

Also I should note that I am using Maven for my build process and have the mysql-connector specified as "provided" scope.

回答1:

Additinally to previous answer: you have to add to your jetty plugin in maven config dependency:

<plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty.version}</version>
            <configuration>
                <stopKey>blah-blah-blah</stopKey>
                <stopPort>9966</stopPort>
                <webAppConfig>
                    <contextPath>/</contextPath>
                </webAppConfig>
                <jettyEnvXml>${basedir}/src/jetty-env.xml</jettyEnvXml>
            </configuration>
            <dependencies>              
                <dependency>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>8.4-701.jdbc4</version>
                </dependency>
            </dependencies>
        </plugin>

And then you can use provided scope at main project dependencies. I did it right now, and it works. Thank you for your question (and Nishant too)



回答2:

Does not directly answer your question but since I love portability in webapps my war will contain the connector jar and a connection pool (e.g the super duper c3p0). That means that the container will not manage the database connection for me anymore nor will I use JNDI to describe the connection properties. But the webapp is now 100% portable and predictable on tomcat, jetty, resin, jboss etc.



回答3:

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New class="org.mortbay.jetty.plus.naming.Resource">
    <Arg>hd-props</Arg>
    <Arg>
        <New class="java.util.Properties">
            <Call name="load">
                <Arg>
                    <New class="java.io.FileReader">
                        <Arg>cfg/dev-local.properties</Arg>
                    </New>
                </Arg>
            </Call>
        </New>
    </Arg>
</New>

It is a jetty-env.xml, which points to .properties file, which contains all the connect params to DB.

    <bean id="jndi" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/hd-props"/>
    </bean>
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="jndi"/>
    </bean>

It is a spring config (i'm using spring too)

And then, I call mvn jetty:run, and it works fine...



回答4:

Perhaps you could try using Maven .war overlays for this purpose, though I don't know if they work with other dependencies.

So basically your project would be

parent
|---- original-war
|---- new-war

Where your original-war project has the mysql dependency as <scope>provided</scope> but the the new-war module is just a pom that has a <packaging>war</packaging>, depends on the original war (for the overlay) has the mysql dependency with the compile scope, and runs the jetty plugin (leave the jetty plugin out of the original-war module). If this works, then you'll have to deal with the minor inconvenience of doing your development in one module but whatever testing you are doing in maven within another module.