Maven 項目打包需要注意到的那點事兒

1. 關(guān)于 Maven 打 war 包

使用 Eclipse 的 Maven 2 插件開發(fā)一個 JEE 項目》詳細介紹了如何在 Eclipse 使用 Maven 新建一個 JEE 項目并對其進行斷點跟蹤調(diào)試揍堰,但是沒有介紹如何對 JEE 項目打 war 包护奈。其實很簡單撵儿,你只需要把 pom.xml 中的 <packaging>jar</packaging> 換成 <packaging>war</packaging> 就可以使用 mvn package 命令對其打 war 包了芽腾,而不需要添加任何 maven 插件。只要你遵循了 maven 規(guī)范(比如照著《使用 Eclipse 的 Maven 2 插件開發(fā)一個 JEE 項目》所述做了)峦朗,那你打成的 war 包就肯定包含了第三方依賴包:

maven打的war包里邊的第三方依賴包

把這個 war 包丟進 tomcat 的 webapps 目錄份乒,重啟 tomcat 即可完成了該項目的部署。你唯一需要注意的是椰憋,在重啟 tomcat 之前把你的 war 重命名為 項目訪問路徑.war厅克。比如作者打成的 war 包是為 swifton-1.0.0.war,對該項目定義的訪問路徑是 /swifton橙依,那么我在重啟 tomcat 之前需要將其重命名為 swifton.war证舟。

2. 可執(zhí)行程序打 jar 包

關(guān)于可執(zhí)行程序(需要指定一個 main 類)打 jar 包就沒這么方便了,我們需要考慮以下幾個問題:

  • 配置文件需要打進 jar 包窗骑;
  • 需要指定 main 入口類女责;
  • 所依賴的第三方庫也要打進 jar 包;

只有同時滿足以上三點创译,我們才可以直接使用 java -jar swiftonrsa-1.0.0.jar 命令成功執(zhí)行該程序抵知。
為了讓討論不那么抽象,我們在 Eclipse 下新建一個 maven 項目 swiftonrsa:


新建maven項目swiftonrsa

其中,com.defonds.RsaEncryptor 是入口 main 類辛藻,其源碼如下:

package com.defonds;  
 
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
import com.defonds.service.LinkPayAntharService;  
  
public class RsaEncryptor {  
  
    public static void main(String[] args) {  
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
        LinkPayAntharService linkPayAntharService = (LinkPayAntharService) context.getBean("linkPayAntharService");  
        linkPayAntharService.dealWithYearData();  
    }  
}  

2.1 配置文件打包不需要額外關(guān)注

只要你項目所依賴的配置文件都按照 maven 規(guī)范放對位置(src/main/resources)碘橘,那么打好的 jar 包就會把它們一起打包:


配置文件打包不需要額外關(guān)注

但是這樣打好的 jar 包既沒有指定 main 入口類,也沒有將依賴包打進來吱肌,我們運行它:


swiftonrsa-1.0.0.jar中沒有清單屬性

提示"swiftonrsa-1.0.0.jar中沒有主清單屬性"痘拆,我們查看打好 jar 包下 META-INF 目錄中的 MANIFEST.MF,其內(nèi)容如下:
Manifest-Version: 1.0
Built-By: Defonds
Build-Jdk: 1.7.0_67
Created-By: Apache Maven 3.2.3
Archiver-Version: Plexus Archiver
確實沒有指出 main 入口類氮墨。

2.2 maven-assembly-plugin 插件

于是我們引入了 maven-assembly-plugin 插件纺蛆,pom.xml 中加入如下代碼:

<build>  
    <plugins>  
        <plugin>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <configuration>  
                <appendAssemblyId>false</appendAssemblyId>  
                <descriptorRefs>  
                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                </descriptorRefs>  
                <archive>  
                    <manifest>  
                        <mainClass>com.defonds.RsaEncryptor</mainClass>  
                    </manifest>  
                </archive>  
            </configuration>  
            <executions>  
                <execution>  
                    <id>make-assembly</id>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>assembly</goal>  
                    </goals>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>  

執(zhí)行 mvn assembly:assembly,成功構(gòu)建 swiftonrsa-1.0.0.jar规揪,查看其打包目錄桥氏,各種配置文件以及第三方依賴包也都有:


assembly打包后的目錄結(jié)構(gòu)

然后查看 META-INF 目錄中的 MANIFEST.MF,內(nèi)容如下:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Defonds
Build-Jdk: 1.7.0_67
Main-Class: com.defonds.RsaEncryptor
懷著興奮的心情執(zhí)行之:


assembly打包后的執(zhí)行情況

