【Maven】maven 项目构建

Maven是apache的一个开源项目。是一个用来把源代码构建成可发布的构件的工具。

Maven的功能非常强大,可以认为是一个项目管理工具,不仅仅是一个构建工具。

Maven本身的核心很小,但是可以在上面扩展出很多的插件。

Maven采用的是插件的思想,通过插件的功能扩展出很多的功能。

Maven采用约定大于配置的思想,在项目中采用了很多约定规则来减少配置。不像ant这样的构建工具需要很多的配置。

作为一个项目构建工具,最重要的是管理项目的库和项目之间的依赖关系。


本文将以以下面的例子来作为学习maven的一个过程。

假设要构建一个名为Content-Search的项目,该项目包含了三个子项目:contentSearch-dal,contentSearch-biz,contentSearch-web。

项目采用Struts2、hibernate3、spring2.5的框架结合。使用mysql数据库。

contentSearch-web:Web层的代码, struts2+spring2.5结合的框架。

contentSearch-biz:业务逻辑层代码。

contentSearch-dal:数据持久层代码。

三个项目之间的依赖和对外部库的依赖关系大概如下:

 

 

maven example

 

作为从一个项目构建工具角度出发,maven主要要实现的就是项目的依赖的二方、三方库管理,项目之间的依赖管理。以及项目的整个生命周期的管理,包括编译(compile)、测试(test)、打包(packaging)、部署(install)等。

以以上为背景,先介绍下maven的基本概念:

依赖管理:对每个项目对外部的依赖的管理。

远程仓库:maven外部引用仓库的管理。

项目LifeCycle抽象和管理:项目生命周期编译、测试和打包等过程的抽象和管理。

 

1. Maven安装

http://maven.apache.org/download.html在上面根据不同的操作系统选择不同的安装版本。

安装后的目录结构如下:

bin/:maven脚本

boot/

conf/:全局的settings.xml。 如果需要自己定制可以覆盖.m2文件夹里的settings.xml文件。

lib/:有包含maven核心的jar文件

设置环境变量: M2_HOME=maven安装路径  Path: $M2_HOME/bin

 

2. Maven核心概念

groupId , artifactId, packaging, version:—— 以上4个是 Maven 的 坐 标(coordinates),它们唯一标识了一个项目。

groupId: 团体,公司,小组,组织,项目,或者其它团体。如contentSearch-web,contentSearch-dal,contentSearch-biz同属一个groupId。

artifactId:在 groupId 下的表示一个单独项目的唯一标识符。项目名称ID 。

packaging: 标识项目的类型,如jar,war等。

Version:版本号。

以文章开头的例子为例,contentSearch-web的坐标可以定义如下:

groupId: com.companyName.contentSearch

artifactId:contentSearch-web

packaging: war

Version:1.0。

而contentSearch-dal的坐标如下:

groupId: com.companyName.contentSearch

artifactId:contentSearch-dal

packaging: jar

Version:1.0。

在一个 Maven 仓库中,所有的东西存储在一个与 Maven 项目坐标十分匹配的目录结构中。

/groupId/artifactId/version/artifactId-version.packaging.


POM:项目抽象模型

这是maven非常核心的概念。Maven通过它来管理项目。每一个项目都有一个pom.xml文件,该文件定义了改项目的基本信息,依赖关系等,maven对项目的生命周期管理也是基于此文件。以contentSearch-dal为例,该项目的pom.xml定义如下(关于pom.xml里面各项的含义,本文不做过多介绍,相信看了基本都能清楚。):

<pre name="code" class="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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId> com.companyName.contentSearch</groupId>
		<artifactId>contentSearch</artifactId>
		<version>1.0</version>
	</parent>
	<artifactId>contentSearch-dal</artifactId>
	<packaging>jar</packaging>
	<version>1.0</version>
	<name>contentSearch-dal</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>generalorm</groupId>
			<artifactId>hibernate</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.6</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.5.8</version>
		</dependency>
		<dependency>
			<groupId>slf4j</groupId>
			<artifactId>slf4j-nop</artifactId>
			<version>1.5.2</version>
		</dependency>
		<dependency>
			<groupId>antlr</groupId>
			<artifactId>antlr</artifactId>
			<version>2.7.6</version>
		</dependency>
		<dependency>
			<groupId>jta</groupId>
			<artifactId>jta</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.9.0.GA</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.1</version>
		</dependency>
	</dependencies>
</project>
 

Repository

仓库,二方库,三方库的概念。每当安装完成maven之后就会有一个默认的本地仓库和远程仓库。本地仓库在用户工作目录下的.m2文件夹,如在linux下为/home/yblin/.m2。远程repository默认在settings.xml里面配置,如果要修改该文件必须在.m2文件夹下面替换一下即可。当进行编译的时候,maven会先查找本地Repository,如果本地Repository没有,会去取远程repository 。

 

