1.利用maven-jar-pluginspring
<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-pluginapache
<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>