1.利用maven-jar-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>
再來看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>
2.利用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> <!--打包時排除掉資料庫連線串和相關配置檔案--> <exclude>jdbc.properties</exclude> <exclude>module.config.properties</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xxx.Server</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> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/cxf/bus-extensions.txt</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>