Maven构建失败-编译器插件

48

我正在尝试使用Maven构建项目,但遇到以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project webgoat-container: Fatal error compiling: java.lang.ExceptionInInitializerError: Unable to make field private com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs accessible: module jdk.compiler does not "opens com.sun.tools.javac.processing" to unnamed module @5d01a2eb -> [Help 1]
项目的pom.xml文件是:

<modelVersion>4.0.0</modelVersion>
<groupId>org.owasp.webgoat</groupId>
<artifactId>webgoat-parent</artifactId>
<packaging>pom</packaging>
<version>8.2.1-SNAPSHOT</version>

<name>WebGoat Parent Pom</name>
<description>Parent Pom for the WebGoat Project. A deliberately insecure Web Application</description>
<inceptionYear>2006</inceptionYear>
<url>https://github.com/WebGoat/WebGoat</url>

<organization>
    <name>OWASP</name>
    <url>https://github.com/WebGoat/WebGoat/</url>
</organization>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.3</version>
</parent>

<licenses>
    <license>
        <name>GNU General Public License, version 2</name>
        <url>https://www.gnu.org/licenses/gpl-2.0.txt</url>
    </license>
</licenses>

<developers>
    <developer>
        <id>mayhew64</id>
        <name>Bruce Mayhew</name>
        <email>webgoat@owasp.org</email>
        <organization>OWASP</organization>
        <organizationUrl>https://github.com/WebGoat/WebGoat</organizationUrl>
    </developer>
    <developer>
        <id>nbaars</id>
        <name>Nanne Baars</name>
        <email>nanne.baars@owasp.org</email>
        <organizationUrl>https://github.com/nbaars</organizationUrl>
        <timezone>Europe/Amsterdam</timezone>
    </developer>
    <developer>
        <id>misfir3</id>
        <name>Jason White</name>
        <email>jason.white@owasp.org</email>
    </developer>
    <developer>
        <id>zubcevic</id>
        <name>René Zubcevic</name>
        <email>rene.zubcevic@owasp.org</email>
    </developer>
    <developer>
        <id>jwayman</id>
        <name>Jeff Wayman</name>
        <email/>
    </developer>
    <developer>
        <id>dcowden</id>
        <name>Dave Cowden</name>
        <email/>
    </developer>
    <developer>
        <id>lawson89</id>
        <name>Richard Lawson</name>
        <email/>
    </developer>
    <developer>
        <id>dougmorato</id>
        <name>Doug Morato</name>
        <email>doug.morato@owasp.org</email>
        <organization>OWASP</organization>
        <organizationUrl>https://github.com/dougmorato</organizationUrl>
        <timezone>America/New_York</timezone>
        <properties>
            <picUrl>https://avatars2.githubusercontent.com/u/9654?v=3&amp;s=150</picUrl>
        </properties>
    </developer>
</developers>

<mailingLists>
    <mailingList>
        <name>OWASP WebGoat Mailing List</name>
        <subscribe>https://lists.owasp.org/mailman/listinfo/owasp-webgoat</subscribe>
        <unsubscribe>Owasp-webgoat-request@lists.owasp.org</unsubscribe>
        <post>owasp-webgoat@lists.owasp.org</post>
        <archive>http://lists.owasp.org/pipermail/owasp-webgoat/</archive>
    </mailingList>
</mailingLists>

<scm>
    <url>https://github.com/WebGoat/WebGoat</url>
    <connection>scm:git:git@github.com:WebGoat/WebGoat.git</connection>
    <developerConnection>scm:git:git@github.com:WebGoat/WebGoat.git</developerConnection>
    <tag>HEAD</tag>
</scm>

<issueManagement>
    <system>Github Issues</system>
    <url>https://github.com/WebGoat/WebGoat/issues</url>
</issueManagement>

<ciManagement>
    <system>Travis CI</system>
    <url>https://travis-ci.org/WebGoat/WebGoat</url>
</ciManagement>

