Running web app in both Jetty and Tomcat

后端 未结 4 1741
耶瑟儿~
耶瑟儿~ 2021-02-14 18:19

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 di

相关标签:
4条回答
  • 2021-02-14 18:52

    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.

    0 讨论(0)
  • 2021-02-14 18:54

    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)

    0 讨论(0)
  • 2021-02-14 19:01

    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.

    0 讨论(0)
  • 2021-02-14 19:15
    <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...

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