Maven error Cannot access defaults field of Properties
Asked Answered
I

3

49

I use the newest version of Java -> 16. When I run mvn clean, I am getting Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer error. I read that adding maven-war-plugin can be a solution, but it didn't work for me. When I run mvn install, the following error occurs:

Error injecting constructor, java.lang.ExceptionInInitializerError: Cannot access defaults field of Properties at org.apache.maven.plugin.war.WarMojo.(Unknown Source) while locating org.apache.maven.plugin.war.WarMojo

How can I solve these problems? Are they caused by the Java version?

<modelVersion>4.0.0</modelVersion>

<groupId>web-programming</groupId>
<artifactId>servlet-demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
    <java.version>16</java.version>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <maven.compiler.source>${java.version}</maven.compiler.source>
</properties>

<dependencies>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!-- Optional. Allows the app to be run by simply typing mvn jetty:run -->
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.4</version>
            <dependencies>
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>1.2.13</version>
                </dependency>
            </dependencies>
            <configuration>
                <contextPath>/</contextPath>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <connectors>
                    <connector
                        implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>9090</port>
                    </connector>
                </connectors>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
        </plugin>
    </plugins>
</build>
Inch answered 19/4, 2021 at 20:34 Comment(4)
Have you checked whether your maven version is comparable with your java version or not?Bookrest
You are using a ca. 9 year old pluing (maven-war-plugin) and a 13 year old plugin (maven-jetty-plugin) and you should use the most recent version maven.apache.org/plugins furthermoreSisco
I changed the versions of maven-war-plugin and maven-jetty-plugin and the errors disappeared. Thank you very much.Inch
@Sani please post a solution and validate itSafety
M
89

Change the version of the war plugin in your pom.xml and add maven-compiler-plugin according to your jdk version.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.3.1</version>
</plugin>
<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
</plugin>
Macadamia answered 22/7, 2021 at 12:12 Comment(1)
just adjusting the war-plugin version helped, omitting the compiler-plugin version (no <version>... part)Kegan
C
6

with in your build tag paste below code

<pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </pluginManagement>
Carline answered 30/5, 2023 at 14:29 Comment(0)
T
0
  <pluginManagement>
   
       <plugins>
         <plugin>
           <artifactId>maven-clean-plugin</artifactId>
           <version>3.1.0</version>
         </plugin>
    
         <plugin>
           <artifactId>maven-resources-plugin</artifactId>
           <version>3.0.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>3.8.0</version>
         </plugin>
    <plugin>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.22.1</version>
    </plugin>
    <plugin>
     <artifactId>maven-war-plugin</artifactId>
     <version>3.2.2</version>
    </plugin>
    <plugin>`enter code here`
     <artifactId>maven-install-plugin</artifactId>
     <version>2.5.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-deploy-plugin</artifactId>
     <version>2.8.2</version>
    </plugin>
   </plugins>
  </pluginManagement>

Add this plugin in pom.xml file in between . error will vanish

Torey answered 5/4, 2023 at 5:45 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Lamaism

© 2022 - 2024 — McMap. All rights reserved.