<properties>
    <!-- Use UTF-8 Encoding -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>

    <!-- This build number will be ubdated by Travis-CI -->
    <build.number>build</build.number>

    <!-- Shared properties with plugins and version numbers across submodules-->
    <activation.version>1.1.1</activation.version>
    <commons-collections.version>3.2.1</commons-collections.version>
    <commons-lang3.version>3.4</commons-lang3.version>
    <commons-io.version>2.6</commons-io.version>
    <guava.version>30.1-jre</guava.version>
    <lombok.version>1.18.4</lombok.version>
    <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
    <maven-failsafe-plugin.version>2.22.0</maven-failsafe-plugin.version>
    <maven-jar-plugin.version>3.1.2</maven-jar-plugin.version>
    <maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
    <maven-source-plugin.version>3.1.0</maven-source-plugin.version>
    <maven-surefire-plugin.version>3.0.0-M4</maven-surefire-plugin.version>
    <revision>8.2.0-SNAPSHOT</revision>
</properties>

<modules>
    <module>webgoat-container</module>
    <module>webgoat-lessons</module>
    <module>webgoat-server</module>
    <module>webwolf</module>
    <module>webgoat-integration-tests</module>
    <module>docker</module><!-- copy required jars in preparation of docker all-in-one build -->
</modules>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>flatten-maven-plugin</artifactId>
            <version>1.2.5</version>
            <configuration>
            </configuration>
            <executions>
                <execution>
                    <id>flatten</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>flatten</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <consoleOutput>true</consoleOutput>
                <failsOnError>true</failsOnError>
                <configLocation>config/checkstyle/checkstyle.xml</configLocation>
                <suppressionsLocation>config/checkstyle/suppressions.xml</suppressionsLocation>
                <suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>3.14.0</version>
            <configuration>
                <targetJdk>11</targetJdk>
                <failurePriority>1</failurePriority><!-- 5 means fail even on the lowest priority, 0 means never fail -->
                <rulesets>
                    <!--suppress UnresolvedMavenProperty -->
                    <ruleset>${maven.multiModuleProjectDirectory}/config/pmd/pmd-ruleset.xml</ruleset>
                </rulesets>
                <failOnViolation>true</failOnViolation>
                <printFailingErrors>true</printFailingErrors>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>owasp</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.owasp</groupId>
                    <artifactId>dependency-check-maven</artifactId>
                    <version>5.3.2</version>
                    <configuration>
                        <failBuildOnCVSS>7</failBuildOnCVSS>
                        <skipProvidedScope>true</skipProvidedScope>
                        <skipRuntimeScope>true</skipRuntimeScope>
                        <suppressionFiles>
                            <!--suppress UnresolvedMavenProperty -->
                            <suppressionFile>
                                ${maven.multiModuleProjectDirectory}/config/dependency-check/project-suppression.xml
                            </suppressionFile>
                        </suppressionFiles>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>check</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

<repositories>
    <repository>
        <id>central</id>
        <url>https://repo.maven.apache.org/maven2</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <url>https://repo.maven.apache.org/maven2</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

mvn -v 输出:

Maven home: /Users/tom/apache-maven-3.6.3
Java version: 16, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"

而且我的$JAVA_HOME已经设置为:

% echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home

我认为Maven存在一个问题,即不知道我的正确的javac位置,但我不确定我需要在pom.xml文件中编辑/添加什么内容来确保它知道。我验证了javac是16版本。


你能分享一下webgoat-container项目的pom.xml文件吗?似乎问题出在编译该模块时。 - Ajinkya
请先使用 JDK 11 构建您的项目,因为我认为您遇到了与 Lombok 相关的问题。 - khmarbaise
7个回答

61

请将您的Lombok版本更新到最新版本。