3. Maven命令

创建一个项目:

mvn archetype:create -DgroupId=com.xxx.simple  - DartifactId =simple

archetype:create是一个goal,-DgroupId=org.sonatype.mavenbook.ch03代表要传到目标goal任务的参数,以-D开头。

插件和目标:maven archetype:create. Archetype是插件标识,create是目标标识。

一个 Maven 插件是一个单个或者多个目标的集合。Maven 插件的例子有一些简单但核心的插件,像 Jar 插件,它包含了一组创建 JAR 文件的目标,Compiler插件,它包含了一组编译源代码和测试代码的目标,或者 Surefire 插件,它包含一组运行单元测试和生成测试报告的目标。而其它的,更有专门的插件包括:Hibernate3 插件,用来集成流行的持久化框架 Hibernate,JRuby 插件,它让你能够让运行 ruby 称为 Maven 构建的一部分或者用 Ruby 来编写 Maven 插件。Maven也提供了自定义插件的能力。一个定制的插件可以用 Java 编写,或者用一些其它的语言如 Ant,Groovy,beanshell 和之前提到的 Ruby。

 

生命周期命令:maven install等

Mvn install命令没有进行任何插件配置或者定制,所以这个例子绑定了一组标准插件的目标到默认的生命周期。当 Maven 经过以 package 为结尾的默认生命周期的时候,下面的目标按顺序被执行:

resources:resources

      Resources 插件的 resources 目标绑定到了 resources 阶段。这个目标复

      制 src/main/resources 下的所有资源和其它任何配置的资源目录,到输

      出目录。

compiler:compile

      Compiler 插件的 compile 目标绑定到了 compile 阶段。这个目标编译

      src/main/java 下的所有源代码和其他任何配置的资源目录,到输出目

      录。

resources:testResources

      Resources 插件的 testResources 目标绑定到了 test-resources 阶段。

      这个目标复制 src/test/resources 下的所有资源和其它任何的配置的测

      试资源目录,到测试输出目录。

compiler:testCompile

      Compiler 插件的 testCompile 目标绑定到了 test-compile 阶段。这个目

      标编译 src/test/java 下的测试用例和其它任何的配置的测试资源目录,

      到测试输出目录。

surefire:test

      Surefire 插件的 test 目标绑定到了 test 阶段。这个目标运行所有的测试

      并且创建那些捕捉详细测试结果的输出文件。默认情况下,如果有测试失

      败,这个目标会终止。

jar:jar

      Jar 插件的 jar 目标绑定到了 package 阶段。这个目标把输出目录打包成

      JAR 文件。

当 Maven 运行一个目标的时候,每个目标都会访问定义在项目 POM 里的信息。

 

 

其他命令:

mvn install:install-file -DgeneratePom=true -DgroupId=jep -DartifactId=jep -Dversion=3.3.0 -Dpackaging=jar -Dfile=E:/lib/LIB_COMMON/jep-3.3.0-trial.jar:添加一个包到依赖库。

mvn dependency:resolve  mvn dependency:tree浏览项目依赖。

mvn archetype:create :创建 Maven 项目

mvn compile :编译源代码

mvn test-compile :编译测试代码

mvn test : 运行应用程序中的单元测试

mvn site : 生成项目相关信息的网站

mvn clean :清除目标目录中的生成结果

mvn package : 依据项目生成 jar 文件

mvn install :在本地 Repository 中安装 jar

mvn eclipse:eclipse :生成 Eclipse 项目文件

4. Maven实践

以文章开头例子为例,做为实践。

1. 下载依赖包,并添加到本地repository.如果配置了远程依赖库里面已经有了这些依赖包,那无需下载,在编译时会自动下载到本地库。

下载struts2,hibernate2,spring2.5,mysql-jdbc等相关的包到本地,通过命令安装到本地repository.

mvn install:install-file -DgrouptId=framework -DartifactId=spring-beans -Dversion=2.5.6 -Dfile=spring-beans-2.5.6.jar

该命令将spring-beans-2.5.6.jar安装到本地repository.

2. 创建总控项目

mvn archetype:create命令创建总控项目,修改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> com.companyName.contentSearch </groupId>
  <artifactId>contentSearch</artifactId>
  <packaging>pom</packaging>
  <version>1.0</version>
  <name>Chapter 6 Simple Parent Project</name>
  <modules>
    <module>contentSearch-dal</module>
    <module>contentSearch-biz</module>
    <module>contentSearch-web</module>
  </modules>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>1.5</source>
            <target>1.5</target>
          </configuration>
        </plugin>
      </plugins>
   </pluginManagement>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


