使用Maven Assembly插件生成的压缩包中Jar体积异常缩小导致推送至PCF失败问题咨询
解决Maven Assembly插件生成的Jar缺失BOOT-INF及Spring Boot Loader类的问题
这种情况我之前碰到过,核心问题在于你用Assembly插件直接打包了项目的编译类,而没有利用Spring Boot Maven插件生成的完整可执行Fat Jar。Spring Boot的可执行Jar有特殊的目录结构(BOOT-INF存放依赖和业务类,org/springframework/boot/loader存放加载器类),Assembly默认的打包逻辑不会自动处理这个结构,所以生成的Jar只是个"瘦Jar",体积骤减且缺失关键文件,导致PCF部署失败。
下面是具体的修复步骤:
1. 确保Spring Boot Maven插件正确配置
首先,你的pom.xml必须包含Spring Boot插件,它负责生成完整的可执行Fat Jar。添加以下配置:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
执行mvn package后,target目录下会生成两个Jar:
[your-project-name]-[version].jar:完整的可执行Fat Jar(包含BOOT-INF和loader类)[your-project-name]-[version].jar.original:原始的瘦Jar(只有项目自身类)
2. 调整Maven Assembly插件配置,打包Fat Jar
修改Assembly插件的配置,让它引用Spring Boot生成的Fat Jar,而不是直接打包项目的classes目录。示例配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>package-distribution</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>src/main/assembly/distribution.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
3. 编写Assembly描述符文件
在src/main/assembly下创建distribution.xml,指定要包含的Fat Jar和其他资源:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>pcf-distribution</id> <formats> <format>zip</format> <!-- 按你的需求选择压缩格式 --> </formats> <fileSets> <!-- 引入Spring Boot生成的Fat Jar --> <fileSet> <directory>${project.build.directory}</directory> <includes> <include>${project.build.finalName}.jar</include> </includes> <outputDirectory>/</outputDirectory> </fileSet> <!-- 如果需要打包配置文件,可添加这部分 --> <fileSet> <directory>src/main/resources</directory> <includes> <include>*.yml</include> <include>*.properties</include> </includes> <outputDirectory>/config</outputDirectory> </fileSet> </fileSets> </assembly>
4. 验证结果
执行mvn clean package后,打开生成的压缩包,里面的Jar应该和target目录下的Fat Jar体积一致。解压Jar查看,能看到BOOT-INF目录和org/springframework/boot/loader下的所有类文件,此时推送PCF就不会报错了。
内容的提问来源于stack exchange,提问作者Divya Kampasi
相关产品推荐
相关产品推荐