1
我对Java非常陌生,正在学习一个较旧的Spring/Spring Boot课程。这是唯一提到MongoDB的课程(我需要在Java环境下学习它:P)。这个答案保住了我剩下的头发,哈哈。谢谢! - Int'l Man Of Coding Mystery
我使用了https://mvnrepository.com/artifact/org.projectlombok/lombok来查找最新版本(此时为1.18.24),并将我的依赖从项目`org.project:lombok`(没有版本,因为它是从SpringParent org.springframework.boot:spring-boot-starter-parent:2.1.0.RELEASE中获取的)更改为使用org.projectlombok.lombok:1.18.24。在我的情况下,我认为问题是依赖关系没有正确定义。 - PatS
如果我没有偶然看到这个答案,就不可能找到解决方案。 - Kalana Dananjaya
它在IDE中运行,但无法通过maven编译。这个bug真是个噩梦。非常感谢你,Methmal!供以后参考,版本1.18.26被证明可行。 - Valerij Dobler

16

解决步骤:

  1. 执行 mvn --version 命令:

Maven home: /Applications/apache-maven-3.8.1 Java version: 1.16.0_291, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre Default locale: en_IN, platform encoding: UTF-8 OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"

  1. 如果你的 Java 版本与项目所需版本不同,例如我的情况是 1.8,则修改环境变量设置:

nano ~/.zshenv -> 追加 "-v 1.8" 并提及版本名称。

这里是 .zshenv 文件:

------------------------ ------------------------ 

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

export M2_HOME=/usr/local/Cellar/maven/3.8.1

export PATH=$PATH:$M2_HOME/bin
------------------------ ------------------------ 

Do source ~/.zshenv

现在再检查一下:
 mvn --version

Maven home: /Applications/apache-maven-3.8.1 Java version: 1.8.0_291, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre Default locale: en_IN, platform encoding: UTF-8 OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"
你会看到版本已更改为1.8
现在构建项目。

2
这是正确的解决方案,有详细的步骤,谢谢! - Nestor Milyaev
1
export JAVA_HOME=$(/usr/libexec/java_home -v1.8.0) - 更新 - Vipul Gupta
1
它正在更改Java环境变量版本,但在我的情况下没有解决实际问题。 - Devendra Singraul

7

构建此项目需要使用 Java 11 。我使用 JDK 11 在本地构建了它。我使用的是 Maven 包装器。您可以尝试使用以下命令。

./mvnw clean install

请在切换到JDK 11并确保检出主分支后再试一次。

5

我遇到了同样的问题。在我的情况下,mvn使用的是Java 17版本,而项目所需的版本是Java 8。按照以下方式设置插件配置没有帮助:

          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.1</version>
              <configuration>
                 <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>

它似乎只告诉mvn当前编译器(在我的情况下是java 17)将源代码和目标设定为1.8版本。实际由mvn用于项目编译的javac仍然是java 17。在我的特定情况下,这种安排不起作用,并且我一直收到对应的错误。 我需要指示maven使用属于java 8安装的javac。通过设置一个环境变量JAVA_1_8_HOME并指向我mac系统上已安装版本的java home,我实现了这一点。所需的版本/Library/Java/JavaVirtualMachines/openjdk-8.jdk/Contents/Home是通过运行此命令/usr/libexec/java_home -V找到的,并将其设置为JAVA_1_8_HOME环境变量。 以下配置属性需要设置为maven-compiler-plugin

              <configuration>
                  <verbose>true</verbose>
                  <fork>true</fork>
                  <executable>${env.JAVA_1_8_HOME}/bin/javac</executable>
                  <compilerVersion>1.8</compilerVersion>
              </configuration>

1
在我的情况下,项目Lombok的版本选择错误。我选择了最新版本,问题得到解决。

对我来说一样。谢谢! - Antonio Petricca

1

从官方网站下载 jdk-11apache-maven-3.8.6 ,并按以下方式更新java和maven路径:(在mac上,输入命令 open .zshrc 并添加以下行)

export JAVA_HOME=/Users//Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home

export M2_HOME=/Users//Downloads/apache-maven-3.8.6

export PATH=$PATH:$M2_HOME/bin

alias mvn='$M2_HOME/bin/mvn'

export PATH=/opt/apache-maven-3.8.6/bin:$PATH

然后运行 mvn clean compile


1
你正在使用两个不同的路径。不要将已使用的二进制文件和其他内容存放在用户文件夹中,它们很可能会被删除。 - SimonC

0

检查您的Java版本是否与您的项目兼容


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接