Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer 问题解决


问题

Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer

发生场景

在Eclipse中创建了一个Maven类型的项目,项目的打包方式是war, 也就是Web 项目, 可是创建完成的项目的pom.xml 有两处错误, 分别是:

  1. Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer
  2. web.xml is missing and <failOnMissingWebXml> is set to true
    在这里插入图片描述

原因及解析

虽然这两个错误提示不会影响项目的正常运行, 但是项目上显示连个红色的叉叉总归是不好看。

首先看一下第一个问题 Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer, 不能对类进行实例化。这个错误的原因是Eclipse 和 Maven 插件(maven-war-plugin)的版本的兼容问题, eclipse从2021-03版本更新为2021-06版本后,如果Maven的版本还是在2.x 版本的话就会出现这个错误。而在 Eclipse创建项目的时候, 会使用一个默认的版本。 所以解决方法就是在 pom.xml 指定maven-war-plugin的版本。

第二个问题 web.xml is missing and <failOnMissingWebXml> is set to true。 在Eclipse 中创建的Maven的 war打包类型的项目默认是不会产生web.xml 文件的, 虽然web.xml 文件是 Java Web 项目的入口文件,但是在后来的Java Web 项目,可以使用Java 类文件来取代web.xml 文件, 也就是web.xml 不是必要的。 不过在 Eclipse中还是会给出提示, 解决方法就是也就包含在错误信息中, 即在 pom.xml 配置failOnMissingWebXml 。

解决方法

在pom.xml 增加如下配置:

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.3.2</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build> 

增加上述部分之后, 错误消失, 如下图:
在这里插入图片描述

注意:如果执行Maven > Update Project… 操作之后, 项目的Java Compliler 和 JRE库又回到 1.5 的话,可以通过设置Maven的 JDK版本进行解决。

在这里插入图片描述

具体步骤:

  1. 下载和解压Maven

  2. 在Maven > Installations 添加本地的Maven
    在这里插入图片描述

  3. 设置Maven配置文件
    在这里插入图片描述

  4. 在Maven的settings.xml 增加如下配置:

    <profile>     
      <id>JDK-1.8</id>       
      <activation>       
        <activeByDefault>true</activeByDefault>       
        <jdk>1.8</jdk>       
      </activation>       
      <properties>       
        <maven.compiler.source>1.8</maven.compiler.source>       
        <maven.compiler.target>1.8</maven.compiler.target>       
        	<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>       
      </properties>       
    </profile>
    
  </profiles>

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>