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


Question :

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

Happening scene :

A Maven-type project was created in Eclipse. The packaging method of the project is war, that is, a Web project. However, there are two errors in the pom.xml of the created project, which are:

  1. Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer
  2. web.xml is missing and <failOnMissingWebXml> is set to true
    insert image description here

Reason and analysis :

Although these two error messages will not affect the normal operation of the project, it is always ugly to display two red crosses on the project.

Let's look at the first question first Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer, the class cannot be instantiated. The cause of this error is the compatibility issue between Eclipse and the Maven plug-in (maven-war-plugin) version. After eclipse is updated from version 2021-03 to version 2021-06, if the version of Maven is still in version 2.x, it will appear this error. When Eclipse creates a project, a default version will be used. So the solution is to specify the version of maven-war-plugin in pom.xml.

Second question web.xml is missing and <failOnMissingWebXml> is set to true. The Maven war packaging type project created in Eclipse does not generate a web.xml file by default. Although the web.xml file is the entry file of the Java Web project, in the later Java Web project, you can use the Java class file to Replaces the web.xml file, ie web.xml is not necessary. However, a prompt will still be given in Eclipse, and the solution is to include it in the error message, that is, configure failOnMissingWebXml in pom.xml.

Solution :

Add the following configuration in 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> 

After adding the above parts, the error disappears, as shown in the figure below:
insert image description here

Note: If the Java Complier and JRE library of the project return to 1.5 after executing the Maven > Update Project... operation, it can be solved by setting the JDK version of Maven.

insert image description here

Specific steps:

  1. Download and extract Maven

  2. Add local Maven in Maven > Installations
    insert image description here

  3. Set Maven profile
    insert image description here

  4. Add the following configuration in Maven's 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>

Guess you like

Origin blog.csdn.net/oscar999/article/details/128273436