1. Maven介绍
1.1. 简介
java编写的用于构建系统的自动化工具。 目前版本是2.0.9,注意maven2和maven1有很大区别,阅读第三方文档时需要区分版本。
1.2. Maven资源
1.3. Maven和Ant的区别
Maven正在逐渐取代Ant,很多java开源软件(Spring、Struts2 ……)已经使用maven。
1.4. Maven的基本功能
通过我写的商品管理的小例子,演示结合maven和svn的功能。
2. Maven使用
2.1. Maven的安装和配置
2.2. Maven的基本使用
Maven的基本使用介绍通过命令行编写简单的java和web项目。
2.2.1. 实现Java项目
通过maven在命令行下创建普通java项目,也就是main方法执行的项目或者jar文件的类库。
2.2.1.1. 创建Maven项目
执行:
mvn archetype:generate
在交互界面中:
观察helloworld目录(Define value for artifactId输入的项目名称)下生成的文件和目录:
2.2.1.2. 运行Maven项目
命令行进入helloworld目录Define value for artifactId输入的项目名称)。 项目打包
mvn package
检查命令生成了什么?
运行打包的jar文件:
java -cp target\helloworld-1.0-SNAPSHOT.jar com.easymorse.App
编译源程序
mvn compile
编译并测试
mvn test
清空生成的文件
mvn clean
将maven项目转化为eclipse项目 命令行运行:
mvn eclipse:eclipse
打开eclipse,菜单选择:file>import>general>existing projects into workspace,在对话框中选中目录,导入即可。 如果要清除有关eclipse项目的配置信息:
mvn -Dwtpversion=1.0 eclipse:clean
联合使用
mvn eclipse:clean clean
2.2.2. 实现Web项目
通过maven在命令行下创建java web项目。
2.2.2.1. 创建Maven项目
在命令行输入,这一步和创建java项目类似:
mvn archetype:generate
交互步骤说明:
需要在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>
repository目录的作用 repository的位置,在用户目录的.m2目录下。 repository目录的作用,对依赖类库的缓存。
2.2.2.2. 运行Maven项目
项目打包
mvn package
启动tomcat
mvn tomcat:run
启动jetty
mvn jetty:run
转化为eclipse项目
mvn -Dwtpversion=1.5 eclipse:eclipse
这样生成wtp插件的web项目。 打开eclipse,菜单选择:file>import>general>existing projects into workspace,在对话框中选中目录,导入即可。 另外,需要在eclipse里创建一个classpath变量,名称为:M2_REPO,值为系统用户下.m2/repository目录。
3. POM文件的基本配置
3.1. POM介绍
3.1.1. 什么是POM
Project Object Model,项目对象模型。 通过xml格式保存的pom.xml文件。 作用类似ant的build.xml文件,功能更强大。 该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。
3.1.2. 快速浏览
一个完整的pom.xml文件,放置在项目的根目录下。
<project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- The Basics --> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> <packaging>...</packaging> <dependencies>...</dependencies> <parent>...</parent> <dependencyManagement>...</dependencyManagement> <modules>...</modules> <properties>...</properties> <!-- Build Settings --> <build>...</build> <reporting>...</reporting> <!-- More Project Information --> <name>...</name> <description>...</description> <url>...</url> <inceptionYear>...</inceptionYear> <licenses>...</licenses> <organization>...</organization> <developers>...</developers> <contributors>...</contributors> <!-- Environment Settings --> <issueManagement>...</issueManagement> <ciManagement>...</ciManagement> <mailingLists>...</mailingLists> <scm>...</scm> <prerequisites>...</prerequisites> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories> <distributionManagement>...</distributionManagement> <profiles>...</profiles> </project>
3.2. 基本设置
3.2.1. 协作关系
<project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.codehaus.mojo</groupId> <artifactId>my-project</artifactId> <version>1.0</version> <packaging>war</packaging> </project>
3.2.2. POM间关系
3.2.2.1. 依赖关系
依赖关系列表(dependency list)是POM的重要部分。
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version> <scope>test</scope> </dependency> ... </dependencies>
如何查到依赖的类库? 一般可以通过这个网站:http://www. 比如查询hibernate,可以找到结果列表中的hibernate类库条目。 点击:http://www./artifact/org.hibernate/hibernate, 选择版本,比如3.2.6ga,即:http://www./art ... /hibernate/3.2.6.ga 复制文章中的:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.6.ga</version> </dependency>
到pom.xml文件中即可。 是否还需要找到hibernate依赖的pom? 不需要,hibernate也会有pom,maven会通过它的pom自动找到它依赖的类库。
3.2.2.2. 继承关系
继承其他pom.xml配置的内容。 maven提供了一个类似java.lang.Object的顶级父pom.xml文件。 可以通过下面命令查看当前pom.xml受到超pom.xml文件的影响:
mvn help:effective-pom
创建一个各种项目可复用的pom.xml文件:http://easymorse./svn/trunk/pom/pom.xml 部署要复用的pom.xml文件:
mvn install
在自己的pom文件中继承上述pom:
<parent> <groupId>com.easymorse</groupId> <artifactId>pom</artifactId> <version>0.1</version> </parent>
3.2.2.3. 聚合关系
用于将多个maven项目聚合为一个大的项目。 比如目录结构如下:
. |-- pom.xml |-- module-a `-- pom.xml |-- module-b `-- pom.xml |-- module-c `-- pom.xml |-- foo-all `-- pom.xml
那么总的pom.xml文件类似:
... <modules> <module>module-a</module> <module>module-b</module> <module>module-c</module> <module>foo-all</module> </modules> </project>
参考文档:http://maven./plugins/maven-eclipse-plugin/reactor.html 原文示例有误,见:modules.rar
3.2.3. 其他配置
maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。 有以下分类:
3.3. 其他
3.3.1. 设置离线
在.m2目录下创建settings.xml文件(如果没有的话)
<settings xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/settings-1.0.0.xsd"> <offline>true</offline> </settings>
3.3.2. 安装第三方包
经常有第三方包,因为一些原因,在网上repository上没有,需要自己动手安装。 比如sun某些版本的jar文件,比如oracle的驱动。 已oracle驱动程序为例,比如驱动路径为c:/driver/ojdbc14.jar,是10.2.0.3.0版本 在该网址能够查到:http://www./artifact/com.oracle/ojdbc14 artifactId和groupId。
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>
3.3.3. 部署到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./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./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. 未讲到的内容
|
|