使用Maven+Eclipse+Tomcat小结

本文介绍如何在Eclipse+Tomcat环境下使用Maven进行依赖管理,包括配置私服、定义pom.xml文件、集成m2eclipse插件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发中,初次准备引入maven, 原本的开发环境是Eclipse+Tomcat。因为对maven的其他功能不是很熟悉,所以最初准备只使用它的依赖管理的功能,不影响现有的开发模式。

作如下配置:nexus配置完成,搭建了私服。使用外的的Tomcatweb容器。Eclipse 已经添加了maven 需要的插件m2eclipse等。

在编译的时候,有时候会说没有tools.jar,那么在installed jrejdk中加入jdk/lib/下的所有的包。再在启动eclipse时,加入一行启动参数: -vm"D:/javaDev/jdk1.5.0.8/bin",这样就保证了它是在jdk下运行,否则它会在Eclipse自带的jre下运行。 注意,vm与后面的路径之间不要有空格。

原工程不是maven推荐的结构。现在要使用maven, 只需要在原工程的根目录下新建一个pom.xml文件,因为我们这里不是采用的maven 的标准目录结构,所以,需要在注意pom.xml需要指定目录结构,以覆盖它的标准目录结构。

现在的目录结构如下:

需要把这个工程转化为maven 工程。按如下步骤:

在这个工程上 new->other->maven pom profile, 生成一个基本的 pom.xml 文件。 pom.xml 中添加如下的信息:

<build>

<directory>target</directory><!-- 这是打包的文件的路径。默认是该工程的根目录。 -->

<finalName>dwr2.0.5</finalName><!--生成的目标文件名 -->

<sourceDirectory>src</sourceDirectory><!-- 源文件名夹的名称。这里对应填写我们的src目录。 -->

<outputDirectory>WebRoot/WEB-INF/classes</outputDirectory><!--编译后的java文件输出的文件夹位置。 -->

<testSourceDirectory>test/java</testSourceDirectory><!-- 测试文件源文件夹。这里与src文件夹的定义是相同的。在打包时有区别,打包时,test文件夹下的源文件和资源文件都不被打到war包里面。 -->

<resources><!-- 存放资源文件。这里的文件不需要编译,在部署打包时,直接复制这里的文件 -->

<resource>

<directory>src/resources</directory><!-- -->

<excludes>

<exclude>**/*.java</exclude><!--例外的文件类型。如果这里存放有.java文件,则仍然需要编译。 -->

</excludes>

</resource>

</resources>

<testResources><!-- 与上面resource的意义相同。 -->

<testResource>

<directory>test/resources</directory>

<excludes>

<exclude>**/*.java</exclude>

</excludes>

</testResource>

</testResources>

<scriptSourceDirectory><!--脚本源文件。这里的文件也不用经过编译。通常存放css,javascript,img等文件内容。 -->

</scriptSourceDirectory>

</build>

定义使用的插件,我们加入 tomcat 的插件,方便开发调试及部署。其中, tomcat 插件可以支持外部的 tomcat 的开发和部署。另外,开发 web 应用,还需要加入 war 的插件。这些 plugins 也在 build 节点中。

<plugins><!-- -->

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>tomcat-maven-plugin</artifactId>

<version>1.0-beta-1</version>

<configuration>

<url>http://localhost/manager</url>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-war-plugin</artifactId>

<version>2.0.2</version>

<configuration>

<warSourceDirectory>WebRoot</warSourceDirectory>

<dependentWarExcludes>

**/jdbc.properties,**/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**

</dependentWarExcludes>

</configuration>

</plugin>

</plugins>

定义使用到的 jar 文件:

<!-- 定义使用到的jar文件。按照我们在nexus中查到的dependency节点抄下来即可(指定了包,类库名,版本号)。 -->

<dependencies>

<dependency>

<groupId>net.bingo</groupId>

<artifactId>dwr</artifactId>

<version>2.0.5</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.0.4</version>

</dependency>

</dependencies>

完整的 pom.xml 文件现在如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>dwr2.0.5</groupId>

<artifactId>dwr2.0.5</artifactId>

<name>dwr2.0.5</name>

<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<url>@mailto:guorabbit@126.com</url>

<build>

<directory>target</directory><!-- 这是打包的文件的路径。默认是该工程的根目录。 -->

<finalName>dwr2.0.5</finalName><!--生成的目标文件名 -->

<sourceDirectory>src</sourceDirectory><!-- 源文件名夹的名称。这里对应填写我们的src目录。 -->

<outputDirectory>WebRoot/WEB-INF/classes</outputDirectory><!--编译后的java文件输出的文件夹位置。 -->

<testSourceDirectory>test/java</testSourceDirectory><!-- 测试文件源文件夹。这里与src文件夹的定义是相同的。在打包时有区别,打包时,test文件夹下的源文件和资源文件都不被打到war包里面。 -->

<resources><!-- 存放资源文件。这里的文件不需要编译,在部署打包时,直接复制这里的文件 -->

<resource>

<directory>src/resources</directory><!-- -->