maven-assembly-plugin 插件沒有給我們帶來驚喜猛铅。錯誤信息如下:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
原來這是 assembly 插件的一個 bug:http://jira.codehaus.org/browse/MASSEMBLY-360字支,它在對第三方打包時,對于 META-INF 下的 spring.handlers奸忽,spring.schemas 等多個同名文件進行了覆蓋堕伪,遺漏掉了一些版本的 xsd 本地映射。

2.3 maven-shade-plugin 插件

有破必有立栗菜。http://jira.codehaus.org/browse/MASSEMBLY-360 跟帖中有網(wǎng)友推薦了 maven-shade-plugin 插件欠雌。于是我們使用 maven-shade-plugin 將 maven-assembly-plugin 替代:

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-shade-plugin</artifactId>  
            <version>1.4</version>  
            <executions>  
                <execution>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>shade</goal>  
                    </goals>  
                    <configuration>  
                        <transformers>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                <mainClass>com.defonds.RsaEncryptor</mainClass>  
                            </transformer>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.handlers</resource>  
                            </transformer>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.schemas</resource>  
                            </transformer>  
                        </transformers>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>  

對于多個第三方包 META-INF 下的同名的 spring.handlers 文件它采取的態(tài)度是追加而不是覆蓋。執(zhí)行 maven clean package疙筹,成功構(gòu)建 swiftonrsa-1.0.0.jar富俄,查看其打包目錄,各種配置文件以及第三方依賴包也都有而咆,以及 META-INF 目錄中的 MANIFEST.MF 的內(nèi)容霍比,基本如 maven-assembly-plugin 打包后的樣子,執(zhí)行之:


shade插件打包后執(zhí)行情況

錯誤信息如下:
java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
這是由于一些包重復(fù)引用翘盖,打包后的 META-INF 目錄多出了一些 *.SF 等文件所致桂塞。
有破必有立。博客 http://zhentao-li.blogspot.com/2012/06/maven-shade-plugin-invalid-signature.html 給出了解決方案馍驯,pom.xml 添加:

