一、安装
1、下载Maven包到指定目录如:C:/tools/maven-3.0.5
2、新建环境变量M2_HOME:其值即maven根目录(C:/tools/maven-3.0.5)
3、添加到Path变量中%M2_HOME%\bin
4、运行mvn -version检测是否安装成功
5、搭建本地资源库
a、新建环境变量M2_REPO=D:/m2/repository,并添加到path中
b、个性maven/conf/setting.xml,修改localRepository节点路径修改为D:/m2/repository
6、eclipse中window/preferences/java/build path/classpath variables下添加M2_REPO变量,值为D:/m2/repository
二、基本使用
pom.xml中增加servlet容器的插件
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.6</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> tomcat插件 jetty插件 编译插件的配置
三、安装第三方包
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=c:/driver/ojdbc14.jar
这样就可以在pom中依赖引用了:
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.3.0</version> </dependency>
四、布署到tomcat
tomcat配置有管理权限的用户:conf\tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="manager"/> <user username="marshal" password="password" roles="manager"/> </tomcat-users>
在pom文件的tomcat插件中添加:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <configuration> <url>http://localhost:8080/manager</url> <server>myserver</server> <path>/mycontext</path> </configuration> </plugin>
在.m2/settings.xml文件中增加:
<settings 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/settings-1.0.0.xsd"> <servers> <server> <id>myserver</id> <username>marshal</username> <password>password</password> </server> </servers> </settings>
运行打包部署,在maven项目目录下:
mvn tomcat:deploy
然后访问:http://localhost:8080/mycontext/ 即可。
撤销部署:
mvn tomcat:undeploy
启动web应用:
mvn tomcat:start
停止web应用:
mvn tomcat:stop
重新部署:
mvn tomcat:redeploy
部署展开的war文件:
mvn war:exploded tomcat:exploded
五、其他
3.3.4. 未讲到的内容
- settings.xml的配置
- pom.xml的详细配置
-
自定义插件的方法:http://maven.apache.org/plugins/maven-archetype-plugin/examples/archetype.html
-
自定义goal的执行:<preGoal><postGoal>
- 插件的查询和使用
- 搭建镜像repository
- 在maven中调用ant