Maven的用途之一是服务于构建,它是一个异常强大的构建工具,能够帮我们自动化构建过程,从清理、编译、测试到生成报告,再到打包和部署。我们不需要也不应该一遍又一遍地输入命令,一次又一次地点击鼠标,我们要做的是使用Maven配置好项目,然后输入简单的命令(如mvn clean install),Maven会帮我们处理那些烦琐的任务。
Maven是跨平台的,无论是在Windows上,还是在Linux或者Mac上,都可以使用同样的命令。
Java不仅是一门编程语言,还是一个平台,通过JRuby和Jython,我们可以在Java平台上编写和运行Ruby和Python程序。我们也应该认识到,Maven不仅是构建工具,还是一个依赖管理工具和项目信息管理工具。
1.新建maven工程,将Java web项目中对应的代码放在maven工程相应目录下。
参考文章:http://zk1878.iteye.com/blog/1222330
http://www.cnblogs.com/syxchina/p/3526770.html
2.通过在项目上右键->转为maven项目,填写信息自动创建pom.xml
3. 配置class输出目录和项目jar包依赖
1)把项目的src目录配置/WebRoot/WEB-INF/classes目录下
2)把项目的lib目录jar添加到classpath
3)对于无法再mvnrepository中找到的jar包,需要手工将相应jar包加到本地maven库中:进入需要添加的jar包(这里是ibatis.jar)的目录,执行命令
`mvn install:install-file -DgroupId=com.ibatis -DartifactId=ibatis -Dversion=1.0 -Dpackaging=jar -Dfile=ibatis.jar
-DgeneratePom=true`
即可,并在pom.xml中添加依赖
<dependency>
<groupId>com.ibatis</groupId>
<artifactId>ibatis</artifactId>
<version>1.0</version>
</dependency>
4. 添加pom jetty插件和配置
(可参考文章:http://www.micmiu.com/software/build/maven-web-eclipse-deploy/
)
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Maven Web</groupId>
<artifactId>Maven Web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.15</version>
<configuration>
<contextPath>/</contextPath>
<webAppSourceDirectory>WebRoot</webAppSourceDirectory>
<scanIntervalSeconds>2</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9090</stopPort>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>WebRoot/WEB-INF</directory>
<includes><include>classes</include></includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</build>
</project>