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

系统中使用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包了!

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值