Maven - 替换Jar中的文件

M99*_*M99 4 maven-2 maven-plugin maven

我想在执行Maven构建时替换现有jar/zip文件中的文件.实现这一目标的最简单方法是什么?

Pet*_*lka 10

我最喜欢这类任务的是maven-antrun-plugin,它为你带来了完整的蚂蚁功能.

你可以像这样使用它:

<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>
Run Code Online (Sandbox Code Playgroud)

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

这样的构建在其他机器上也是不可再现的(或难以再现).

  • 我什至更喜欢将 `antrun` 插件与命令 `jar uf jar-file input-file(s)` 一起使用,如 http://docs.oracle.com/javase/tutorial/deployment/jar/update.html 中所述 (2认同)

Tne*_*nem 3

我认为没有专用的插件可以执行此操作,但我想您可以使用exec 插件更新 jar 中的 .class 文件中的信息来完成此操作。