Maven - 替换Jar包中的文件

8

我想在进行Maven构建时替换现有jar/zip文件中的一个文件。最简单的方法是什么?

2个回答

13

我推荐使用maven-antrun-plugin来完成这类任务,它可以为你提供完整的ant功能。

你可以像这样使用它:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
        <id>repack</id>
        <phase>compile</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <target>
            <!-- note that here we reference previously declared dependency -->
            <unzip src="${org.apache:common-util:jar}" dest="${project.build.directory}/tmp"/>
            <!-- now do what you need to any of unpacked files under target/tmp/ -->
            <zip basedir="${project.build.directory}/tmp" destfile="${project.build.directory}/common-util-modified.jar"/>
            <!-- now the modified jar is available  -->
          </target>
        </configuration>
      </execution>
    </executions>
  </plugin>

但是请记住 - 永远不要修改您本地仓库中的任何文件 - 就像这个例子中指向的${org.apache:common-util:jar}一样。这样做会影响您在同一台计算机上所有项目的后续构建(=针对相同的本地repo)。

在其他计算机上,这样的构建也是无法复制的(或难以复制的)。


2
我甚至更喜欢使用antrun插件和命令jar uf jar-file input-file(s),如http://docs.oracle.com/javase/tutorial/deployment/jar/update.html中所述。 - Vince

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接