maven-war-plugin_maven-war-plugin

來源:互聯網
上載者:User
Apache Maven WAR Plugin

The WAR Plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive.

“打包“這個詞聽起來比較土,比較正式的說法應該是”構建項目軟體包“,具體說就是將項目中的各種檔案,比如原始碼、編譯產生的位元組碼、設定檔、文檔,按照規範的格式產生歸檔,最常見的當然就是JAR包和WAR包了,複雜點的例子是Maven官方下載頁面的分發包,它有自訂的格式,方便使用者直接解壓後就在命令列使用。作為一款”打包工具“,Maven自然有義務協助使用者建立各種各樣的包,規範的JAR包和WAR包自然不再話下,略微複雜的自訂打包格式也必須支援,本文就介紹一些常用的打包案例以及相關的實現方式,除了前面提到的一些包以外,你還能看到如何產生源碼包、Javadoc包、以及從命令列可直接啟動並執行CLI包。

任何一個Maven項目都需要定義POM元素packaging(如果不寫則預設值為jar)。顧名思義,該元素決定了項目的打包方式。實際的情形中,如果你不聲明該元素,Maven會幫你產生一個JAR包;如果你定義該元素的值為war,那你會得到一個WAR包;如果定義其值為POM(比如是一個父模組),那什麼包都不會產生。除此之外,Maven預設還支援一些其他的流行打包格式,例如ejb3和ear。你不需要瞭解具體的打包細節,你所需要做的就是告訴Maven,”我是個什麼類型的項目“,這就是約定優於配置的力量。

對應於同樣的package生命週期階段,Maven為jar項目調用了maven-jar-plugin,為war項目調用了maven-war-plugin,換言之,packaging直接影響Maven的構建生命週期。瞭解這一點非常重要,特別是當你需要自訂打包行為的時候,你就必須知道去配置哪個外掛程式。 war plugin的常用配置參數 archiveClasses配置項

該配置的值為true|false,預設是false。表示是否將class進行打包。
正常情況下war類型的工程,java代碼編譯後的類檔案會放到WEB-INF/classes目錄下面,散裝。 當該參數配置為true時,會將所有的class打包為一個jar,jar的名字與war的名字一致(除了尾碼)。然後把這個jar放到WEB-INF/lib目錄下,此時WEB-INF/classes目錄下是空的。 attachClasses配置項

該配置的值為true|false,預設是false。表示在發布war包的時候是否同時發布一個jar包(只有classes,不包含頁面相關檔案)。
正常情況下war類型的工程,當我們執行install或者deploy的時候build出一個war包,安裝到本地或者發布到遠程。
當該參數配置為true時,除了war包外還會多出一個jar包,不過該jar包的classifier預設為classes。 overlays配置節點

overlays配置的作用是,將指定war包中的內容與當前項目進行合并。合并策略:如果存在同名衝突,則使用當前項目中的檔案。 overlay的具體配置項(include|exclude)可以指定包含或者排除特定模式的檔案。 配置樣本 多war合并

archiveClasses和attachClasses參數可以同時配置為true。此時打包檔案中含有lib,不含classes。發布時會同時發布classifier為classes的jar包。

如果當前工程A需要從B工程的war包中合入分頁檔,同時代碼中也要使用B的類檔案。工程B包含配置如下:

 1 2 3 4 5 6 7 8 
 <plugin> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-war-plugin </artifactId> <version>2.6 </version> <configuration> <attachClasses>true </attachClasses> </configuration> </plugin> 

工程A包含配置如下:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 
 <dependencies> <dependency> <groupId>groupB </groupId> <artifactId>B </artifactId> <version>1.0-SNAPSHOT </version> <classifier>classes </classifier> <scope>provided </scope> </dependency> <dependency> <groupId>groupB </groupId> <artifactId>B </artifactId> <version>1.0-SNAPSHOT </version> <type>war </type> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-war-plugin </artifactId> <version>2.6 </version> <configuration> <overlays> <overlay> <groupId>groupB </groupId> <artifactId>B </artifactId> </overlay> </overlays> </configuration> </plugin> </plugins> </build> 

可注意如果同名的檔案夾會進行合并,不合并空檔案夾。 把class檔案打包成jar,怎麼排除resources目錄?