<excludes>

<exclude>**/*.java</exclude><!--例外的文件类型。如果这里存放有.java文件,则仍然需要编译。 -->

</excludes>

</resource>

</resources>

<testResources><!-- 与上面resource的意义相同。 -->

<testResource>

<directory>test/resources</directory>

<excludes>

<exclude>**/*.java</exclude>

</excludes>

</testResource>

</testResources>

<scriptSourceDirectory><!--脚本源文件。这里的文件也不用经过编译。通常存放css,javascript,img等文件内容。 -->

</scriptSourceDirectory>

<plugins><!-- tomcat 的插件 -->

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>tomcat-maven-plugin</artifactId>

<version>1.0-beta-1</version>

<configuration>

<url>http://localhost/manager</url>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-war-plugin</artifactId>

<version>2.0.2</version>

<configuration>

<warSourceDirectory>WebRoot</warSourceDirectory>

<dependentWarExcludes>

**/jdbc.properties,**/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**

</dependentWarExcludes>

</configuration>

</plugin>

</plugins>

</build>

<!-- 定义使用到的jar文件。按照我们在nexus中查到的dependency节点抄下来即可(指定了包,类库名,版本号)。 -->

<dependencies>

<dependency>

<groupId>net.bingo</groupId>

<artifactId>dwr</artifactId>

<version>2.0.5</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.0.4</version>

</dependency>

</dependencies>

<!--这是生成站点

<distributionManagement>

<site>

<id>website</id>

<url>http://localhost/www/docs/project/</url>

</site>

</distributionManagement>

-->

</project>

.m2/setting.xml 中的文件设置如下:

<?xml version="1.0"?>

<settings xmlns="http://maven.apache.org/settings/1.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<servers>

<!-- 前两个server是设置的上传到私服的用户名和密码。分别可以上传release 版本和snapshot版本的。后面的是部署到tomcat下的,提供tomcat的manager角色的用户名和密码,注意与你的tomcat下conf/tomcat-users.xml中的设置保持一致。因为通过的是UI的方式部署,所以需要先启动tomcat。 -->

<server>

<id>nexus-releases</id>

<username>admin</username>

<password>admin123</password>

</server>

<server>

<id>nexus-snapshots</id>

<username>admin</username>

<password>admin123</password>

</server>

<server>

<id>tomcat-server</id>

<username>admin</username>

<password></password>

</server>

</servers>

<mirrors>

<mirror>

<id>Nexus</id>

<name>Nexus Public Mirror</name>

<url>http://localhost:8081/nexus/content/groups/public</url>

<mirrorOf>central</mirrorOf>

</mirror>

<mirror>

<!--This is used to direct the public snapshots repo in the

profile below over to a different nexus group -->

<id>nexus-public-snapshots</id>

<mirrorOf>public-snapshots</mirrorOf>

<url>

http://localhost:8081/nexus/content/groups/public-snapshots

</url>

</mirror>

<mirror>

<!--This sends everything else to /public -->

<id>nexus</id>

<mirrorOf>*</mirrorOf>

<url>http://localhost:8081/nexus/content/groups/public</url>

</mirror>

</mirrors>

<profiles>

<profile>

<id>development</id>

<repositories>

<repository>

<id>central</id>

<url>http://central</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>false</enabled>

</snapshots>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>central</id>

<url>http://central</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>false</enabled>

</snapshots>

</pluginRepository>

</pluginRepositories>

</profile>

<profile>

<!--this profile will allow snapshots to be searched when activated-->

<id>public-snapshots</id>

<repositories>

<repository>

<id>public-snapshots</id>

<url>http://public-snapshots</url>

<releases>

<enabled>false</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>public-snapshots</id>

<url>http://public-snapshots</url>

<releases>

<enabled>false</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</pluginRepository>

</pluginRepositories>

</profile>

</profiles>

<activeProfiles>

<activeProfile>development</activeProfile>

</activeProfiles>

</settings>

注意所有的localhost 都需要改为实际nexus 所安装在的文件服务器的IP地址。nexus的默认端口是8081,可不修改。

现在就可以像以前一样使用 tomcat +Eclipse 了。我们以前使用 tomcat 的部署方式不受任何影响。断点调试等问题,因为是使用的外部的 Eclipse Tomcat ,与 maven 并无关系,所以这些都不受影响。唯一的区别就是,我们现在的 jar 包,可以通过在 pom.xml 定义了以后,执行 install 命令进行安装了。开发时和暴露式部署后,在 WEB-INF/lib 的目录是空的,我们的 jar 包只是以外部类库引用的方式在使用,同时在 SVN 提交代码时,也不再需要关心 jar 文件了。只有在打 war 包的时候, WEB-INF/lib 的目录会包含用到的 jar 文件。 至此,我们的初步的目标 -- 使用 maven 管理依赖的这个目标已经达到。而且,因为我们已经有了自己的目录结构,另外使用 maven 的其他功能也已经很容易实现了。看了许多天,终于成功使用,作一小结,以兹纪念。 guorabbit, 2009-03-27.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值