当前位置:   article > 正文

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

mvn 解压zip

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

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

 

  1. <dependency>
  2. <groupId>com.github.codegerm</groupId>
  3. <artifactId>hangout-dist</artifactId>
  4. <version>${hangout.version}</version>
  5. <type>zip</type>
  6. <classifier>release-bin</classifier>
  7. </dependency>

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

 

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-dependency-plugin</artifactId>
  4. <executions>
  5. <execution>
  6. <id>copy-dependencies</id>
  7. <phase>prepare-package</phase>
  8. <goals>
  9. <goal>copy-dependencies</goal>
  10. </goals>
  11. <configuration>
  12. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  13. <overWriteReleases>false</overWriteReleases>
  14. <overWriteSnapshots>false</overWriteSnapshots>
  15. <overWriteIfNewer>true</overWriteIfNewer>
  16. <excludeArtifactIds>hangout-dist</excludeArtifactIds>
  17. </configuration>
  18. </execution>
  19. </executions>
  20. </plugin>

 

 

 

 

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

 

 

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-dependency-plugin</artifactId>
  4. <executions>
  5. <execution>
  6. <id>unpack</id>
  7. <phase>prepare-package</phase>
  8. <goals>
  9. <goal>unpack</goal>
  10. </goals>
  11. <configuration>
  12. <artifactItems>
  13. <artifactItem>
  14. <groupId>com.github.codegerm</groupId>
  15. <artifactId>hangout-dist</artifactId>
  16. <version>${hangout.version}</version>
  17. <type>zip</type>
  18. <classifier>release-bin</classifier>
  19. <outputDirectory>${project.build.directory}/hangout-dist-${hangout.version}-release-bin</outputDirectory>
  20. </artifactItem>
  21. </artifactItems>
  22. </configuration>
  23. </execution>
  24. </executions>
  25. </plugin>

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

 

 

  1. <plugin>
  2. <artifactId>maven-antrun-plugin</artifactId>
  3. <executions>
  4. <execution>
  5. <id>create-staging-area</id>
  6. <phase>prepare-package</phase>
  7. <goals>
  8. <goal>run</goal>
  9. </goals>
  10. <configuration>
  11. <tasks>
  12. <copy todir="${project.build.directory}/lib">
  13. <fileset dir="${project.build.directory}/hangout-dist-${hangout.version}-release-bin/modules" />
  14. <fileset dir="${project.build.directory}/hangout-dist-${hangout.version}-release-bin/libs">
  15. <exclude name="commons-collections-3.2.1.jar"/>
  16. </fileset>
  17. </copy>
  18. </tasks>
  19. </configuration>
  20. </execution>
  21. </executions>
  22. </plugin>

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

 

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

 

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-assembly-plugin</artifactId>
  4. <version>2.4</version>
  5. <!-- The configuration of the plugin -->
  6. <configuration>
  7. <finalName>${project.name}-${project.version}</finalName>
  8. <outputDirectory>${project.build.directory}/release/</outputDirectory>
  9. <!-- Specifies the configuration file of the assembly plugin -->
  10. <descriptors>
  11. <descriptor>src/main/assembly/assembly.xml</descriptor>
  12. </descriptors>
  13. </configuration>
  14. <executions>
  15. <execution>
  16. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  17. <phase>package</phase> <!-- bind to the packaging phase -->
  18. <goals>
  19. <goal>single</goal>
  20. </goals>
  21. </execution>
  22. </executions>
  23. </plugin>

在指定的目录下面创建assembly.xml文件,然后添加我们需要压缩为zip的目录和文件

 

 

  1. <assembly>
  2. <id>bin</id>
  3. <!-- Generates a zip package containing the needed files -->
  4. <formats>
  5. <format>zip</format>
  6. </formats>
  7. <!-- Adds dependencies to zip package under lib directory -->
  8. <!-- Project artifact is not copied under library directory since it is added to the root directory of the zip package. -->
  9. <fileSets>
  10. <fileSet>
  11. <directory>${project.basedir}/target</directory>
  12. <outputDirectory>/target</outputDirectory>
  13. <includes>
  14. <include>lib/**</include>
  15. <include>conf/**</include>
  16. <include>bin/**</include>
  17. <include>registry/**</include>
  18. <include>nativelibs/**</include>
  19. <include>*</include>
  20. </includes>
  21. </fileSet>
  22. </fileSets>
  23. </assembly>

编译构建maven,我们就可以将工程中特定的文件或者目录创建为一个zip包了!

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/308096
推荐阅读
相关标签
  

闽ICP备14008679号