正常情況下war類型的工程,java代碼編譯後的類檔案會放到WEB-INF/classes目錄下面,散裝。 當archiveClasses參數配置為true時,會將所有的class打包為一個jar,jar的名字與war的名字一致。然後把這個jar放到WEB-INF/lib目錄下,此時WEB-INF/classes目錄下是空的,但如果想在war包的WEB-INF/classes中保留logback.xml設定檔,該怎麼做。

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
 <plugin> <artifactId>maven-war-plugin </artifactId> <version> 3.0 .0 </version> <configuration> <archiveClasses>true </archiveClasses> <webResources> <resource> <directory>src /main/resources </directory> <targetPath>WEB-INF/classes </targetPath> <filtering>true </filtering> </resource> </webResources> </configuration> </plugin> 對項目進行動態封裝,不同的環境使用不同的設定檔打包 demo 1 
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
 project/ `-- src | -- main | | -- java | | -- resources | | | -- local | | | | -- logback.xml | | | | -- spring-dataSource.xml | | | `-- variable.propertes | | | -- product | | | | -- logback.xml | | | | -- spring-dataSource.xml | | | `-- variable.propertes | | `-- qa | | | -- logback.xml | | | -- spring-dataSource.xml | | `-- variable.propertes | | -- webapp `-- test 
 1 2 
 maven package –P local 開發環境 maven package –P product 生產環境 
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 
 <profiles> <profile> <id>local </id> <properties> <package.environment>local </package.environment> </properties> <activation> <activeByDefault>true </activeByDefault> </activation> </profile> <profile> <id>product </id> <properties> <package.environment>product </package.environment> </properties> </profile> </profiles> <build> <resources> <!-- class檔案打包成jar包,排除相關設定檔和jrebel設定檔 --> <resource> <directory>src/main/resources </directory> <excludes> <exclude>local/* </exclude> <exclude>product/* </exclude> <exclude>rebel.xml </exclude> </excludes> <filtering>true </filtering> </resource> </resources> <plugins> <!-- 區別dev環境與生產環境logback.xml配置 --> <plugin> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-war-plugin </artifactId> <configuration> <archiveClasses>true </archiveClasses> <failOnMissingWebXml>false </failOnMissingWebXml> <webResources> <resource> <directory>src/main/resources/$ {package.environment} </directory> <filtering>true </filtering> <targetPath>WEB-INF/classes </targetPath> </resource> </webResources> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-jar-plugin </artifactId> <executions> <execution> <id>default-jar </id> <phase>package </phase> <goals> <goal>jar </goal> </goals> </execution> </executions> </plugin> </plugins> </build> demo 2 
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
 project/ `-- src | -- main | | -- java | | -- env | | | -- dev | | | | -- log4j.properties | | | | -- spring-dataSource.xml | | | `-- variable.propertes | | | -- prod | | | | -- log4j.properties | | | | -- spring-dataSource.xml | | | `-- variable.propertes | | `-- qa | | | -- log4j.properties | | | -- spring-dataSource.xml | | `-- variable.propertes | | -- resources | `-- webapp `-- test 
 1 2 
 mvn clean package -P dev mvn clean package -P qa 
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 
 <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> <packaging>war </packaging> <version>0.0.1-SNAPSHOT </version> <name>demo Maven Webapp </name> <groupId>webapp </groupId> <artifactId>webapp </artifactId> <scm> <connection>scm:svn:http://127.0.0.1/dummy </connection> <developerConnection>scm:svn:https://127.0.0.1/dummy </developerConnection> <tag>HEAD </tag> <url>http://127.0.0.1/dummy </url> </scm> <properties> <spring-version>3.1.0.RELEASE </spring-version> </properties> <build> <finalName>$ {final.name} </finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-war-plugin </artifactId> <version>2.1.1 </version> <configuration> <overlays> <overlay> <groupId>TransactionResource </groupId> <artifactId>TransactionResource </artifactId> <excludes> <exclude>WEB-INF/web.xml </exclude> </excludes> </overlay> </overlays> <webResources> <resource> <directory>$ {runtime.env} </directory> <targetPath>WEB-INF/classes </targetPath> </resource> </webResources> </configuration> </plugin> </plugins> </build> <dependencies> …… </dependencies> <profiles> <profile> <id>dev </id> <activation> <activeByDefault>true </activeByDefault> </activation> <properties> <runtime.env>src/main/env/dev </runtime.env> <final.name>webapp </final.name> </properties> <dependencies> <dependency> <groupId>com.eightqiu </groupId> <artifactId>CodeCmns </artifactId> <version>0.0.1-SNAPSHOT </version> </dependency> </dependencies> </profile> <profile> <id>qa </id> <properties> <runtime.env>src/main/env/qa </runtime.env> <final.name>webapp_$ {buildNumber} </final.name> </properties> <build> <plugins> <plugin> <groupId>org.codehaus.mojo </groupId> <artifactId>buildnumber-maven-plugin </artifactId> <version>1.1 </version> <executions> <execution> <phase>validate </phase> <goals> <goal>create </goal> </goals> </execution> </executions> <configuration> <format> {0,date,yyyyMMdd} </format> <items> <item>timestamp </item> </items> </configuration> </plugin> </plugins> </build> <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-javadoc-plugin </artifactId> <version>2.8.1 </version> </plugin> </plugins> </reporting> <dependencies> <dependency> <groupId>com.eightqiu </groupId> <artifactId>CodeCmns </artifactId> <version>0.0.1-SNAPSHOT </version> <scope>provided </scope> </dependency> </dependencies> </profile> <profile> <id>prod </id> <properties> <runtime.env>src/main/env/prod </runtime.env> <final.name>webapp </final.name> </properties> <dependencies> <dependency> <groupId>com.eightqiu </groupId> <artifactId>CodeCmns </artifactId> <version>0.0.1-SNAPSHOT </version> <scope>provided </scope> </dependency> </dependencies> </profile> </profiles> </project>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.