您的位置:首页 > 运维架构 > Tomcat

使用Maven+Eclipse+Tomcat小结

2009-03-28 00:50 423 查看
 

在开发中,初次准备引入maven,
原本的开发环境是Eclipse+Tomcat。因为对maven的其他功能不是很熟悉,所以最初准备只使用它的依赖管理的功能,不影响现有的开发模式。
作如下配置:nexus配置完成,搭建了私服。使用外的的Tomcat作web容器。Eclipse 已经添加了maven 需要的插件m2eclipse等。
在编译的时候,有时候会说没有tools.jar,那么在installed jre的jdk中加入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.
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