• Apache Maven(一):快速入门


    Maven 是什么?

    Maven 是一个项目管理和整合工具。Maven 为开发者提供了一套完整的构建生命周期框架。开发团队几乎不用花多少时间就能够自动完成工程的基础构建配置,因为 Maven 使用了一个标准的目录结构和一个默认的构建生命周期。

    在有多个开发团队环境的情况下,Maven 能够在很短的时间内使得每项工作都按照标准进行。因为大部分的工程配置操作都非常简单并且可复用,在创建报告、检查、构建和测试自动配置时,Maven 可以让开发者的工作变得更简单。

    Maven 安装

    这里介绍一下在Windows 系统上安装Maven。注:Maven 3.3+ 需要JDK 1.7或更高版本才能执行

    Maven 的下载地址:官方下载 

    我们将下载下来的zip文件,解压到本地磁盘目录,然后配置环境变量。

    环境变量配置(windows7为例):

    1.添加M2_HOME 和 MAVEN_HOME 环境变量。注:Maven 说只是添加 M2_HOME , 但一些项目仍引用 Maven 的文件夹 MAVEN_HOME, 因此,为了安全也把它添加进去

    2.更新Path变量的变量值。在最后追加

    命令:mvn -v 或 mvn -version: 查看Maven版本信息,并确定是否安装成。

    C:UsersAdministrator>mvn -v
    Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:0
    5+08:00)
    Maven home: D:apache-maven-3.5.3in..
    Java version: 1.8.0_161, vendor: Oracle Corporation
    Java home: D:DevSoftjdk1.8.0_161
    Default locale: zh_CN, platform encoding: GBK
    OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

    如果你看到类似消息,说明 Apache Maven 在 Windows 上已安装成功。

    第一个Maven项目

    可以在项目目录下执行如下命令:会在你的项目路径下生成一个名称为hello的Maven项目。

    mvn archetype:generate -DgroupId=com.cnblogs.hello -DartifactId=hello -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

     hello目录结构如下图:

    所有的源代码放在文件夹 /src/main/java/, 所有的单元测试代码放入 /src/test/java/。

    pom.xml文件内容:该文件是在Maven项目中的核心配置文件。它是一个单独的配置文件,其中包含以您构建项目所需的大部分信息。后面我会详细介绍这个文件。

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     3   <modelVersion>4.0.0</modelVersion>
     4   <groupId>com.cnblogs.hello</groupId>
     5   <artifactId>hello</artifactId>
     6   <packaging>jar</packaging>
     7   <version>1.0-SNAPSHOT</version>
     8   <name>hello</name>
     9   <url>http://maven.apache.org</url>
    10   <dependencies>
    11     <dependency>
    12       <groupId>junit</groupId>
    13       <artifactId>junit</artifactId>
    14       <version>3.8.1</version>
    15       <scope>test</scope>
    16     </dependency>
    17   </dependencies>
    18 </project>

    接下来执行命令:mvn package 将项目进行打包处理。注:这个命令必须在hello项目木木下执行即:D:MavenWorkhello目录

    D:MavenWorkhello>mvn package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ----------------------< com.cnblogs.hello:hello >-----------------------
    [INFO] Building hello 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
    . build is platform dependent!
    [INFO] skip non existing resourceDirectory D:MavenWorkhellosrcmain
    esources
    
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
    d is platform dependent!
    [INFO] Compiling 1 source file to D:MavenWorkhello	argetclasses
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
    llo ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
    . build is platform dependent!
    [INFO] skip non existing resourceDirectory D:MavenWorkhellosrc	est
    esources
    
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello -
    --
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
    d is platform dependent!
    [INFO] Compiling 1 source file to D:MavenWorkhello	arget	est-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello ---
    [INFO] Surefire report directory: D:MavenWorkhello	argetsurefire-reports
    Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
    rg/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom
    Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
    g/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom (1.7 k
    B at 740 B/s)
    Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
    rg/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
    
    Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
    g/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
    (2.3 kB at 1.0 kB/s)
    Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
    rg/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar
    Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
    g/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar (26 kB
     at 10 kB/s)
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.cnblogs.hello.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello ---
    [INFO] Building jar: D:MavenWorkhello	argethello-1.0-SNAPSHOT.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 10.482 s
    [INFO] Finished at: 2018-06-15T14:46:34+08:00
    [INFO] ------------------------------------------------------------------------

    最后执行java命令进行测试打包生成的jar

    java -cp target/hello-1.0-SNAPSHOT.jar com.cnblogs.hello.App

    结果打印如下:

    Hello World!

    最后生成站点,执行命令:mvn site

    mvn site

    执行命令会会根据项目的信息生成一个网站。您可以查看target/site下生成的文档

    通过快速入门我可以简单了解Maven的一些功能,这只是一个快速入门指南。后续几章会有更全面的详细介绍。

  • 相关阅读:
    centos 安装 py 3.0+
    ubuntu下安装多版本Python
    DRF之注册器响应器分页器
    头部随着滚动高度的变化由透明慢慢变成不透明
    悬浮滚动
    判断某天是周几
    正则限制input只能输入大于0的数字
    原生js倒计时
    从两个时间段里分别计算出有几天工作日与周末
    sublime text3连续打出1到10的标签div
  • 原文地址:https://www.cnblogs.com/peter1018/p/9183728.html
Copyright © 2020-2023  润新知