当前访客身份:游客 [ 登录  | 注册加入尚学堂]
启用新域名sxt.cn
新闻资讯

[Maven学习笔记一]Maven概览

我来了! 发表于 2年前  | 评论(0 )| 阅读次数(1074 )|   1 人收藏此文章,   我要收藏
什么是Maven
  1. Maven这个单词意为知识的积累,最早在Jakata Turbine项目中它开始被用来试图简化构建过程,Maven是一种很方便的发布项目信息的方式,以及一种在多个项目中共享JAR的方式。
  2. Maven是项目管理工具,它为项目构建、测试、打包、部署定义了一个标准的生命周期。。
  3. Maven基于一个 通用的软件对象模型(Project Object Model),也就是POM。
  4. Maven项目属于Apache软件基金会的一个顶级项目http://maven.apache.org/。Apache的很多项目都是基于Maven来管理的。同时,越来越多的项目和产品使用Maven作为他们项目管理的基础。它变得易于在项目和构建系统建立关系,并且在这个关系之上导航和做报告。
  5. Maven的规范和中央仓库为项目定义了一种全新的命名系统。使用Maven可以很容易的加入其他的依赖项,并发布你自己的组件。

Maven安装

下载Maven

  1. 最新版本3.2.2,http://maven.apache.org/download.cgi
  2. 解压
  3. sudo vim /etc/profile
  4. 在最后添加系统变量M2_HOME
    Shell命令代码  
    1. export M2_HOME=/home/tom/devsoftware/apache-maven-3.2.2  
    2. export PATH=$PATH:$M2_HOME/bin  
     
  5. source /etc/profile
  6. 验证安装成功

