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

时间:2011-01-27 07:30:46

标签: java tomcat maven jetty jndi

我有一个网络应用程序,我在Tomcat上运行。它使用MySQL连接器,但它没有与war捆绑在一起,而是包含在Tomcat的公共lib目录下,因此我可以通过JNDI访问数据源。

我想做一些与Jetty(开发时)类似的东西,更确切地说是Jetty + Maven。有没有办法让我在通过Maven运行Jetty时在类路径中包含mysql-connector jar(即没有将它捆绑在war文件中)?

另外我应该注意,我正在使用Maven进行构建过程,并将mysql-connector指定为“提供”范围。

4 个答案:

答案 0 :(得分:4)

另外回答以前的答案: 你必须在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)

答案 1 :(得分:2)

不直接回答你的问题,但由于我喜欢webapps中的可移植性,我的战争将包含连接器jar和连接池(例如超级duper c3p0)。这意味着容器将不再为我管理数据库连接,也不会使用JNDI来描述连接属性。但是webapp现在可以100%移植,可以预测tomcat,jetty,resin,jboss等。

答案 2 :(得分:0)

<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,它工作正常......

答案 3 :(得分:0)

也许您可以尝试使用Maven .war overlays来实现此目的,但我不知道它们是否与其他依赖项一起使用。

所以基本上你的项目将是

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

你的原战项目的mysql依赖关系为<scope>provided</scope>,但是新战模块只是一个有<packaging>war</packaging>的pom,取决于原始战争(对于叠加)与编译范围的mysql依赖关系,并运行jetty插件(将jetty插件保留在原始模块之外)。如果这样做,那么你将不得不处理在一个模块中进行开发的轻微不便,但无论你在另一个模块中的maven中做什么测试。