Maven 5 分钟入门教程

Maven 5 分钟入门教程
  • 安装
  • Maven是一个Java工具,所以你得先把Java运行时环境安装好才能继续。
    首先, 下载并安装好Maven,然后按下面的指导一步步执行. 在命令行环境下面输入下面的指令:
    mvn --version

    你应该能看到类似下面这样的一些关于Maven的版本信息:
    Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
    Maven home: D:\apache-maven-3.0.3\bin\..
    Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
    Java home: E:\Program Files\Java\jdk1.6.0_25\jre
    Default locale: nl_NL, platform encoding: Cp1252
    OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
    

    鉴于你的网络配置,你可能需要一些其它的配置,你可以到Apache的官方网站上看更详细的配置。 http://maven.apache.org/guides/mini/guide-configuring-maven.html不过入门阶段,默认的配置也就够了。
  • 用命令行创建一个 Project:
  • 找个合适的地方建个文件夹神马的,然后在命令行环境里面cd到这个目录下面来。执行下马的语句。你第一次看不懂不要紧,看着看着你就慢慢的懂了。
    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    如果你刚刚安装Maven,这个命令执行的时间会比较长,因为它会到网上做一些更新,如果你网速不好,那么时间就更久了。所以你就多念几声“阿弥陀佛”,保佑它顺利执行完。如果你人品好的话,那么很快,你就可以在当前文件夹下面看到这么一堆东西了。你可以执行Tree 命令看看生成的目录结构。
    Tree . (仔细看,tree 后面有个小点哦,表示当前目录。)

    my-app
    |-- pom.xml (这个其实是最终于的产物。)
    `-- src
        |-- main
        |   `-- java
        |       `-- com
        |           `-- mycompany
        |               `-- app
        |                   `-- App.java (这个是生存的SourceCode)
        `-- test
            `-- java
                `-- com
                    `-- mycompany
                        `-- app
                            `-- AppTest.java(这个是测试的Code,是不是感觉超牛B,超变态)
    
  • POM配置文件
  • Pom.xml是工程Maven配置文件的核心,它是个包含了主要配置信息的单一文件。通过这个配置文件,你可以安装你想要的方式来编译整个工程。它的复杂性确实有点望而生畏,所以你将来需要花时间去慢慢熟悉它,这样你的工作才能更有效率。
    <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.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>Maven Quick Start Archetype</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.8.2</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
  • 刚才这个命令行做了哪些事情?
  • 这个命令行执行了一个Maven的目标任务(goal) archetype:generate, 我们传了很多参数给这个目标任务。前缀 archetype 表示一个插件. 插件封装了很多目标任务(goal)。这个概念跟Ant里面的类似。你可以到Apache官网了解更多关于插件的信息。 http://maven.apache.org/plugins/index.html这里提供了各种各样的插件来满足不同的需要。每个都够你折腾一段时间。
  • Build the Project
  • cd my-app
    mvn package

    执行这个指令是帮你把项目打包,当然所以打包依赖的任务也都会自动被执行到。这根Ant大同小异。 你可以在控制台看到这样的信息。
     ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2 seconds
    [INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
    [INFO] Final Memory: 3M/6M
    [INFO] ------------------------------------------------------------------------
    

    和前面你执行的那个长长的命令行不同(archetype:generate),你可能已经注意到这个命令行很短,后面跟了个参数 package,这个参数和前面不一样,它不是什么插件,也不是什么目标任务。Maven给它起了个新名字(phase),我们暂时翻译成状态。可以理解为项目的一中状态。当你输入这个指令后,Maven帮你做了下面这些事情:
    • 1.validate
    • 2.generate-sources
    • 3.process-sources
    • 4.generate-resources
    • 5.process-resources
    • 6.compile

    执行 Tree . 命令,你可以看到它生存了新的目录。
    ├─src
    │  ├─main
    │  │  └─java
    │  │      └─com
    │  │          └─mycompany
    │  │              └─app
    │  └─test
    │      └─java
    │          └─com
    │              └─mycompany
    │                  └─app
    └─target
        ├─classes
        │  └─com
        │      └─mycompany
        │          └─app
        ├─maven-archiver
        ├─surefire-reports
        └─test-classes
            └─com
                └─mycompany
                    └─app

    其中还打了个jar包 my-app-1.0-SNAPSHOT.jar
  • Maven Phases(Maven 预定义指令)
  • 下面列举了Maven最常用的一些指令
    • validate: validate the project is correct and all necessary information is available
    • compile: compile the source code of the project
    • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
    • package: take the compiled code and package it in its distributable format, such as a JAR.
    • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
    • verify: run any checks to verify the package is valid and meets quality criteria
    • install: install the package into the local repository, for use as a dependency in other projects locally
    • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
    • clean: cleans up artifacts created by prior builds
    • site: generates site documentation for this project

    对于上面的这些指令,我们建议你每个都执行一下,看看什么效果。对你了解Maven的功能很有帮助。
  • 生成你的项目站点
  • mvn site

    这个比较有意思,会帮你的Project生存一个站点。
    This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.
    如果你有兴趣的话,可以到官网了解更多的信息。
    http://maven.apache.org/guides/getting-started/index.html

    猜你喜欢

    转载自ieqq.iteye.com/blog/1364149
    今日推荐