3. 创建子项目

总控项目包括了三个子项目,在总控项目文件夹下面创建3个子项目如下:

contentSearch-dal:

<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>
<parent>
     <groupId> com.companyName.contentSearch </groupId>
     <artifactId>contentSearch</artifactId>
     <version>1.0</version>
</parent>
<artifactId>contentSearch-dal</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>contentSearch-dal</name>
<url>http://maven.apache.org</url>
<dependencies>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>generalorm</groupId>
         <artifactId>hibernate</artifactId>
         <version>3.0</version>
     </dependency>
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.6</version>
     </dependency>
     <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.14</version>
     </dependency>
     <dependency>
         <groupId>dom4j</groupId>
         <artifactId>dom4j</artifactId>
         <version>1.6.1</version>
     </dependency>
     <dependency>
         <groupId>slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.5.8</version>
     </dependency>
     <dependency>
         <groupId>slf4j</groupId>
         <artifactId>slf4j-nop</artifactId>
         <version>1.5.2</version>
     </dependency>
     <dependency>
         <groupId>antlr</groupId>
         <artifactId>antlr</artifactId>
         <version>2.7.6</version>
     </dependency>
     <dependency>
         <groupId>jta</groupId>
         <artifactId>jta</artifactId>
         <version>1.1</version>
     </dependency>
     <dependency>
         <groupId>javassist</groupId>
         <artifactId>javassist</artifactId>
         <version>3.9.0.GA</version>
     </dependency>
     <dependency>
         <groupId>commons-collections</groupId>
         <artifactId>commons-collections</artifactId>
         <version>3.1</version>
     </dependency>
</dependencies>
</project>


contentSearch-biz:

<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>
<parent>
     <groupId> com.companyName.contentSearch</groupId>
     <artifactId>contentSearch</artifactId>
     <version>1.0</version>
</parent>
<artifactId>contentSearch-biz</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>contentSearch-biz</name>
<url>http://maven.apache.org</url>
<dependencies>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId> com.companyName.contentSearch </groupId>
         <artifactId>contentSearch-dal</artifactId>
         <version>1.0</version>
         <scope></scope>
     </dependency>
     <dependency>
         <groupId>com.sun</groupId>
         <artifactId>mail</artifactId>
         <version>1.4.3</version>
         <scope></scope>
     </dependency>
     <dependency>
         <groupId>com.sun</groupId>
         <artifactId>activation</artifactId>
         <version>1.1.1</version>
         <scope></scope>
     </dependency>
</dependencies>
</project>

contentSearch-web:

<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>
<parent>
     <groupId> com.companyName.contentSearch </groupId>
     <artifactId>contentSearch</artifactId>
     <version>1.0</version>
</parent>
<artifactId>contentSearch-web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>contentSearch-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
     <dependency>
         <groupId>apache.tomcat</groupId>
         <artifactId>servlet-api</artifactId>
         <version>2.5</version>
     </dependency>
     <dependency>
         <groupId>apache.commons</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>1.2.1</version>
     </dependency>
     <dependency>
         <groupId>commons-logging</groupId>
         <artifactId>commons-logging</artifactId>
         <version>1.0.4</version>
     </dependency>
     <dependency>
         <groupId>apache.tomcat</groupId>
         <artifactId>jsp-api</artifactId>
         <version>1.0</version>
     </dependency>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>struts2-core</artifactId>
         <version>2.1.8</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>xwork-core</artifactId>
         <version>2.1.6</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>spring-core</artifactId>
         <version>2.5.6</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>spring-web</artifactId>
         <version>2.5.6</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>spring-context</artifactId>
         <version>2.5.6</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>spring-beans</artifactId>
         <version>2.5.6</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>freemarker</artifactId>
         <version>2.3.15</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>struts2-spring-plugin</artifactId>
         <version>2.1.8</version>
     </dependency>
     <dependency>
         <groupId> com.companyName.contentSearch </groupId>
         <artifactId>contentSearch-biz</artifactId>
         <version>1.0</version>
     </dependency>
     <dependency>
         <groupId>framework</groupId>
         <artifactId>struts2-json-plugin</artifactId>
         <version>2.1.8</version>
     </dependency>
</dependencies>
<build>
     <finalName>contentSearch-web</finalName>
     <plugins>
         <plugin>
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>maven-jetty-plugin</artifactId>
             <version>6.1.9</version>
         </plugin>
     </plugins>
</build>
</project>

4. 编写代码,打包运行。

mvn clean install

mvn jetty:run

以上只是从构建的角度介绍maven的使用,还有很多其他的特性待以后学习后再做介绍。作为一个优秀的构建工具,maven还是值得推荐的。

 

http://maven.apache.org/guides/ 

附上官网上的指南地址。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值