Maven--项目管理神器

    特别鸣谢:http://www.cnblogs.com/hongwz/p/5456578.html


Maven的作用

  1. 在开发中,为了保证编译通过,我们会到处去寻找jar包,当编译通过了,运行的时候,却发现"ClassNotFoundException",我们想到的是,难道还差jar包?
  2. 每个Java项目的目录结构都没有一个统一的标准,配置文件到处都是,单元测试代码到底应该放在那里也没有一个权威的规范。
  3. 因此,我们就要用到Maven(使用Ant也可以,不过编写Ant的xml脚本比较麻烦)----一个项目管理工具
  4. Maven主要做了两件事:
  • 统一开发规范与工具
  • 统一管理jar包

    下面我们来对比一下,首先建立一个普通的Java工程,是这样的:

   这个我们都很熟悉,src下建包写代码,那么配置文件放在哪里?单元测试代码放在哪里?没有一个统一标准,更多时候都是开发者的自由发挥,每个人有自己的风格,这并不十分适合团队协作。接下来,看一下使用maven构建一个普通Java项目之后的目录结构:

看到使用Maven构建的普通Java项目,对源代码、单元测试代码、资源乃至后续需要的文件都有专门的目录规划。

上面的最后有一个pom.xml,这是Maven的核心配置文件,pom称为Project Object Model(项目对象模型),它用于描述整个Maven项目,所以也称为Maven描述文件。

当然事情不会这么简单,接下来,继续进入Maven的世界吧。

pom.xml

打开pom.xml,最基础的是这样的:

1
2
3
4
5
6
7
8
9
10
<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.xrq.withmaven</groupId>
   <artifactId>withmaven</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <build/>
</project>

因为这个配置文件是Maven的核心,因此有必要详细解读一下pom.xml,来先看一下上面的几个:

1、modelVersion

  指定了当前Maven模型的版本号,对于Maven2和Maven3来说,它只能是4.0.0

2、groupId

  顾名思义,这个应该是公司名或是组织名。一般来说groupId是由三个部分组成,每个部分之间以"."分隔,第一部分是项目用途,比如用于商业的就是"com",用于非营利性组织的就  是"org";第二部分是公司名,比如"tengxun"、"baidu"、"alibaba";第三部分是你的项目名

3、artifactId

  可以认为是Maven构建的项目名,比如你的项目中有子项目,就可以使用"项目名-子项目名"的命名方式

4、version

  版本号,SNAPSHOT意为快照,说明该项目还在开发中,是不稳定的版本。在Maven中很重要的一点是,groupId、artifactId、version三个元素生成了一个Maven项目的基本坐标,这非常重要,我在使用和研究Maven的时候多次感受到了这点。

在上面的这些元素之外,还有一些元素,同样罗列一下:

1、packing

  项目打包的类型,可以使jar、war、rar、ear、pom,默认是jar

2、dependencies和dependency

  前者包含后者。前面说了,Maven的一个重要作用就是统一管理jar包,为了一个项目可以build或运行,项目中不可避免的,会依赖很多其他的jar包,在Maven中,这些依赖就被称为dependency。

  说到这里,就有一个本地仓库远程仓库的概念了。官方下载的本地仓库的配置在"%MAVEN_HOME%\conf\settings.xml"里面,找一下"localRepository"就可以了;MyEclipse默认的本地仓库的地址在"{user.home}/.m2/repository"路径下,同样找一下"localRepository"就可以找到MyEclipse默认的本地仓库了。

  本地仓库和远程仓库是这样的,Maven工程首先会从本地仓库中获取jar包,当无法获取指定jar包时,本地仓库会从远程仓库(中央仓库)中下载jar包,并放入本地仓库以备将来使用

  举个例子,比方说我的项目中用到了MyBatis,那么可以这么配置:

1
2
3
4
5
6
7
<dependencies>
     <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis</artifactId>
         <version>3.2.5</version>
     </dependency>
</dependencies>

    之前有说过groupId、artifactId、version唯一标识一个Maven项目,有了这三个元素,我们就可以去远程仓库下载MyBatis3.2.5.jar到本地仓库了。回想我们之前的做法,如果要MyBatis的jar包,发现没有,然后去网上下载一个,需要另外的jar包,然后去网上下载一个,但是有了Maven,就方便多了,只需要配置jar包对应的dependency依赖,Maven会自动帮助我们去远程仓库中下载jar包到本地仓库中。

