1,认识maven
maven是基于Ant的构建工具的项目构建工具,作用是将项目打包到服务器中,使用maven,可以将一个大项目拆分成多个子项目,然后通过依赖关系把子项目联系在一起构成一个大的项目,也可以将一个大项目拆分成多个项目部署到多个服务器中,以缓解服务器压力(分布式部署思想)。maven的运行原理图如下所示:
maven首先通过中央仓库(中央官方仓库地址:https://repo1.maven.org/maven2/,国内可使用阿里的中央仓库)下载架包放到本地仓库(如果本地有无需下载),放到本次仓库中后使用本地仓库的架包,而且maven在配置架包的时候,会把架包依赖也下载下来,大大的方便我们找架包。
网址(查看坐标):http://mvnrepository.com/
1)配置本地参考2,配置maven环境
在本地安装好maven之后,找到目录conf\settings.xml文件,找到下面这个注释:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
在这个注释下面配置(复制就可以了)
<localRepository>D:/m2/repository</localRepository>
本地仓库就配置好了,本地仓库自己建个文件夹就可以了。
2)配置中央仓库:
找到 <mirrors>标签,在标签内配置中央参考,我用的是阿里的中央仓库,要是使用官方的仓库,可以百度找地址就可以了。
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
3)配置jdk
如果不配置,maven使用的jdk是1.4或是1.5,在打包到服务器时运行使用的是本地配置的jdk环境,可能会出现错误。在<profiles>标签内配置。
<profile>
<id>jdk-1.7</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
3,创建maven项目
我是用的编译器是eclipse,那我就使用它来举例了。
1)New--> Maven project
2)勾选复选框,表示创建一个简单Maven项目(不应用任何模板)
3)填写项目信息
group id:自定义,一般是公司域名倒写
artifact id :自定义,项目名
version:版本号
Packaging: 项目的类型,最终会被打包成什么类型.,jar : java项目;war: web项目;pom:逻辑父项目,只要一个项目有子项目必须是pom类型;
点击finish就完成项目的创建了。
4,maven项目目录结构(javaee视图)
1)src/main/java:存放java代码
2)src/main/resources:存放配置文件
3)pom.xml文件:maven的配置文件,配置当前项目所依赖的其他项目或jar或插件等
5,maven项目的依赖关系
1)依赖关系
创建两个maven项目,test1和test2,test1为war项目,test2为jar项目,在test1中引入test2的jar项目,在test1的pom.xml文件内的<dependencies>标签写入:
<dependency>
<groupId>com.yjb</groupId>
<artifactId>test2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
就可将test2引入到test1项目中。
2)子类项目和父类项目的继承关系
首先创建父类项目,项目类型必须是pom类型。在创建子类项目,在创建面板引入父类项目,如下图所示:
点击完成,父类项目就把子类项目关联到一起了。先看父项目的pom文件:
<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>com.yjb</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
</project>
在看子项目的pom文件:
<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>
<parent>
<groupId>com.yjb</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.yjb</groupId>
<artifactId>child</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
</project>
查看两个项目的pom文件发现,两个项目的groupId和version相同,在子项目这两个标签会报警告,我们可以将子类项目的groupId和version删掉,由于是继承关联,子项目直接引用父项目的groupId和version。
<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>
<parent>
<groupId>com.yjb</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child</artifactId>
<packaging>war</packaging>
</project>
在这里可以看出,父项目可以统一管理子项目的版本号和组名ID,不易出现版本错误问题。
注意:父项目必须是pom项目,子项目可以是war、jar或者pom项目。
3)聚合关系
右键点击父项目,创建子项目选项为project module,父项目选择问parent,此时父项目将会出现一个新标签modules用于表现其强依赖关系,这种方式主要是为了看出父类到底包含哪些子项目,其功能和继承关系是一样的。
<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>com.yjb</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>child2</module>
</modules>
</project>
6,聚合项目和继承项目的区别
在语意上聚合项目父项目和子项目关系性较强,在语意上单纯继承项目父项目和子项目关系性较弱
7,<dependencyManagement>标签
1)写在父项目,其作用作用为声明可能使用到的所有jar,子项目中只需要有坐标的<groupid>和<artifactid>,<version>继承父项目
2)在父项目中<properties>把所有版本好进行统一管理,子标签自定义,在父类的依赖架包中的version标签使用 ${自定义名称} 引用标签的值
父项目的pom文件
<properties>
<spring-version>4.1.6.RELEASE</spring-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
子项目的pom文件:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
8,maven创建war项目
1)项目结构
比较jar项目,war项目多出一个webapp目录,主要用于存放静态资源和网页,需要创建MATA-INF和WEN-INF文件同时需要导入web.xml文件。
2)JSP环境搭建
如果需要些jsp,需要对pom文件进行三个依赖(html不需要),
<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>com.bjsxt</groupId>
<artifactId>ego-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<servlet-version>3.0.1</servlet-version>
<jsp-version>2.2</jsp-version>
<jstl-version>1.2</jstl-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
然后导入tomcat
右键项目build path->configure build path->add library->server runtime->apache tomcat添加tomcat,然后clean项目在install就可以了。
9,使用插件
这里举例使用tomcat插件,但使用tomcat插件之后,发布项目之后使用的不在是本地tomcat而是使用配置的tomcat插件。
使用标签build的子标签plugins,如下所示。
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 控制tomcat端口号 -->
<port>80</port>
<!-- 项目发布到tomcat后的名称 -->
<!-- / 相当于把项目发布名称为ROOT -->
<!-- /abc -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
当使用插件tomcat时,会把jsp的jar全部打包到tomcat插件中,而jsp的架包值需要在编译期使用,运行会报错,所以还需要在jsp的jar包依赖使用scope标签来规定范围。
<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>com.bjsxt</groupId>
<artifactId>ego-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<servlet-version>3.0.1</servlet-version>
<jsp-version>2.2</jsp-version>
<jstl-version>1.2</jstl-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
运行项目:
右键项目--> run as --> maven build-->Goals 中输入 :clean tomcat7:run