Maven 是怎樣建立War 包?

      最近在網上看到一篇介紹maven基礎知識的文章,以爲對初學Maven的朋友必定有幫助。水平有限,翻譯的很差,請你們見諒。 java

介紹

      在處理WEB應用的時候,最終使用的工程文件是以War包的形式交付。Maven編譯系統能夠輕鬆的建立War包。接下來就讓咱們看看Maven是如何把一個源文件的工程轉換成War包的。 web

Maven 版本 Apache Maven 3.0.4 shell

        

工程實例

       讓咱們來看看這個很是典型的Maven化的WEB工程 apache

                             

對應的POM.xml以下: app

<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>mygroup.com</groupId>
  <artifactId>myprojectname</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>myprojectname Maven Webapp</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>
  <build>
    <finalName>myprojectname</finalName>
  </build>
</project>

 咱們用此命令War包  webapp

mvn package
C:\Projects\myprojectname>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myprojectname Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
...
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ myprojectname ---
[INFO] Packaging webapp
[INFO] Assembling webapp [myprojectname] in [C:\Projects\myprojectname\target\myprojectname]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Projects\myprojectname\src\main\webapp]
[INFO] Webapp assembled in [18 msecs]
[INFO] Building war: C:\Projects\target\myprojectname.war
...

 War生成在根目錄下 maven

/target/myprojectname.war

以下圖,概況Maven生成War包過程 post

 

Maven 默認配置

     咱們都知道Maven能夠很容易的把源文件工程建立爲War包,可是POM文件中什麼也沒有設置。這是怎麼回事啊?實際上Maven有本身默認的設置。這稱之爲 「convention over configuration」,Maven在配置中提供默認值。 單元測試

 第一,由於有一些Maven插件自己就與Maven 的生命週期綁定在一塊兒。例如,在編譯階段使用 測試

compiler:compile 做爲默認命令。這就意味着當執行到編譯階段compiler plugin 被調用執行。若是選擇生成WAR,那麼 war:war 會與這個階段綁定。

第二,當沒有明確設置參數的時候,插件都會有本身默認值。例如 compiler:compile 目標有個參數是 compilerId。當默認值是 javac就意味着JDK 會被使用。當須要生成其餘形式時能夠重寫此配置。

第三,一些設置包含在 Super POM,此文件是POM文件默認繼承的。從Mavne3 起 Super POM 被放在

maven_dir/lib/maven-model-builder-3.0.3.jar:org/apache/maven/model/pom-4.0.0.xml

 在這裏咱們能夠發現不少默認的配置信息

<build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
  ...
  </build>

 Maven 生命週期

     在咱們的工程中,當執行 mvn package 命令,maven會執行它整個生命週期中的六個階段

process-resources, compile, process-test-resources, test-compile, test and package

每一個階段會包含一個或多個目標。Maven 插件提供目標:一個插件能夠有一個或多個目標。例如

Compiler 插件有兩個目標:compiler:compile  和 compiler:testCompile

咱們可使用 mvn help:describe -Dcmd=phasename 命令列出以下內容

C:\Project\myprojectname>mvn help:describe -Dcmd=package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myprojectname Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ myprojectname ---
[INFO] 'package' is a phase corresponding to this plugin:
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
 
It is a part of the lifecycle for the POM packaging 'war'. This lifecycle includes the following phases:
* validate: Not defined
* initialize: Not defined
* generate-sources: Not defined
* process-sources: Not defined
* generate-resources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
* process-classes: Not defined
* generate-test-sources: Not defined
* process-test-sources: Not defined
* generate-test-resources: Not defined
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:2.10:test
* prepare-package: Not defined
* package: org.apache.maven.plugins:maven-war-plugin:2.1.1:war
* pre-integration-test: Not defined
* integration-test: Not defined
* post-integration-test: Not defined
* verify: Not defined
* install: org.apache.maven.plugins:maven-install-plugin:2.3.1:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.496s
[INFO] Finished at: Sat May 12 04:30:35 CEST 2012
[INFO] Final Memory: 5M/121M
[INFO] ------------------------------------------------------------------------

 下面讓咱們看看每一個目標

1.resources:resources

   此目標用來將資源文件夾下的內容拷貝到輸出目錄

 2.compiler:compile

     此目標編譯源項目工程

3.resources:testResources

    此目標拷貝測試資源到測試輸出目錄

4.compiler:testCompile

       此目標編譯測試項目

5.surefire:test


      此目標執行工程的單元測試,編譯的測試類放在 /target/test-classes             

6.war:war

       此目錄建立War包。它會把全部須要的文件放在

/target/myprojectname/
       然後將他們打包生成 **.war。其中一個步驟是將   /src/main/webapp/   拷貝到輸出目錄。                                   

War插件另一個重要步驟是拷貝Class文件到到 WEB-INF/classes目錄和項目所依賴的jar包到 WEB-INF/lib目錄。

默認狀況下,插件還包含兩個Maven描述文件:

  1. META-INF/maven/${groupId}/${artifactId}/pom.xml
  2. pom.properties 文件,META-INF/maven/${groupId}/${artifactId}/pom.properties

#Generated by Maven #Sat May 12 00:50:42 CEST 2012 version=1.0-SNAPSHOT groupId=mygroup.com artifactId=myprojectname

最終的War包放在/target/目錄下。

項目依賴

      pom.xml文件會有一個默認的(JUnit)依賴。咱們能夠加另一個經常使用的Jar  log4j。 

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>

當沒有設置依賴範圍 scope),默認爲 compile scope。這就意味着此依賴在編譯、測試、運行階段均可以獲得。

只要是運行中會用到的jar包,都會拷貝到 /WEB-INF/lib目錄

相關文章
相關標籤/搜索