Maven - 替换 Jar 中的文件

发布于 2024-11-14 21:23:52 字数 58 浏览 5 评论 0原文

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

I want to replace a file in an existing jar/zip file while doing a Maven build. What is the easiest way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-11-21 21:23:52

对于此类任务,我最喜欢的是 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}。这样做会影响您在同一台计算机上进一步构建所有项目(=针对同一本地存储库)。

此类构建在其他机器上也是不可重现(或难以重现)的。

My favorite for this sort of tasks is maven-antrun-plugin which brings you complete ant functionality.

You can use it like this:

<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>

But remember - never modify any files in your local repository - in this example pointed to by ${org.apache:common-util:jar}. Doing so would affect your further builds of all your projects on the same computer (= against the same local repo).

Such builds are also irreproducible (or hard to reproduce) on other machines.

你又不是我 2024-11-21 21:23:52

我不认为有一个专门的插件可以做到这一点,但我想你可以使用 exec 插件 和来自 更新 jar 中的 .class 文件 的信息来完成此操作。

I don't think there is a dedicated plugin to do this but I would imagine you can use the exec plugin and information from Updating .class file in jar to accomplish this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文