maven压缩/解压zip/war包操作

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

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

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

1
2
3
4
5
6
7
<dependency>
<groupId>com.newrelic.agent.java</groupId>
<artifactId>newrelic-java</artifactId>
<version>${newrelic.version}</version>
<scope>provided</scope>
<type>zip</type>
</dependency>

2、使用maven-dependency-plugin插件在copy dependency包的时候,去掉我们的zip包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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>newrelic-java</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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.newrelic.agent.java</groupId>
<artifactId>newrelic-java</artifactId>
<version>${newrelic.version}</version>
<type>zip</type>
<scope>provided</scope>
<outputDirectory>${project.build.directory}/newrelic</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

4、使用maven-antrun-plugin插件复制解压后的目录文件中的jar包到工程lib目录下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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}/newrelic" />
<exclude name="newrelic-api-javadoc.jar"/>
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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的目录和文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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包了!

二、Maven打包期依赖war包/Zip包 并解压至指定目录的pom配置

首先配置依赖,引入需要依赖的war包和zip包:

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>com.huawei.mateinfo</groupId>
<artifactId>kewen-common-war</artifactId>
<version>0.0.1</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.huawei.mateinfo</groupId>
<artifactId>kewen-common-war-template</artifactId>
<version>0.0.1</version>
<type>zip</type>
</dependency>

在使用maven-dependency-plugin插件,指定依赖的文件解压路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  <plugin>  
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.huawei.mateinfo</groupId>
<artifactId>kewen-common-war</artifactId>
<version>0.0.1</version>
<type>war</type>
<outputDirectory>../workdir/sms</outputDirectory>
<includes>META-INF/**,WEB-INF/**/*,heartbeat.jsp</includes>
</artifactItem>
<artifactItem>
<groupId>com.huawei.mateinfo</groupId>
<artifactId>kewen-common-war-template</artifactId>
<version>0.0.1</version>
<type>zip</type>
<outputDirectory>../workdir</outputDirectory>
<includes>*/**</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

之后正常install编译打包即可。

如果你喜欢我的blog,请鼓励我一杯咖啡☕️
0%