Enchantres
Enchantres

Reputation: 1055

Maven error Cannot access defaults field of Properties

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>

Upvotes: 52

Views: 121204

Answers (3)

Muhammad Taha
Muhammad Taha

Reputation: 355

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>

Upvotes: 7

Ashish Gusian
Ashish Gusian

Reputation: 41

  <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

Upvotes: 0

Akshay
Akshay

Reputation: 964

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>

Upvotes: 94

Related Questions