Maven 错误 无法访问“属性”的默认值字段

2022-09-03 04:15:18

我使用最新版本的Java ->16。当我运行mvn clean时,我得到无法初始化类org.apache.maven.plugin war.util.WebappStructureSerializer错误。我读到添加maven-war-plugin可以是一个解决方案,但它对我不起作用。当我运行 mvn 安装时,发生以下错误:

注入构造函数 java.lang.ExceptionInitializerError 时出错:无法访问 org.apache.maven..plugin.war.WarMojo 上的属性默认值字段。(未知来源)同时找到 org.apache.maven.plugin.warmojo

我该如何解决这些问题?它们是由Java版本引起的吗?

<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>

答案 1

更改您的 war 插件的版本,并根据您的 jdk 版本进行添加。pom.xmlmaven-compiler-plugin

<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>

答案 2

推荐