zoukankan      html  css  js  c++  java
  • Maven

    1-下载及安装

    1.1 - Maven - 项目管理利器

    http://maven.apache.org/
    Apache组织的开源项目。
    Maven是一个基于POM(Project Object Model, 项目对象模型)的项目构建和管理工具。
    可以通过一小段描述信息来管理项目的构建、报告和文档,有助于开发者快速完成项目的配置,快速建立开发环境,从而提高开发效率。

    常用项目管理和构建工具:

    1.2 - Maven的下载及安装

    以windows系统为例:

    1. 确认已安装JDK并配置系统变量JAVA_HOME
    2. 安装Maven(解压安装包,例如:apache-maven-3.5.0-bin.zip)
    3. 配置系统变量M2_HOME,指向maven的安装目录(例如"D:DownLoadFilesapache-maven-3.5.0"), 并将maven的安装目录的bin目录添加到系统path(例如"%M2_HOME%in",注意前后的分号)
    4. 运行"mvn -version"命令测试是否安装成功,如果安装成功则显示Maven、Java、OS等相关版本信息:
    $ mvn -version
    Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
    Maven home: D:DownLoadFilesapache-maven-3.5.0
    Java version: 1.8.0_101, vendor: Oracle Corporation
    Java home: C:Program FilesJavajdk1.8.0_101jre
    Default locale: en_US, platform encoding: GBK
    OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
    

    安装后的目录结构:

    - bin/  : 包含mvn的运行脚本
    - boot/ : 包含一个类加载器的框架
    - conf/ : 配置文件目录,例如经常用到的settings.xml文件
    - lib/  : maven运行时用到的所有类库(包括maven自身和第三方的类库)
    - LICENSE  : 许可信息
    - NOTICE  :版权信息
    - README.txt  :使用说明及其他信息
    

    1.3 - Maven项目的默认目录结构

    pom.xml            Maven项目的核心配置文件,位于项目根目录
    
    src                    源代码目录
      - src/main/java/         源文件(不包含测试)目录,存放主代码
      - src/main/resources/      资源文件目录
      - src/test/java/         测试源文件目录,存放测试代码
      - src/test/resources/      测试资源文件目录
    
    target             构建过程中的默认生成的临时目录
      - target/classes/            存放src/main/java目录下源文件编译出来的字节码文件(.class)
      - target/maven-archiver/
      - target/maven-status/
      - target/surefire-reports/  存放生成的测试报告
      - target/test-classes/      存放src/test/java目录下源文件编译出来的字节码文件(.class)
      - target/xxx-y.y.y-zzz.jar  生成的jar包
    

    2-坐标和仓库

    2.1 - Maven坐标

    在Maven中任何一个依赖、插件、项目构建的输出都可以被称为构件
    所有构件通过坐标所为其唯一的标识

    示例:一个基本坐标

            <groupId>anliven.testmaven02</groupId>
            <artifactId>testmaven02</artifactId>
            <version>0.0.1-SNAPSHOT</version>
    

    2.2 - Maven资源仓库

    Maven资源库为依赖包提供来源。
    如果本地仓库中的没有相应的依赖包,默认继续在中央或远程仓库查找,下载后会放到本地仓库。如果都没有查找到,将提示报错。

    • 中央仓库(central):Maven官方维护的仓库,例如“http://central.maven.org/maven2/”。
    • 远程仓库:在远程服务器上建立的私有性质仓库,本质类似于中央仓库,可通过setting.xml文件设置。
    • 本地仓库:包含下载的所有依赖包,默认位于用户目录(例如windows系统:C:Users<username>.m2 epository),可通过setting.xml文件设置。

    官方资源仓库

    阿里云资源仓库
    Nexus : http://maven.aliyun.com/nexus/
    Repositories: repositories http://maven.aliyun.com/nexus/#view-repositories

    查找jar包信息

    其他资源仓库

    2.3 - 设置本地Maven仓库

    本地仓库包含下载的所有依赖包,默认位于用户目录(例如windows系统:C:Users<username>.m2 epository),可通过setting.xml文件设置。
    示例:

    <localRepository>D:DownLoadFilesapache-maven-repo</localRepository>
    

    2.4 - 设置镜像仓库

    通过设置多个远程和中央仓库的镜像地址可以避免访问缓慢或无法访问的问题。
    示例:

       <mirrors>
            <!-- 中央仓库 -->    
            <mirror>
                <id>central</id>
                <mirrorOf>*</mirrorOf>
                <name>central</name>
                <url>http://central.maven.org/maven2/</url>
            </mirror>
            <!-- 中央仓库1 -->
            <mirror>
                <id>repo1</id>
                <mirrorOf>central</mirrorOf>
                <name>Human Readable Name for this Mirror.</name>
                <url>http://repo1.maven.org/maven2/</url>
            </mirror>  
            <!-- 中央仓库2 -->
            <mirror>
                <id>repo2</id>
                <mirrorOf>central</mirrorOf>
                <name>Human Readable Name for this Mirror.</name>
                <url>http://repo2.maven.org/maven2/</url>
            </mirror>
            <!-- 阿里云仓库 -->
            <mirror>
                <id>alimaven</id>
                <mirrorOf>central</mirrorOf>
                <name>aliyun maven</name>
                <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
            </mirror>    
        </mirrors>
    

    3-POM文件

    3.1 - POM

    POM文件是Maven项目中的核心配置和管理文件,也被称为Maven描述文件。

    • POM(Project Object Model,项目对象模型)是描述项目构建信息的XML格式文件,位于项目的根目录。
    • 统一管理项目构建的关键信息,包括:开发规范、开发工具、项目代码、测试代码、资源、依赖的包等。
    • pom之间实际上存在三种关系:继承、依赖、聚合。

    官网参考信息:http://maven.apache.org/ref/3.5.0/maven-model/maven.html

    3.2 - 解读pom.xml

    • project POM文件的根元素,包含约束信息
    • modelVersion 指定当前Maven模型的版本号,对于Maven2和Maven3只能设置为4.0.0
    • groupId 项目的全球唯一标识符(整个系统的名称),一般是域名的反写
    • artifactId 构件标识符(子模块名称),和groupId一起唯一标识一个构件,可以使用"项目名-子模块名"的命名方式
    • version 项目当前版本,格式为:主版本.次版本.增量版本-限定版本号;限定版本号可以设置为SNAPSHOT(开发),Laest(最新),Alpha(内部测试),Release(稳定),Beta(公测),GA(正式发布)等

    重要:groupId、artifactId、version三个元素构成基本坐标,可以唯一标识一个Maven项目。

    • packaging 项目产生的构件类型(项目打包的类型),可以取值为jar、war、rar、ear、pom,也可以创建新类型;如果不设置,默认为jar
    • name 项目的名称, Maven产生的文档用
    • url 项目主页的URL, Maven产生的文档用
    • description 项目的详细描述, Maven 产生的文档用
    • developers 项目开发人员信息
    • licenses 许可信息
    • organization 组织信息
    • dependencies 项目相关的所有依赖(dependency 包含一个依赖包的坐标信息)
    • properties 定义配置属性,例如设置project.build.sourceEncoding为UTF-8,防止中文乱码
    • build 定义构建项目需要的信息
    • resources 描述项目相关或测试相关的所有资源路径

    3.3 - parent - 继承

    应用在子项目中。

        <!-- 父项目的坐标。如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值。坐标包括group ID,artifact ID和 version。 --> 
        <parent> 
            <!-- 被继承的父项目的构件标识符 --> 
            <artifactId>xxx</artifactId>
            <!-- 被继承的父项目的全球唯一标识符 -->
            <groupId>xxx</groupId> 
            <!-- 被继承的父项目的版本 --> 
            <version>xxx</version>
            <!-- 父项目的pom.xml文件的相对路径。默认值是../pom.xml。Maven首先在构建当前项目的地方寻找父项目的pom,其次在文件系统的这个位置(relativePath位置),然后在本地仓库,最后在远程仓库寻找父项目的pom。 --> 
            <relativePath>xxx</relativePath> 
        </parent> 
    

    3.4 - dependencies - 依赖

    官网信息:Dependency Scope
    http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

    • groupId 依赖项的groupId
    • artifactId 依赖项的artifactId
    • version 依赖项的版本
    • exclusions 排除项目中的依赖冲突时使用。
    • scope 依赖项的适用范围:
      • compile,缺省值,适用于所有阶段,会随着项目一起发布。
      • provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
      • runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
      • test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
      • system,类似provided,与本机系统相关联,可移植性差,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
      • import,导入的范围,只使用在dependencyManagement中,表示从其它的pom中导入dependecy的配置
        <!-- 该元素描述了项目相关的所有依赖。 这些依赖组成了项目构建过程中的一个个环节。它们自动从项目定义的仓库中下载。 --> 		
        <dependencies> 
            <dependency> 
                <!-- 依赖的group ID --> 
                <groupId> org.apache.maven </groupId> 
                <!-- 依赖的artifact ID --> 
                <artifactId> maven-artifact </artifactId> 
                <!-- 依赖的版本号。 在Maven 2里, 也可以配置成版本号的范围。 --> 
                <version> 3.8.1 </version> 
                <!-- 依赖类型,默认类型是jar。它通常表示依赖的文件的扩展名,但也有例外。一个类型可以被映射成另外一个扩展名或分类器。类型经常和使用的打包方式对应,尽管这也有例外。一些类型的例子:jar,war,ejb-client和test-jar。--> 
                <type> jar </type> 
                <!-- 依赖的分类器。分类器可以区分属于同一个POM,但不同构建方式的构件。分类器名被附加到文件名的版本号后面。例如,如果你想要构建两个单独的构件成JAR,一个使用Java 1.4编译器,另一个使用Java 6编译器,你就可以使用分类器来生成两个单独的JAR构件。 --> 
                <classifier></classifier> 
                <!-- 依赖范围。在项目发布过程中,帮助决定哪些构件被包括进来。欲知详情请参考依赖机制。 
                    - compile :默认范围,用于编译 
                    - provided:类似于编译,但支持你期待jdk或者容器提供,类似于classpath 
                    - runtime: 在执行时需要使用 
                    - test: 用于test任务时使用 
                    - system: 需要外在提供相应的元素。通过systemPath来取得 
                    - systemPath: 仅用于范围为system。提供相应的路径 
                    - optional: 当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用 --> 
                <scope> test </scope> 
                <!-- 仅供system范围使用。注意,不鼓励使用这个元素,并且在新的版本中该元素可能被覆盖掉。该元素为依赖规定了文件
                     系统上的路径。需要绝对路径而不是相对路径。推荐使用属性匹配绝对路径,例如${java.home}。 --> 
                <systemPath></systemPath> 
                <!-- 排除依赖列表,不依赖项目的依赖。此元素主要用于解决版本冲突问题 --> 
                <exclusions> 
                    <exclusion> 
                        <artifactId> spring-core </artifactId> 
                        <groupId> org.springframework </groupId> 
                    </exclusion> 
                </exclusions> 
                <!-- 可选依赖,如果在项目B中声明C依赖为可选,那么需要在依赖于B的项目A中显式的引用对C的依赖。可选依赖阻断依赖的传递性。 --> 
                <optional> true </optional> 
            </dependency> 
        </dependencies>	
    

    dependencyManagement
    依赖管理。其中定义的多个依赖,并不会实际引入。
    应用在父模块中,供子模块所继承使用。

    <!-- 继承自该项目的所有子项目的默认依赖信息。这部分的依赖信息不会被立即解析,而是当子项目声明一个依赖(必须描述group ID和artifact ID信息),
    如果group ID和artifact ID以外的一些信息没有描述,则通过group ID和artifact ID匹配到这里的依赖,并使用这里的依赖信息。 --> 
        <dependencyManagement> 
            <dependencies> 
                <!-- 参见dependencies/dependency元素 --> 
                <dependency></dependency> 
            </dependencies> 
        </dependencyManagement> 
    

    3.5 - modules - 聚合

    可以通过一个大的项目来整合各个小的模块

     <!-- 模块(有时称作子项目) 被构建成项目的一部分。列出的每个模块元素是指向该模块的目录的相对路径 --> 
        <modules>
            <!--子项目相对路径-->
            <module></module>
        </modules> 
    

    同时需要将packaging设置为pom

      <packaging>pom</packaging>
    

    3.6 - pluginManagement

    Plugin的配置,应用在父项目。

        <!-- 子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置 --> 
        <pluginManagement> 
             <!-- 使用的插件列表 。 --> 
             <plugins> 
                 <!-- plugin元素包含描述插件所需要的信息。 --> 
                 <plugin> 
                     <!-- 插件在仓库里的group ID --> 
                     <groupId></groupId> 
                     <!-- 插件在仓库里的artifact ID --> 
                     <artifactId></artifactId> 
                     <!-- 被使用的插件的版本(或版本范围) --> 
                     <version></version> 
                     <!-- 是否从该插件下载Maven扩展(例如打包和类型处理器),由于性能原因,只有在真需要下载时,该元素才被设置成enabled。 --> 
                     <extensions>true/false</extensions>
    				
                     <!-- 在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。 --> 
                     <executions> 
                         <!-- execution元素包含了插件执行需要的信息 --> 
                         <execution> 
                             <!-- 执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标 --> 
                             <id></id>
                             <!-- 绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段 --> 
                             <phase></phase>
                             <!-- 配置的执行目标 --> 
                             <goals></goals> 
                             <!-- 配置是否被传播到子POM --> 
                             <inherited>true/false</inherited> 
                             <!-- 作为DOM对象的配置 --> 
                             <configuration></configuration>
                         </execution> 
                     </executions> 
                     
                     <!-- 项目引入插件所需要的额外依赖 --> 
                     <dependencies>
                         <!-- 参见dependencies/dependency元素 --> 
                         <dependency></dependency> 
                     </dependencies> 
    
                     <!-- 任何配置是否被传播到子项目 --> 
                     <inherited>true/false</inherited>
                     <!-- 作为DOM对象的配置 --> 
                     <configuration></configuration>
                 </plugin> 
             </plugins> 
         </pluginManagement> 
    

    3.7 - resources

    Build时需要的资源文件

            <!-- 这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。 --> 
            <resources> 
                <!-- 这个元素描述了项目相关或测试相关的所有资源路径 --> 
                <resource> 
                    <!-- 描述了资源的相对路径(相对target/classes目录)。如果只是想把资源放到源码目录结构里,就不需要该配置。 --> 
                    <targetPath></targetPath> 
                    <!-- 是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。 --> 
                    <filtering></filtering>
                    <!-- 描述存放资源的目录,该路径相对POM路径 --> 
                    <directory></directory>
                    <!-- 包含的模式列表,例如**/*.xml. --> 
                    <includes>
                        <include></include>
                    </includes>
                    <!-- 排除的模式列表,例如**/*.xml -->
                    <excludes>
                        <exclude></exclude>
                    </excludes>
                </resource> 
            </resources> 
    

    4-生命周期

    4.1 - 生命周期

    • clean 清理项目
    • default 构建项目
    • site 生成项目站点

    4.2 - 完整的项目构建过程

    清理、编译、测试、打包、集成测试、验证、部署
    clean、compile、test、package、install
    注意:执行某个阶段时,其前面的阶段会顺序执行,但不会触发其他生命周期的另外阶段。

    4.3 - clean的阶段

    • pre-clean 执行清理前的工作
    • clean 清理上一次构建生成的所有文件
    • post-clean 执行清理后的文件

    4.4 - default构建项目(最核心)

    compile、test、package、install

    4.5 - site的阶段

    根据pom文件中信息自动生成站点

    • pre-site 在生成项目站点前要完成的工作
    • site 生成项目的站点文档
    • post-site 在生成项目站点后要完成的工作
    • site-deploy 发布

    5-常用命令

    5.1 - 查看帮助信息

    • mvn --help/-h
    • mvn help:help 显示help插件的帮助信息
    • mvn help:help -Ddetail=true 显示help插件的详细帮助信息
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
    $ mvn help:help
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building testmaven02 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-help-plugin:2.2:help (default-cli) @ testmaven02 ---
    [INFO] Maven Help Plugin 2.2
      The Maven Help plugin provides goals aimed at helping to make sense out of the
      build environment. It includes the ability to view the effective POM and
      settings files, after inheritance and active profiles have been applied, as
      well as a describe a particular plugin goal to give usage information.
    
    This plugin has 9 goals:
    
    help:active-profiles
      Displays a list of the profiles which are currently active for this build.
    
    help:all-profiles
      Displays a list of available profiles under the current project.
      Note: it will list all profiles for a project. If a profile comes up with a
      status inactive then there might be a need to set profile activation
      switches/property.
    
    help:describe
      Displays a list of the attributes for a Maven Plugin and/or goals (aka Mojo -
      Maven plain Old Java Object).
    
    help:effective-pom
      Displays the effective POM as an XML for this build, with the active profiles
      factored in.
    
    help:effective-settings
      Displays the calculated settings as XML for this project, given any profile
      enhancement and the inheritance of the global settings into the user-level
      settings.
    
    help:evaluate
      Evaluates Maven expressions given by the user in an interactive mode.
    
    help:expressions
      Displays the supported Plugin expressions used by Maven.
    
    help:help
      Display help information on maven-help-plugin.
      Call mvn help:help -Ddetail=true -Dgoal=<goal-name> to display parameter
      details.
    
    help:system
      Displays a list of the platform details like system properties and environment
      variables.
    
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.409 s
    [INFO] Finished at: 2017-10-24T14:43:49+08:00
    [INFO] Final Memory: 11M/309M
    [INFO] ------------------------------------------------------------------------
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
    $
    

    5.2 - 查看插件的帮助信息:

    • mvn <plug-in>:help
    • 示例:mvn dependency:help

    5.3 - 使用Maven Help 插件

    mvn help:describe命令可以显示某个插件的信息

    • -Dplugin=pluginName
    • -Dgoal(或-Dmojo)=goalName
    • -Ddetail=true
    mvn help:describe -Dplugin=help    # 使用help插件的describe目标来显示Maven Help插件的信息
    mvn help:describe -Dplugin=help -Dfull    # 显示Maven Help插件所有可用目标
    mvn help:describe -Dplugin=exec -Dfull    # 显示Maven Exec插件所有可用目标
    mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull    # 获取单个目标的信息,显示Maven Compiler插件的compile目标信息
    mvn help:effective-pom    # 查看实际pom信息
    

    5.4 - 常用命令

    • mvn -version/-v 查看maven版本
    • mvn compile 编译源代码
    • mvn test 运行测试
    • mvn test-compile 编译测试代码
    • mvn test -skipping compile -skipping test-compile # 只测试,不编译,也不测试编译;
    • mvn package 打包(生成target目录,编译、测试代码,生成测试报告,生成jar/war文件)
    • mvn package -Dmaven.test.skip=ture 打包时跳过测试
    • mvn install 安装jar到本地Repository中
    • mvn install -X 打开 Maven 的调试标记查看完整的依赖踪迹(包含被拒绝引入的构件)
    • mvn clean 清除产生的项目(删除target目录及文件)
    • mvn clean install 先清除,然后编译安装到本地仓库
    mvn -e    # 显示详细错误信息
    mvn validate    # 验证工程是否正确,所有需要的资源是否可用
    mvn verify    # 运行任何检查,验证包是否有效且达到质量标准
    
    mvn archetype:create -DgroupId=packageName -DartifactId=projectName    # 创建Maven的普通Java项目
    mvn archetype:generate    # 反向生成maven项目的骨架(建议使用,简洁方便,根据提示操作即可)
    
    mvn eclipse:eclipse    # 生成eclipse项目结构(将项目转化为Eclipse项目)
    mvn eclipse:clean    # 清除Eclipse项目结构
    mvn idea:idea    # 生成idea项目结构
    
    mvn site    # 生成站点目录: 
    mvn site-deploy    # 生成站点目录并发布
    
    mvn deploy    # 上传到私有服务器
    
    mvn jar:jar    # 只打jar包
    mvn -Dtest package    # 只打包不测试
    mvn source:jar    # 源码打包
    mvn source:jar-no-fork    # 源码打包
    
    mvn dependency:list    # 显示当前项目已被解析的依赖
    mvn dependency:resolve    # 显示已解决依赖的列表
    mvn dependency:tree    # 显示整个依赖树
    mvn dependency:analyze    # 分析项目的依赖信息,作用等同于mvn dependency:tree
    

    5.5 - 一些示例

    mvn compile 编译

    如果是第一次运行mvn compile等命令时,将会下载很多的第三方和maven所依赖的jar包。
    在Maven项目根目录下,默认生成target目录:

    target             构建过程中的默认生成的临时目录
      target/classes/            存放src/main/java目录下源文件编译出来的字节码文件(.class)
      target/maven-status/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ mvn compile
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building testmaven 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsTestMavensrcmain
    esources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to D:Anliven-RunningenEclipseProjectsTestMaven	argetclasses
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.064 s
    [INFO] Finished at: 2017-10-20T10:56:12+08:00
    [INFO] Final Memory: 15M/292M
    [INFO] ------------------------------------------------------------------------
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l
    total 1
    -rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
    drwxr-xr-x 1 guowli 1049089   0 Oct 19 13:21 src/
    drwxr-xr-x 1 guowli 1049089   0 Oct 20 10:56 target/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l target/
    total 0
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ls -l target/classes/anliven/testmaven01/HelloMaven.class
    -rw-r--r-- 1 guowli 1049089 406 Oct 20 10:56 target/classes/anliven/testmaven01/HelloMaven.class
    

    mvn test 测试

    生成如下目录:
    target/surefire-reports/ 存放生成的测试报告
    target/test-classes/ 存放src/test/java目录下源文件编译出来的字节码文件(.class)

    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l target/
    total 0
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ mvn test
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building testmaven 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsTestMavensrcmain
    esources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsTestMavensrc	est
    esources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to D:Anliven-RunningenEclipseProjectsTestMaven	arget	est-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
    [INFO] Surefire report directory: D:Anliven-RunningenEclipseProjectsTestMaven	argetsurefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running anliven.testmaven01.HelloMavenTest
    Run test!
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.064 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3.286 s
    [INFO] Finished at: 2017-10-20T11:01:06+08:00
    [INFO] Final Memory: 16M/291M
    [INFO] ------------------------------------------------------------------------
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l target/
    total 0
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l target/surefire-reports/
    total 9
    -rw-r--r-- 1 guowli 1049089  282 Oct 20 11:01 anliven.testmaven01.HelloMavenTest.txt
    -rw-r--r-- 1 guowli 1049089 6398 Oct 20 11:01 TEST-anliven.testmaven01.HelloMavenTest.xml
    

    mvn package 打包

    生成如下目录及文件:
    target/maven-archiver/
    target/xxx-y.y.y-zzz.jar 生成的jar包

    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l target/
    total 0
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
    drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ mvn package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building testmaven 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsTestMavensrcmain
    esources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsTestMavensrc	est
    esources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to D:Anliven-RunningenEclipseProjectsTestMaven	arget	est-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
    [INFO] Surefire report directory: D:Anliven-RunningenEclipseProjectsTestMaven	argetsurefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running anliven.testmaven01.HelloMavenTest
    Run test!
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven ---
    [INFO] Building jar: D:Anliven-RunningenEclipseProjectsTestMaven	arget	estmaven-0.0.1-SNAPSHOT.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3.437 s
    [INFO] Finished at: 2017-10-20T11:13:48+08:00
    [INFO] Final Memory: 16M/213M
    [INFO] ------------------------------------------------------------------------
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l target/
    total 4
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 10:56 classes/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 11:13 maven-archiver/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 10:56 maven-status/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 11:01 surefire-reports/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 11:01 test-classes/
    -rw-r--r-- 1 guowli 1049089 2151 Oct 20 11:13 testmaven-0.0.1-SNAPSHOT.jar
    

    mvn clean 删除target目录及文件

    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ll
    total 5
    -rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
    drwxr-xr-x 1 guowli 1049089   0 Oct 19 13:21 src/
    drwxr-xr-x 1 guowli 1049089   0 Oct 20 11:13 target/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ mvn clean
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building testmaven 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
    Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (0 B at 0 B/s)
    Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar
    Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar (0 B at 0 B/s)
    [INFO]
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testmaven ---
    [INFO] Deleting D:Anliven-RunningenEclipseProjectsTestMaven	arget
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3.072 s
    [INFO] Finished at: 2017-10-20T17:13:51+08:00
    [INFO] Final Memory: 11M/245M
    [INFO] ------------------------------------------------------------------------
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l
    total 1
    -rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
    drwxr-xr-x 1 guowli 1049089   0 Oct 19 13:21 src/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $
    

    mvn install 安装jar包到本地仓库中

    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ mvn install
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building testmaven 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom
    Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom (0 B at 0 B/s)
    Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar
    Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar (0 B at 0 B/s)
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsTestMavensrcmain
    esources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to D:Anliven-RunningenEclipseProjectsTestMaven	argetclasses
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsTestMavensrc	est
    esources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to D:Anliven-RunningenEclipseProjectsTestMaven	arget	est-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
    [INFO] Surefire report directory: D:Anliven-RunningenEclipseProjectsTestMaven	argetsurefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running anliven.testmaven01.HelloMavenTest
    Run test!
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven ---
    [INFO] Building jar: D:Anliven-RunningenEclipseProjectsTestMaven	arget	estmaven-0.0.1-SNAPSHOT.jar
    [INFO]
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ testmaven ---
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (2.5 kB at 4.7 kB/s)
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom (19 kB at 24 kB/s)
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (1.1 kB at 1.9 kB/s)
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5.0 kB at 8.9 kB/s)
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom (7.2 kB at 13 kB/s)
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom (7.3 kB at 13 kB/s)
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar
    Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 kB at 9.8 kB/s)
    Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (230 kB at 179 kB/s)
    [INFO] Installing D:Anliven-RunningenEclipseProjectsTestMaven	arget	estmaven-0.0.1-SNAPSHOT.jar to D:DownLoadFilesapache-maven-repoanliven	estmaven01	estmaven.0.1-SNAPSHOT	estmaven-0.0.1-SNAPSHOT.jar
    [INFO] Installing D:Anliven-RunningenEclipseProjectsTestMavenpom.xml to D:DownLoadFilesapache-maven-repoanliven	estmaven01	estmaven.0.1-SNAPSHOT	estmaven-0.0.1-SNAPSHOT.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 9.602 s
    [INFO] Finished at: 2017-10-20T17:14:19+08:00
    [INFO] Final Memory: 18M/210M
    [INFO] ------------------------------------------------------------------------
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l
    total 5
    -rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
    drwxr-xr-x 1 guowli 1049089   0 Oct 19 13:21 src/
    drwxr-xr-x 1 guowli 1049089   0 Oct 20 17:14 target/
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $ ls -l target/
    total 4
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 classes/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 maven-archiver/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 maven-status/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 surefire-reports/
    drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 test-classes/
    -rw-r--r-- 1 guowli 1049089 2150 Oct 20 17:14 testmaven-0.0.1-SNAPSHOT.jar
    
    guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
    $
    
  • 相关阅读:
    爬过的第一个坑
    Ecshop后台邮件群发
    ECShop 首页调用积分商城里面的的商品
    隐藏select右边的箭头按钮
    让IE6支持PNG透明图片
    PHP替换函数,一些正则!
    php判断终端是手机还是电脑访问网站代码
    ECshop在文章列表页调用文章简介
    Ecshop删除no_license点击查看 云登陆失败,您未有license
    Ecshop商品相册鼠标经过小图切换显示大图
  • 原文地址:https://www.cnblogs.com/anliven/p/7956581.html
Copyright © 2011-2022 走看看