You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven构建报MojoFailureException:测试类包声明不匹配问题求助

解决Minecraft插件Maven项目构建失败问题

我尝试创建一个基于Minecraft插件原型的Maven项目,但每次执行构建时控制台都会报错,推测是插件或依赖问题(仍在学习阶段,判断可能有误)。

控制台错误信息

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.044 s <<< FAILURE! - in 1.0.0.PluginTest
[ERROR] shouldAnswerWithTrue(1.0.0.PluginTest)  Time elapsed: 0.005 s  <<< ERROR!
java.lang.Error:
Unresolved compilation problems:
        The declared package "" does not match the expected package "1.0.0"
        Syntax error on tokens, Name expected instead

        at 1.0.0.PluginTest.<init>(PluginTest.java:1)

[INFO] 
[INFO] Results:
[INFO]
[ERROR] Errors: 
[ERROR]   PluginTest.<init>:1  Unresolved compilation problems: 
        The declared package "...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.443 s
[INFO] Finished at: 2024-06-29T13:27:10+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project firstplugin: There are test failures.
[ERROR]
[ERROR] Please refer to c:\Users\bjorg\OneDrive\Dokumenter\Projects\firstplugin\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>firstplugin</artifactId>
    <version>1.0.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <repositories>
        <!-- This adds the Spigot Maven repository to the build -->
        <repository>
            <id>spigot-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--This adds the Spigot API artifact to the build -->
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.15.1-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <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-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <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>
    </build>
</project>

解决方案

1. 修正测试类的包路径与声明

错误核心是The declared package "" does not match the expected package "1.0.0",说明Maven根据文件路径认为测试类属于1.0.0包,但代码里没有正确声明包,或者路径与包名不匹配:

  • 不要用版本号1.0.0作为包名,改成和项目groupId一致的结构,比如com.example
  • 把测试类PluginTest.java移动到src/test/java/com/example/目录下
  • 在测试类第一行添加正确的包声明:
package com.example;

2. 修复测试类语法错误

Syntax error on tokens, Name expected instead说明测试类代码存在语法问题,确保类结构正确,示例如下:

package com.example;

import org.junit.Test;
import static org.junit.Assert.assertTrue;

public class PluginTest {
    @Test
    public void shouldAnswerWithTrue() {
        assertTrue(true);
    }
}

3. 优化Java版本(可选)

Spigot 1.15.1要求Java 8运行,而当前pom.xml配置的是Java 7,可能引发兼容性问题,修改properties中的版本:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

4. 重新执行构建

完成上述修改后,运行mvn clean install命令重新构建项目即可。


内容的提问来源于stack exchange,提问作者EBJ EBJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:44:51