目标:应用本身打成一个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文件,内容结构如下: