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.                 
相關文章
相關標籤/搜索