在用户跟目录下输入mvn -version,察看Maven是否安装成功

Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T21:51:42+08:00)
Maven home: /home/tom/devsoftware/apache-maven-3.2.2
Java version: 1.7.0_55, vendor: Oracle Corporation
Java home: /software/devsoftware/jdk1.7.0_55/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-39-generic", arch: "amd64", family: "unix"
  Maven版本的Hello World


  1. 在~/learnmaven/maven-ch01中新建空的pom.xml,运行mvn compile,会报错,提示pom.xml文件必需的内容缺失
  2. 在pom.xml添加必需的xml内容,

    Xml代码  

    <?xml version="1.0" encoding="UTF-8"?>  
    <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">  
      
        <!--mandatory element in the project-->  
        <modelVersion>4.0.0</modelVersion>  
      
        <!--mandatory 3 elements in the project,这是确定唯一Maven模块的三元组-->  
        <groupId>LearnMaven</groupId>  
        <artifactId>LearnMaven</artifactId>  
        <version>1.0</version>  
      
    </project>   


 project的四个子元素,modelVersion,groupId,artifactId和version是四个必需的元素,运行mvn compile之后,得到的结果是
  1. Maven编译log代码  
        [INFO] Scanning for projects...  
        [INFO]                                                                           
        [INFO] ------------------------------------------------------------------------  
        [INFO] Building LearnMaven 1.0  
        [INFO] ------------------------------------------------------------------------  
        [INFO]   
        [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ LearnMaven ---  
        [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!  
        [INFO] skip non existing resourceDirectory /home/tom/development/learnmaven/maven-ch01/src/main/resources  
        [INFO]   
        [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ LearnMaven ---  
        [INFO] No sources to compile  
        [INFO] ------------------------------------------------------------------------  
        [INFO] BUILD SUCCESS  
        [INFO] ------------------------------------------------------------------------  
        [INFO] Total time: 2.707 s  
        [INFO] Finished at: 2014-08-02T00:22:44+08:00  
        [INFO] Final Memory: 6M/106M  
        [INFO] ------------------------------------------------------------------------   


     
  2. ~/learnmaven/maven-ch01目录下创建Maven的标准Maven模块的目录结构
        mkdir -p src/main/java

        mkdir -p src/main/resources

        mkdir  -p src/test/java


  4. 在 src/main/java目录执行命令mkdir -p com/tom

      在 src/test/java目录执行命令mkdir -p com/tom

  5. 在com/tom目录下创建一个Java类

Java代码  


    package com.tom;  
    public class HelloMaven {  
        public String hi(String name){  
            return "Hi " + name + ",welcome to the Maven world";  
        }  
    }   


 6.回到 ~/learnmaven/maven-ch01目录执行命令mvn compile,得到如下输出,说明HelloMaven已经编译成功,编译的目标路径是target/classes


 Maven编译log代码  

    [INFO] Scanning for projects...  
    [INFO]                                                                           
    [INFO] ------------------------------------------------------------------------  
    [INFO] Building LearnMaven 1.0  
    [INFO] ------------------------------------------------------------------------  
    [INFO]   
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ LearnMaven ---  
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!  
    [INFO] Copying 0 resource  
    [INFO]   
    [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ LearnMaven ---  
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!  
    [INFO] Compiling 1 source file to /home/tom/development/learnmaven/maven-ch01/<strong>target/classes</strong>  
    [INFO] ------------------------------------------------------------------------  
    [INFO] BUILD SUCCESS  
    [INFO] ------------------------------------------------------------------------  
    [INFO] Total time: 2.928 s  
    [INFO] Finished at: 2014-08-02T00:40:23+08:00  
    [INFO] Final Memory: 9M/107M  
    [INFO] -----------------------------------------------------------------------   


 察看target/classes目录,可以发现com/tom/HelloMaven.classes


  7. 在src/test/java/com/tom编写HelloMaven的单元测试类

 Java代码  


    package com.tom;  
      
    import org.junit.Assert;  
    import org.junit.Test;  
      
    public class HelloMavenTest {  
      
        @Test  
        public void testHi() {  
            String name = "tom";  
            HelloMaven hm = new HelloMaven();  
            Assert.assertEquals("Hi tom,welcome to the Maven world",hm.hi(name));  
        }  
      
    }   
 8.为pom.xml添加junit4依赖,pom.xml为


 Xml代码  


    <?xml version="1.0" encoding="UTF-8"?>  
    <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">  
      
        <!--mandatory element in the project-->  
        <modelVersion>4.0.0</modelVersion>  
      
        <!--mandatory 3 elements in the project-->  
        <groupId>LearnMaven</groupId>  
        <artifactId>LearnMaven-ch01</artifactId>  
        <version>1.0</version>  
      
        <dependencies>  
            <!--JUnit 4.11-->  
            <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.11</version>  
            </dependency>  
        </dependencies>  
      
    </project>   
9.在回到 ~/learnmaven/maven-ch01目录执行命令mvn compile test,看到测试代码编译并且执行,


 测试结果代码  

    -------------------------------------------------------  
     T E S T S  
    -------------------------------------------------------  
    Running com.tom.HelloMavenTest  
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.094 sec  
      
    Results :  
      
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  
      
    [INFO] ------------------------------------------------------------------------  
    [INFO] BUILD SUCCESS  
    [INFO] ------------------------------------------------------------------------  
    [INFO] Total time: 9.853 s  
    [INFO] Finished at: 2014-08-02T01:25:38+08:00  
    [INFO] Final Memory: 17M/170M  
    [INFO] ------------------------------------------------------------------------   
 结果显示:测试用例成功执行,并且在target下创建了surefire-reports目录,在这个目录中创建了关于测试结果的信息文件


 Maven模块依赖

1.在Maven Hello World的基础上,重新创建一个Maven模块,LearnMavenCh02,创建Maven的标准目录结构src/main/java和src/test/java等

2.在src/main/java/com/tom目录下,新建一个Java类,这个类依赖于Maven HelloWorld的HelloMaven,代码:

Java代码  


    package com.tom;  
    public class HelloMavenCh02 {  
        public String hi(String name){  
            return new HelloMaven().hi(name);  
        }  
    }   

 



 因为LearnMaven02模块依赖于LearnMaven01模块,因此,我们需要将对LearnMaven01模块的依赖加入到LearnMaven02模块中,pom文件的内容如下

 Xml代码 


收藏代码

    <?xml version="1.0" encoding="UTF-8"?>  
    <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">  
      
        <!--mandatory element in the project-->  
        <modelVersion>4.0.0</modelVersion>  
      
        <!--mandatory 3 elements in the project-->  
        <groupId>LearnMaven</groupId>  
        <artifactId>LearnMaven-ch02</artifactId>  
        <version>1.0</version>  
      
        <dependencies>  
            <!--JUnit 4.11-->  
            <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.11</version>  
            </dependency>  
            <dependency>  
                <groupId>LearnMaven</groupId>  
                <artifactId>LearnMaven-ch01</artifactId>  
                <version>1.0</version>  
            </dependency>  
        </dependencies>  
      
      
    </project  > 
 3.执行命令mvn compile,报错,报错信息是


[ERROR] Failed to execute goal on project LearnMaven-ch02: Could not resolve dependencies for project LearnMaven:LearnMaven-ch02:jar:1.0: Failure to find LearnMaven:LearnMaven-ch01:jar:1.0 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]

意思是在中央仓库找不到LearnMaven-ch01,原因是我们还没有将LearnMaven-ch01放到本地仓库中

 4.对LearnMaven01模块执行mvn install命令,将LearnMaven01模块添加到本地仓库.在LearnMaven01模块的~/development /learnmaven/maven-ch01目录执行mvn install,可以看到LearnMaven-ch01模块已经添加到本地仓库中,本地仓库路径~/.m2/repository /LearnMaven/LearnMaven-ch01/1.0/LearnMaven-ch01-1.0.jar

 5.回到~/development/learnmaven/maven-ch02目录执行mvn compile发现LearnMaven-ch02模块编译成功

 

仓库

1.在pom.xml中定义的dependency,当查找这些依赖的jar包时候,首先从本地仓库中,然后再到全球的中央Maven库中下载,比如http://mvnrepository.com/中查找下载

2. 本地仓库~/.m2/repository

    执行mvn install是将Maven模块打包后发布到本地仓库中,本地仓库的路径是在M2_HOME/conf/settings.xml中指定,

Xml代码  


    <!-- localRepository  
     | The path to the local repository maven will use to store artifacts.  
     |  
     | Default: ${user.home}/.m2/repository  
    <localRepository>/path/to/local/repo</localRepository>  
    -->   


 3. 中央仓库,默认的配置是在$M2_HOME/lib/maven-settings-builder-3.2.2.jar包的pom-4.0.0.xml中,

Xml代码  


    <repositories>  
       <repository>  
         <id>central</id>  
         <name>Central Repository</name>  
         <url>http://repo.maven.apache.org/maven2</url>  
         <layout>default</layout>  
         <snapshots>  
           <enabled>false</enabled>  
         </snapshots>  
       </repository>  
     </repositories>  
      
     <pluginRepositories>  
       <pluginRepository>  
         <id>central</id>  
         <name>Central Repository</name>  
         <url>http://repo.maven.apache.org/maven2</url>  
         <layout>default</layout>  
         <snapshots>  
           <enabled>false</enabled>  
         </snapshots>  
         <releases>  
           <updatePolicy>never</updatePolicy>  
         </releases>  
       </pluginRepository>  
     </pluginRepositories>   




 

分享到:
分享到:0
关注微信,跟着我们扩展技术视野。每天推送IT新技术文章,每周聚焦一门新技术。微信二维码如下:
微信公众账号:尚学堂(微信号:bjsxt-java)
声明:博客文章版权属于原创作者,受法律保护。如果侵犯了您的权利,请联系管理员,我们将及时删除!
(邮箱:webmaster#sxt.cn(#换为@))
北京总部地址:北京市海淀区西三旗桥东建材城西路85号神州科技园B座三层尚学堂 咨询电话:400-009-1906 010-56233821
Copyright 2007-2015 北京尚学堂科技有限公司 京ICP备13018289号-1 京公网安备11010802015183