背景
項目中maven工程,非jave web項目,之前一直通過eclipse運行。代碼調試通過後,要部署到linux伺服器上。簡單的方法就是直接打成jar包,通過java命令運行。但有一個問題,當涉及到設定檔變更,或者某個java類代碼變動的時候,要重新打jar包,再上傳,運行。
而我的需求是,將工程打成tar.gz包,在linux伺服器上,解壓後,通過sh啟動指令碼運行。設定檔和class檔案可以直接修改或者替換,而不需要更換整個jar包。
經過調研,發現maven的assembly外掛程式可以達到此要求。記錄一下解決方案。 assembly 介紹
assembly可以定義任何一個檔案或者目錄歸檔方式。舉個例子,如果的你的Maven 2工程包含”src/main/bin”這個目錄,你可以指示assembly外掛程式複製“src/main/bin”目錄下所有的檔案到bin目錄裡(歸檔檔案裡的目錄),並且可以修改它們的許可權屬性(UNIX mode)。 配置
要使用assembly,首先在pom.xml中,配置assembly-plugin:
<build> <plugins> <plugin> <artifactId> maven-assembly-plugin </artifactId> <configuration> <descriptors> <!-- 描述檔案路徑 --> <descriptor>assembly.xml</descriptor> </descriptors> <archive> <manifest> <mainClass>com.cmss.clm.AlarmReport</mainClass> </manifest> </archive> </configuration> <executions> <execution> <!--名字任意 --> <id>make-assembly</id> <!-- 綁定到package生命週期階段上 --> <phase>package</phase> <goals> <!-- 只運行一次 --> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
maven打成的包是什麼樣子的,完全由assembly.xml決定。
看下我最終的assembly.xml是什麼樣子的:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>release</id> <formats> <format>tar.gz</format </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <unpack>false</unpack> <scope>runtime</scope> <outputDirectory>AlarmReport/lib</outputDirectory> <useProjectArtifact>false</useProjectArtifact> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>target/classes</directory> <outputDirectory>AlarmReport</outputDirectory> </fileSet> </fileSets> <files> <file> <source>startup.sh</source> <outputDirectory>AlarmReport/bin</outputDirectory> <filtered>true</filtered> </file> </files> </assembly>
<id>release</id>,release會附加在版本後面; <format>tar.gz</format>指明包的格式為tar.gz; <includeBaseDirectory>false</includeBaseDirectory>,如果為false,不會額外產生根目錄,否則,在tar.gz包中會產生以pom.xml中
artifactId和
version命名的根目錄。 <dependencySets>...</dependencySets>是對依賴包的設定:
<unpack>定義了是否解壓依賴包,如果為true,會解壓出依賴包中的class檔案,反之,則不進行解壓; <scope>限定了對哪些依賴包進行操作;(依賴包scope的值是在pom.xml中定義的) <outputDirectory>依賴包在tar.gz包中相對於根目錄的路徑 <useProjectArtifact>依賴包中是否包含當前工程;
assembly中dependencySets的意思就是,將scope為runtime的依賴包,放到AlarmReport/lib目錄下。 <fileSets>指定哪些檔案包含在打出的tar.gz包中:
<fileSet> 指定目錄<directory>下的檔案打包到<outputDirectory>目錄下; <file> 指定檔案<source>在tar.gz包中的路徑:<outputDirectory>;
assembly中兩個fileSets的作用是,將target/classes下的檔案,打包到AlarmReport目錄;將startup.sh打包到AlarmReport/bin目錄
樣本
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>com.cmss.clm</groupId> <artifactId>AlarmReport</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>AlarmReport</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <artifactId> maven-assembly-plugin </artifactId> <configuration> <descriptors> <!-- 描述檔案路徑 --> <descriptor>assembly.xml</descriptor> </descriptors> <archive> <manifest> <mainClass>com.cmss.clm.AlarmReport</mainClass> </manifest> </archive> </configuration> <executions> <execution> <!--名字任意 --> <id>make-assembly</id> <!-- 綁定到package生命週期階段上 --> <phase>package</phase> <goals> <!-- 只運行一次 --> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> ... </dependencies></project>
工程目錄:
assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>RELEASE</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <unpack>false</unpack> <scope>runtime</scope> <outputDirectory>AlarmReport/lib</outputDirectory> <useProjectArtifact>false</useProjectArtifact> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>target/classes</directory> <outputDirectory>AlarmReport</outputDirectory> </fileSet> </fileSets> <files> <file> <source>startup.sh</source> <outputDirectory>AlarmReport/bin</outputDirectory> <filtered>true</filtered> </file> </files> </assembly>
在eclipse中,選擇maven-build:
在快顯視窗中,輸入package:
打出的包,名字為AlarmReport-1.0-RELEASE.tar.gz,目錄結構如下:
TIPS:
如果僅僅想在打包的同時包含有依賴jar包,可以將上面設定檔中的
<descriptors> <!-- 描述檔案路徑 --> <descriptor>assembly.xml</descriptor> </descriptors>
替換成
<descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs>
這個jar-with-dependencies是assembly預先寫好的一個assembly.xml,就不需要我們自己再寫了。