问题:Maven 中是否有任何方法(无需使用 ant 插件)解压缩文件,cd 进入目录,删除文件,然后重新压缩,所有这些都是构建?
这是必要的,因为它是一个复杂的构建,也不想必须使用 gradle 来完成此任务。
最佳答案
truezip-maven-plugin
及其remove
也可以一步满足解压缩、删除文件和再次压缩的要求。目标:
Remove a set of files from an existing archive.
official示例也涵盖了这种情况。
给定以下代码段:
<properties>
<archive>${project.basedir}/sample.zip</archive>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>truezip-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>remove-a-file</id>
<goals>
<goal>remove</goal>
</goals>
<phase>package</phase>
<configuration>
<fileset>
<!-- note how the archive is treated as a normal file directory -->
<directory>${archive}</directory>
<includes>
<include>hello.txt</include>
</includes>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
并执行:
mvn clean package
构建将处理 ${archive}
文件(在本例中为 sample.zip
,与 pom.xml
处于同一级别> 文件,即在 project.basedir
目录中)并从中删除 hello.txt
文件。然后重新压缩一切。
我刚刚测试成功,如果不需要,您甚至可以跳过 properties
部分。但是,您也应该仔细了解:
- zip 文件不应受版本控制,否则会在每次构建时产生冲突
- 该行为很可能不应该是默认 Maven 构建的一部分,因此很适合 Maven profile
- 该插件会替换原始文件,因此如果这是一个问题,您可以先将其复制到另一个位置,然后按上述方式进行处理。要复制它,您可以使用
maven-resources-plugin
及其copy-resources
目标。
关于java - 使用 Maven 解压缩并重新压缩文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39705287/