开发者

Maven - Replace a file in a Jar

开发者 https://www.devze.com 2023-03-12 13:31 出处:网络
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?My favorite for this sort of tasks is maven-antrun-plugin which

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号