修改jar包文件,重新打包jar

一、网上搜到的下列方案都不行:

1、直接用WinRAR拖拽替换jar包内文件

失败,报错:压缩文件已损坏。

我以前替换成功过,但是启动的时候报错。这种方式可能会伤害到jar包的一些标识。


2、解压jar包,然后使用jar命令(比如jar -uvf)重新打包

失败,能打包,但是以可执行文件(./xxx.jar start)运行时提示:jar: cannot execute binary file


最终我用下面方案成功了:


将jar包文件解压,导入到maven项目的main/resource目录下,删除/META-INF/MANIFEST.MF,然后配置maven pom.xml,如下所示:

<?xml version="1.0"  encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <parent>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo</artifactId>
    <version>0.10.3-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>manual-package</artifactId>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <mainClass>com.ctrip.framework.apollo.portal.PortalApplication</mainClass>
          <executable>true</executable>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

    其原理就是将 class文件当做resource打包到jar中即可。


© 2009-2020 Zollty.com 版权所有。渝ICP备20008982号