<configuration>  
  <filters>  
    <filter>  
      <artifact>*:*</artifact>  
      <excludes>  
        <exclude>META-INF/*.SF</exclude>  
        <exclude>META-INF/*.DSA</exclude>  
        <exclude>META-INF/*.RSA</exclude>  
      </excludes>  
    </filter>  
  </filters>  
</configuration>  

于是我們對 maven-shade-plugin 的配置變成這樣:

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-shade-plugin</artifactId>  
            <version>1.4</version>  
            <executions>  
                <execution>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>shade</goal>  
                    </goals>  
                    <configuration>  
                        <filters>  
                            <filter>  
                                <artifact>*:*</artifact>  
                                <excludes>  
                                    <exclude>META-INF/*.SF</exclude>  
                                    <exclude>META-INF/*.DSA</exclude>  
                                    <exclude>META-INF/*.RSA</exclude>  
                                </excludes>  
                            </filter>  
                        </filters>  
                        <transformers>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                <mainClass>com.defonds.RsaEncryptor</mainClass>  
                            </transformer>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.handlers</resource>  
                            </transformer>  
                            <transformer  
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.schemas</resource>  
                            </transformer>  
                        </transformers>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>  

再次執(zhí)行 maven clean package阁危,再次執(zhí)行成功構(gòu)建后的 swiftonrsa-1.0.0.jar:


再次執(zhí)行shade插件打包后的swiftonrsa

最后兩行是具體業(yè)務(wù)實現(xiàn)類 com.defonds.service.LinkPayAntharServiceImpl 成功執(zhí)行打印出的 log 日志。

2.4 示例項目

本文示例項目 swiftonrsa 已上傳至 CSDN 資源汰瘫,有興趣的朋友可以下載下來參考實驗狂打,下載地址:http://download.csdn.net/detail/defonds/8404739
本文示例項目最終 pom.xml 如下:

<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>settle</groupId>  
    <artifactId>swiftonrsa</artifactId>  
    <version>1.0.0</version>  
    <packaging>jar</packaging>  
  
    <name>swiftonrsa</name>  
    <url>http://maven.apache.org</url>  
  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-shade-plugin</artifactId>  
                <version>1.4</version>  
                <executions>  
                    <execution>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>shade</goal>  
                        </goals>  
                        <configuration>  
                            <filters>  
                                <filter>  
                                    <artifact>*:*</artifact>  
                                    <excludes>  
                                        <exclude>META-INF/*.SF</exclude>  
                                        <exclude>META-INF/*.DSA</exclude>  
                                        <exclude>META-INF/*.RSA</exclude>  
                                    </excludes>  
                                </filter>  
                            </filters>  
                            <transformers>  
                                <transformer  
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                    <mainClass>com.defonds.RsaEncryptor</mainClass>  
                                </transformer>  
                                <transformer  
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                    <resource>META-INF/spring.handlers</resource>  
                                </transformer>  
                                <transformer  
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                    <resource>META-INF/spring.schemas</resource>  
                                </transformer>  
                            </transformers>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>  
        </plugins>  
    </build>  
  
  
    <dependencies>  
  
        <!-- logs -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.17</version>  
        </dependency>  
  
        <dependency>  
            <groupId>commons-logging</groupId>  
            <artifactId>commons-logging</artifactId>  
            <version>1.2</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>1.7.10</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>1.7.10</version>  
        </dependency>  
  
        <!-- spring -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>3.2.3.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-orm</artifactId>  
            <version>3.2.3.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>3.2.3.RELEASE</version>  
        </dependency>  
  
        <!-- ibatis -->  
        <dependency>  
            <groupId>org.apache.ibatis</groupId>  
            <artifactId>ibatis-sqlmap</artifactId>  
            <version>2.3.4.726</version>  
        </dependency>  
  
        <!-- connector -->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>5.1.19</version>  
        </dependency>  
  
        <!-- dbcp -->  
        <dependency>  
            <groupId>commons-dbcp</groupId>  
            <artifactId>commons-dbcp</artifactId>  
            <version>1.4</version>  
        </dependency>  
  
        <dependency>  
            <groupId>commons-pool</groupId>  
            <artifactId>commons-pool</artifactId>  
            <version>1.6</version>  
        </dependency>  
  
        <!-- rsa encrypt -->  
        <dependency>  
            <groupId>org.bouncycastle</groupId>  
            <artifactId>bcprov-jdk15on</artifactId>  
            <version>1.51</version>  
        </dependency>  
  
        <!-- commons-codec -->  
        <dependency>  
            <groupId>commons-codec</groupId>  
            <artifactId>commons-codec</artifactId>  
            <version>1.6</version>  
        </dependency>  
  
    </dependencies>  
</project>  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末混弥,一起剝皮案震驚了整個濱河市趴乡,隨后出現(xiàn)的幾起案子对省,更是在濱河造成了極大的恐慌,老刑警劉巖晾捏,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蒿涎,死亡現(xiàn)場離奇詭異,居然都是意外死亡惦辛,警方通過查閱死者的電腦和手機劳秋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來胖齐,“玉大人玻淑,你說我怎么就攤上這事⊙交铮” “怎么了补履?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長剿另。 經(jīng)常有香客問我箫锤,道長,這世上最難降的妖魔是什么驰弄? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任麻汰,我火速辦了婚禮速客,結(jié)果婚禮上戚篙,老公的妹妹穿的比我還像新娘。我一直安慰自己溺职,他們只是感情好岔擂,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著浪耘,像睡著了一般乱灵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上七冲,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天痛倚,我揣著相機與錄音,去河邊找鬼澜躺。 笑死蝉稳,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的掘鄙。 我是一名探鬼主播耘戚,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼操漠!你這毒婦竟也來了收津?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎撞秋,沒想到半個月后长捧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡吻贿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年唆姐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片廓八。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡奉芦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剧蹂,到底是詐尸還是另有隱情声功,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布宠叼,位于F島的核電站先巴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏冒冬。R本人自食惡果不足惜伸蚯,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望简烤。 院中可真熱鬧剂邮,春花似錦、人聲如沸横侦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枉侧。三九已至引瀑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間榨馁,已是汗流浹背憨栽。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留翼虫,地道東北人屑柔。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像蛙讥,于是被迫代替她去往敵國和親锯蛀。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,855評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理次慢,服務(wù)發(fā)現(xiàn)旁涤,斷路器翔曲,智...
    卡卡羅2017閱讀 134,702評論 18 139
  • [TOC] 概述 在項目實踐過程中,有個需求需要做一個引擎能執(zhí)行指定jar包的指定main方法劈愚。 起初我們以一個簡...
    heyikan閱讀 4,692評論 0 4
  • 我們先明確下Packaging的含義(部分內(nèi)容轉(zhuǎn)載自:Maven實戰(zhàn)(九)——打包的技巧) 任何一個Maven項目...
    上課睡覺覺閱讀 10,837評論 0 52
  • 1.編寫POM Maven項目的核心文件是pom.xml瞳遍,POM(Project Objcet Model)項目對...
    zlcook閱讀 5,912評論 7 26