maven解压zip包将jar包copy到指定目录

来源:互联网 发布:网络大学专升本 编辑:程序博客网 时间:2024/06/06 03:37

系统中使用maven构建项目的时候,依赖另一个工程项目,在进行构建的时候需要将另一个工程的zip包解压到本地目录,然后复制到lib目录下面,步骤如下:

1、添加zip包的依赖,需要指定type类型为zip,默认为jar文件

<dependency><groupId>com.github.codegerm</groupId><artifactId>hangout-dist</artifactId><version>${hangout.version}</version><type>zip</type><classifier>release-bin</classifier></dependency>
2、使用maven-dependency-plugin插件在copy dependency包的时候,去掉我们的zip包

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>copy-dependencies</id><phase>prepare-package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/lib</outputDirectory><overWriteReleases>false</overWriteReleases><overWriteSnapshots>false</overWriteSnapshots><overWriteIfNewer>true</overWriteIfNewer><excludeArtifactIds>hangout-dist</excludeArtifactIds></configuration></execution></executions></plugin>

3、使用maven-dependency-plugin插件解压zip文件到指定目录

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>unpack</id><phase>prepare-package</phase><goals><goal>unpack</goal></goals><configuration><artifactItems><artifactItem><groupId>com.github.codegerm</groupId><artifactId>hangout-dist</artifactId><version>${hangout.version}</version><type>zip</type><classifier>release-bin</classifier><outputDirectory>${project.build.directory}/hangout-dist-${hangout.version}-release-bin</outputDirectory></artifactItem></artifactItems></configuration></execution></executions></plugin>
4、使用maven-antrun-plugin插件复制解压后的目录文件中的jar包到工程lib目录下面

<plugin><artifactId>maven-antrun-plugin</artifactId><executions><execution><id>create-staging-area</id><phase>prepare-package</phase><goals><goal>run</goal></goals><configuration><tasks><copy todir="${project.build.directory}/lib"><fileset dir="${project.build.directory}/hangout-dist-${hangout.version}-release-bin/modules" /><fileset dir="${project.build.directory}/hangout-dist-${hangout.version}-release-bin/libs"><exclude name="commons-collections-3.2.1.jar"/></fileset></copy></tasks></configuration></execution></executions></plugin>

利用上面的maven脚本和插件,就能轻松的实现我们的需求!


如果我们想通过maven将工程中指定的目录打成jar包,则我们需要使用到assembly.xml,在maven-assembly-plugin插件中指定assembly.xml文件:

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>2.4</version><!-- The configuration of the plugin --><configuration><finalName>${project.name}-${project.version}</finalName><outputDirectory>${project.build.directory}/release/</outputDirectory><!-- Specifies the configuration file of the assembly plugin --><descriptors><descriptor>src/main/assembly/assembly.xml</descriptor></descriptors></configuration><executions><execution><id>make-assembly</id> <!-- this is used for inheritance merges --><phase>package</phase> <!-- bind to the packaging phase --><goals><goal>single</goal></goals></execution></executions></plugin>
在指定的目录下面创建assembly.xml文件,然后添加我们需要压缩为zip的目录和文件

<assembly><id>bin</id><!-- Generates a zip package containing the needed files --><formats><format>zip</format></formats><!-- Adds dependencies to zip package under lib directory --><!-- Project artifact is not copied under library directory since it is added to the root directory of the zip package. --><fileSets><fileSet><directory>${project.basedir}/target</directory><outputDirectory>/target</outputDirectory><includes><include>lib/**</include><include>conf/**</include><include>bin/**</include><include>registry/**</include><include>nativelibs/**</include><include>*</include></includes></fileSet></fileSets></assembly>
编译构建maven,我们就可以将工程中特定的文件或者目录创建为一个zip包了!

原创粉丝点击