gpt4 book ai didi

java - 为什么 Spring Boot Maven 插件有时会搞乱重新打包我的 Reactor 模块?

转载 作者:行者123 更新时间:2023-12-02 02:56:11 26 4
gpt4 key购买 nike

项目结构

我有一个 Maven Reactor 项目,其结构如下:

company-parent(用于依赖项和插件管理的其他项目)

<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
</parent>

<groupId>com.mycompany</groupId>
<artifactId>maven-mycompany-parent</artifactId>
<version>1</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.6.RELEASE</version>
</plugin>
...
</pluginManagement>
....
</build>

项目父级

<parent>
<groupId>com.mycompany</groupId>
<artifactId>maven-mycompany-parent</artifactId>
<version>1</version>
</parent>

<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>project-api</module>
<module>project-impl</module>
</modules>

project-api 包含 WSDL 和生成的 POJO 的模块

<parent>
<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>project-api</artifactId>
<packaging>jar</packaging>

project-impl 模块包含 WSDL 的具体 JAX-WS 实现

<parent>
<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>project-impl</artifactId>
<packaging>war</packaging>
...
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>project-api</artifactId>
<version>${project.version}</version>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>${project.basedir}/src/main/sql</directory>
<targetPath>sql</targetPath>
</webResource>
<webResource>
<directory>${project.basedir}</directory>
<include>CHANGELOG.md</include>
<filtering>true</filtering>
<targetPath>.</targetPath>
</webResource>
</webResources>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>

问题

有时,重新打包的.war会因为找不到文件和类加载问题而无法执行。我发现,当问题仍然存在时,重新打包的 .warWEB-INF/lib 包含大量 .jar 依赖项和一个空目录:WEB-INF/lib/project-api-1.0-SNAPSHOT.jar/

这是unzip的输出:

-bash-4.2$ unzip -l project-impl/target/project-impl.war | grep project-api
0 07-17-2019 10:32 WEB-INF/lib/project-api-1.0-SNAPSHOT.jar/

当然,执行失败了:

-bash-4.2$ java -jar project-impl/target/project-impl.war
...
[ERROR] 2019-07-17T11:24:55,711 org.spr.boo.SpringApplication Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.mycompany.package.Application]; nested exception is java.io.FileNotFoundException: class path resource [com/mycompany/package/MyPortType.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at com.mycompany.package.Application.main(Application.java:18) [classes!/:3.0.0-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47) [project-impl.war:3.0.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:86) [project-impl.war:3.0.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [project-impl.war:3.0.0-SNAPSHOT]
at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:57) [project-impl.war:3.0.0-SNAPSHOT]
Caused by: java.io.FileNotFoundException: class path resource [com/mycompany/package/MyPortType.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86) ~[spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73) ~[spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:682) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getInterfaces(ConfigurationClassParser.java:1008) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processInterfaces(ConfigurationClassParser.java:375) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:323) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
... 21 more

当我重新运行 mvn install 时,重新生成的 war 有大约 80% 的机会将修复为 project-api-1.0.jar code> 文件应该存在于 WEB-INF/lib 中,没有空目录。

-bash-4.2$ unzip -l project-impl/target/project-impl.war | grep project-api
2740878 07-17-2019 11:49 WEB-INF/lib/project-api-1.0-SNAPSHOT.jar

在 react 器父级上运行mvn install错误结果后我检查了本地存储库中的api jar,它与zip -T一致。在错误的 .war 生成过程中没有错误消息。查了很多论坛,Spring Boot Maven Plugin的问题,没有发现相关的。

你们中有人遇到过这个问题吗?任何帮助将不胜感激。

PS:遗憾的是,完整的 pom.xml 文件无法满足 StackOverflow 30000 个字符的限制。

更新 1

Maven 调试日志显示了 maven-war-plugin 处理项目 API jar 和其他 jar 的方式存在奇怪的差异:

...
[DEBUG] Processing: project-api-1.0-SNAPSHOT.jar
[DEBUG] Processing: cxf-xjc-runtime-3.3.0.jar
[DEBUG] + WEB-INF/lib/cxf-xjc-runtime-3.3.0.jar has been copied.
[DEBUG] Processing: jakarta.xml.bind-api-2.3.2.jar
[DEBUG] + WEB-INF/lib/jakarta.xml.bind-api-2.3.2.jar has been copied.
...

更新2

我把问题推导到了maven-war-plugin:3.2.2。原始 war 文件在重新打包之前就存在此问题(请参阅 relevant post )。

-bash-4.2$ unzip -l project-impl.war.original | grep project-api
0 07-18-2019 02:58 WEB-INF/lib/project-api-1.0-SNAPSHOT.jar/

更新3

void org.apache.maven.plugins.war.packaging.ArtifactsPackagingTask.copyFile( String sourceId, final WarPackagingContext context, final File file, String targetFilename ) throws IOException

war 打包的问题是参数 file 没有指向本地存储库的 project-api-1.0-SNAPSHOT.jar ,而是到 /absolute/path/project-parent/project-api/target/classes 目录。但不确定为什么这个论点是错误的。

最佳答案

我明白了,与spring-boot-maven-plugin无关。问题在于 maven-site-pluginmaven-war-plugin 互相运行。最初,我从 Jenkins 调用了 mvn clean site site:jar deploy ,以便一步即可使用站点分类器部署 jar Artifact 。 maven-site-plugin 使用依赖 Reactor Artifact (project-api-0.1.0-SNAPSHOT.jar 这里,所以 maven-war-plugin 由于某种原因无法引用它。一个空目录将存储在 warWEB-INF/lib 目录中,而不是称为 project-api -0.1.0-SNAPSHOT.jar/。奇怪。

关于java - 为什么 Spring Boot Maven 插件有时会搞乱重新打包我的 Reactor 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57073162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com