在Jetty和Tomcat中运行Web应用程序

我有一个网络应用程序,我在Tomcat上运行生产。它使用MySQL连接器,但它没有与war捆绑在一起,而是包含在Tomcat的公共lib目录下,因此我可以通过JNDI访问数据源。 我想用Jetty(开发时)做一些类似的东西,更确切地说是Jetty + Maven。有没有办法让我在通过Maven运行Jetty时在类路径中包含mysql-connector jar(即没有将它捆绑在war文件中)? 另外我应该注意,我正在使用Maven进行构建过程,并将mysql-connector指定为“提供”范围。     
已邀请:
添加到上一个答案: 你必须在maven配置依赖中添加到你的jetty插件:
<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>
然后,您可以在主项目依赖项中使用提供的范围。我现在就做了,它有效。谢谢你的提问(以及Nishant)     
不直接回答你的问题,但由于我喜欢webapps中的可移植性,我的战争将包含连接器jar和连接池(例如超级duper c3p0)。这意味着容器将不再为我管理数据库连接,也不会使用JNDI来描述连接属性。但是webapp现在是100%便携式的,可以预测tomcat,jetty,resin,jboss等。     
<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>
它是一个jetty-env.xml,它指向.properties文件,其中包含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>
这是一个弹簧配置(我也使用弹簧) 然后,我打电话给mvn jetty:run,它工作得很好......     
也许您可以尝试使用Maven .war覆盖来实现此目的,但我不知道它们是否与其他依赖项一起使用。 所以基本上你的项目就是
parent
|---- original-war
|---- new-war
你的原战项目有mysql依赖为
<scope>provided</scope>
但新战模块只是一个有
<packaging>war</packaging>
的pom,取决于原始战争(对于覆盖)具有与编译范围的mysql依赖,并运行jetty插件(将jetty插件从原始战争模块中移除)。如果这样做,那么你将不得不处理在一个模块中进行开发的轻微不便,但无论你在另一个模块中进行maven测试。     

要回复问题请先登录注册