0

项目结构

我有一个 Maven 反应器项目,结构如下:

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>

project-parent

<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由于找不到文件和类加载问题,无法执行重新打包。我想通了,当问题仍然存在时,重新打包.war的 'sWEB-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时,重新生成的文件有大约 80% 的机会war会被修复,因为project-api-1.0.jar文件应该存在于应有的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

mvn install在reactor parent上运行错误结果后,我检查了本地存储库中的api jar ,它是zip -T一致的。.war故障生成期间没有错误消息。检查了很多论坛,Spring Boot Maven Plugin 的问题,没有发现任何相关的。

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

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

更新 1

Maven 调试日志显示了maven-war-plugin处理项目 APIjar和其他jars 的奇怪差异:

...
[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文件在重新打包前就有这个问题(见相关帖子)。

-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目录。不确定为什么这个论点是错误的。

4

1 回答 1

0

我想通了,和它无关spring-boot-maven-plugin。问题在于彼此之间maven-site-pluginmaven-war-plugin相互追逐。最初,我从 Jenkins 中调用mvn clean site site:jar deploy,以在一步中启用带有站点分类器的 jar 工件的部署。maven-site-plugin对依赖反应堆工件做一些事情(project-api-0.1.0-SNAPSHOT.jar在这里,所以maven-war-plugin由于某种原因不能引用它。一个空目录将存储在war'WEB-INF/lib目录中,而不是称为project-api-0.1.0-SNAPSHOT.jar/.Weird。

于 2019-07-30T05:35:41.720 回答