背景:
另一篇文章說了如何利用IDEA來打包,現在來說說如何利用MAVEN打包
目标:應用本身打成一個jar包,依賴的第三方包放到lib目錄下
開發環境
OS: Windows 8.1
JDK: 1.7.0
開發工具: IntelliJ IDEA 13.1.1
步驟:
1. 首先看下maven項目的标準目錄結構:
2. 看下項目pom.xml的配置,首先要配置maven-jar-plugin,把自己的代碼編譯打包成jar檔案,然後配置maven-assembly-plugin,把整個項目打包成一個釋出包。
<build>
<plugins>
<!-- The configuration of maven-jar-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!--
生成的jar中,不要包含pom.xml和pom.properties這兩個檔案
-->
<addMavenDescriptor>false</addMavenDescriptor>
<!-- Manifest specific configuration -->
<manifest>
<!--
是否要把第三方jar放到manifest的classpath中
-->
<addClasspath>true</addClasspath>
<!--
生成的manifest中classpath的字首,因為要把第三方jar放到lib目錄下,是以classpath的字首是lib/
-->
<classpathPrefix>lib/</classpathPrefix>
<!--
應用的main class
-->
<mainClass>dbRobot.BeanRobot</mainClass>
</manifest>
</archive>
<!--
過濾掉不希望包含在jar中的檔案
-->
<excludes>
<exclude>${project.basedir}/xml/*</exclude>
</excludes>
</configuration>
</plugin>
<!-- The configuration of maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Specifies the configuration file of the assembly plugin -->
<descriptors>
<descriptor>src/main/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3. 再來看package.xml的配置
<assembly>
<id>bin</id>
<!-- 最終打包成一個用于釋出的zip檔案 -->
<formats>
<format>zip</format>
</formats>
<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!--
不使用項目的artifact,第三方jar不要解壓,打包進zip檔案的lib目錄
-->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 把項目相關的說明檔案,打包進zip檔案的根目錄 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<!-- 把項目的配置檔案,打包進zip檔案的config目錄 -->
<fileSet>
<directory>${project.basedir}\src\main\config</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- 把項目的腳本檔案目錄( src/main/scripts )中的啟動腳本檔案,打包進zip檔案的跟目錄 -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>startup.*</include>
</includes>
</fileSet>
<!-- 把項目的腳本檔案(除了啟動腳本檔案),打包進zip檔案的script目錄 -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<exclude>startup.*</exclude>
</includes>
</fileSet>
<!-- 把項目自己編譯出來的jar檔案,打包進zip檔案的根目錄 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
4. 經過jar和assembly,最終得到一個可釋出的zip檔案,内容結構如下: