Maven建立项目

  • 建立一个指定目录:例如:D:\maven,运行cmd并进入该目录,执行以下脚本本:
    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    若是刚安装maven,初次运行时,可能会花费一些时间。由于maven会下载最近的构件(artifacts:构件?)到本地仓库,该构件包括(plugin jars and other files)。因为远程服务器可能会下载超时,因此可能须要执行屡次以上命令,直到执行成功。
    以下图:


    在maven目录下生成一个与artifactId同名的目录,进行该目录
    cd my-app
    运行tree -F 可查看该目录结构:
    以上为maven 建立的标准目录结构。
    main/java 目录:存放源码
    test/java目录:存放测试源码
    pox.xml:POM(Project Object Model)
  • POM
    在maven中,pox.xml文件是项目配置的核心。这个配置文件包含的所需build项目的大多数信息。POM是巨大并且很是复杂,无需了解全部的配置。以上项目的POM:
    <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/maven-v4_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>my-app</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
  • Build项目
    在my-app目录下,运行:
    mvn package
    将打印以下信息:
    Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are:
    1.validate
    2.generate-sources
    3.process-sources
    4.generate-resources
    5.process-resources
    6.compile
    test最近编译和打包的jar:
    java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
    输出:
    Hello World!
  • Maven 阶段(Phases)
    虽然没乎没有一个全面的列表,这些都是最多见的默认执行的生命周期阶段:
    validate:  验证项目是正确的而且全部的必要信息是有效的
    compile:编译项目的源码
    test:测试程序源代,使用合适的的单元测试仪框架,这些测试代码应该不须要打包或部署
    package:编译源码并按分派格式打包,如打包成jar文件
    integration-test:过程和部署包若是有必要到环境中,集成测试能够运行
    verify:运行任务检查,验证包的有效性和符合质量标准
    install:安装包到本地仓库,做为你本机的基他项目前的依赖项
    deploy:在集成电路或发布的环境下,复制最终的包到远程仓库,共享给其余的开发者和项目。

    因为英语水平不高,以避免形成理解上的错误,将原文附上:
    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.
    还有另外两个Maven lifecycles: clean:清理以前创建的artifacts site:生成站点文档为该项目 阶段其实是被映射到相关的目标(underlying goals),挂靠每一个阶段的具体目标依赖于项目的包类型。 For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is - you guessed it - a WAR.                 
相关文章
相关标签/搜索