3、properties

  properties是用来定义一些配置属性的,例如project.build.sourceEncoding(项目构建源码编码方式),可以设置为UTF-8,防止中文乱码,也可定义相关构建版本号,便于日后统一升级。

4、build

  build表示与构建相关的配置,比如build下有finalName,表示的就是最终构建之后的名称。

接着解释一下Maven的目录结构:

  • main目录下是项目的主要代码,test目录下存放测试相关的代码
  • 编译输出后的代码会放在target目录下
  • src/main/java下存放Java代码,src/main/resources下存放配置文件
  • 这里没有webapp,Web项目会有webapp目录,webapp下存放Web应用相关代码
  • pom.xml是Maven项目的配置文件

  • 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>hotelsup</groupId>
    <artifactId>hotelsup</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>hotelsup</name>
    <description />
    <!-- 加入本地私服 -->
    <!-- 设定主仓库 -->
    <repositories>
    <!-- nexus私服 -->
    <repository>
    <id>nexus-repos</id>
    <name>Team Nexus Repository</name>
    <url>http://192.168.0.107:8081/nexus/content/groups/public/</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </repository>
    </repositories>
    <!-- 设定插件仓库 -->
    <pluginRepositories>
    <pluginRepository>
    <id>nexus-repos</id>
    <name>Team Nexus Repository</name>
    <url>http://192.168.0.107:8081/nexus/content/groups/public/</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </pluginRepository>
    </pluginRepositories>
    <properties>
    <webVersion>3.0</webVersion>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- spring版本号 -->
    <spring.version>4.0.2.RELEASE</spring.version>
    <!-- mybatis版本号 -->
    <mybatis.version>3.2.6</mybatis.version>
    <!-- log4j日志文件管理包版本 -->
    <slf4j.version>1.7.7</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>bean-validator</artifactId>
    <version>3.0-JBoss-4.0.2</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.enterprise.deploy</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.jms</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.management.j2ee</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.resource</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.security.auth.message</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.security.jacc</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet.jsp</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api-osgi</artifactId>
    <version>2.2.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
    <version>1.1.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jstl-impl</artifactId>
    <version>1.2</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.3</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.xml</groupId>
    <artifactId>webservices-api-osgi</artifactId>
    <version>2.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.jboss.weld</groupId>
    <artifactId>weld-osgi-bundle</artifactId>
    <version>1.0.1-SP3</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>1.2.1</version>
    </dependency>
    <!-- 引入SSM框架的jar包 -->
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>bean-validator</artifactId>
    <version>3.0-JBoss-4.0.2</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.enterprise.deploy</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.jms</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.management.j2ee</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.resource</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.security.auth.message</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.security.jacc</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet.jsp</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api-osgi</artifactId>
    <version>2.2.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
    <version>1.1.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jstl-impl</artifactId>
    <version>1.2</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.3</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.xml</groupId>
    <artifactId>webservices-api-osgi</artifactId>
    <version>2.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.jboss.weld</groupId>
    <artifactId>weld-osgi-bundle</artifactId>
    <version>1.0.1-SP3</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>1.2.1</version>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
    <scope>test</scope>
    </dependency>
    <!-- spring核心包 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
    </dependency>


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
    </dependency>


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
    </dependency>


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring.version}</version>
    </dependency>


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
    </dependency>


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <!-- 切面增强方法的jar包 -->
    <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.7.4</version>
    </dependency>
    <!-- 引入oracle的jar包 -->
    <dependency>
    <groupId>com.ojdbc</groupId>
    <artifactId>oracle</artifactId>
    <version>1.0</version>
    <classifier>ojdbc6</classifier>
    </dependency>
    <!-- mybatis核心包 -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>${mybatis.version}</version>
    </dependency>
    <!-- mybatis懒加载需要引入的jar包,cglib包 -->
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
      <version>3.1</version>
    </dependency>
    <!-- mybatis/spring包 -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.2</version>
    </dependency>
    <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
    <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
    </dependency>
    <!-- 日志文件管理包 -->
    <!-- log start -->
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j.version}</version>
    </dependency>
    <!-- 格式化对象,方便输出日志 -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.41</version>
    </dependency>


    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
    </dependency>


    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
    <!-- 映入JSON -->
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
    </dependency>
    <!-- 上传组件包 -->
    <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
    </dependency>
    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
    </dependency>
    <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
    </dependency>
    </dependencies>
    <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
    <resource>
    <directory>src</directory>
    <excludes>
    <exclude>**/*.java</exclude>
    </excludes>
    </resource>
    </resources>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>
    </configuration>
    </plugin>
    <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
    <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值