簡體   English   中英

Maven 在使用測試 scope 時無法編譯測試類

[英]Maven cant compile test-classes when using test scope

我目前正在嘗試使用MockBukkit來測試我的 Minecraft 插件,並將它包含在我的 pom 中並通過測試 scope 因為我只需要它。 然而問題是,當使用測試 scope 時,依賴項出現在我的本地存儲庫中,但無法通過 eclipse 或 Maven 解決。

問題似乎是編譯器插件的默認編譯目標試圖編譯測試類,即使它不能使用依賴項,因為它們僅在測試階段可用。 (但這只是我的猜測)。

Maven output:

[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ CityCraft ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 32 source files to C:\Users\lptoa\Desktop\MC Plugins\CityCraft\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[3,32] package be.seeseemelk.mockbukkit does not exist
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[4,32] package be.seeseemelk.mockbukkit does not exist
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[9,17] cannot find symbol
  symbol:   class ServerMock
  location: class test.java.skyvide.de.city.CityTests
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[12,10] cannot find symbol
  symbol:   class Before
  location: class test.java.skyvide.de.city.CityTests
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[19,10] cannot find symbol
  symbol:   class After
  location: class test.java.skyvide.de.city.CityTests
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[15,22] cannot find symbol
  symbol:   variable MockBukkit
  location: class test.java.skyvide.de.city.CityTests
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[16,34] cannot find symbol
  symbol:   variable MockBukkit
  location: class test.java.skyvide.de.city.CityTests
[ERROR] /C:/Users/lptoa/Desktop/MC Plugins/CityCraft/src/test/java/skyvide/de/city/CityTests.java:[22,13] cannot find symbol
  symbol:   variable MockBukkit
  location: class test.java.skyvide.de.city.CityTests

地點:

->src
 --->test
    --->java
      --->example
        --->ExampleTest.java

代碼:

package test.java.example;

import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import main.plugin;

public class ExampleTest {
    
    private ServerMock server;
    private Plugin plugin;

    @Before
    public void setUp()
    {
        server = MockBukkit.mock();
        plugin = (Plugin) MockBukkit.load(Plugin.class);
    }

    @After
    public void tearDown()
    {
        MockBukkit.unmock();
    }
}

聚甲醛:

<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>sv-plugins</groupId>
  <artifactId>CityCraft</artifactId>
  <version>1.0</version>
  <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <profiles>
    <profile>
        <id>testing</id>
        <activation>
            <property>
                <name>test.output.dir</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.3.1</version>
                    <configuration>
                      <outputDirectory>${test.output.dir}</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
  </profiles>
  <repositories>
    <repository>
        <id>papermc</id>
        <url>https://papermc.io/repo/repository/maven-public/</url>
    </repository>
    <repository>
        <id>spigot-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
    <repository>
        <id>everything</id>
        <url>https://repo.citizensnpcs.co/</url>
    </repository>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
    <repository>
        <id>aikar</id>
        <url>https://repo.aikar.co/content/groups/aikar/</url>
    </repository>
  </repositories>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>com.github.WesJD.AnvilGUI:anvilgui</include>
                            <include>co.aikar:acf-paper</include>
                        </includes>
                    </artifactSet>
                </configuration>
            </execution>
        </executions>
      </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
  </build>
  <dependencies>
  <dependency>
    <groupId>com.destroystokyo.paper</groupId>
    <artifactId>paper-api</artifactId>
    <version>1.15.2-R0.1-SNAPSHOT</version>
    <scope>provided</scope>
 </dependency>
  <dependency>
    <groupId>net.citizensnpcs</groupId>
    <artifactId>citizens</artifactId>
    <version>2.0.27-SNAPSHOT</version>
    <type>pom</type>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>sv-plugins</groupId>
    <artifactId>MySQL</artifactId>
    <version>1.0</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>sv-plugins</groupId>
    <artifactId>Clouds</artifactId>
    <version>1.0</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>com.github.WesJD.AnvilGUI</groupId>
    <artifactId>anvilgui</artifactId>
    <version>master-SNAPSHOT</version>
  </dependency>
  <dependency>
    <groupId>co.aikar</groupId>
    <artifactId>acf-paper</artifactId>
    <version>0.5.0-SNAPSHOT</version>
  </dependency>
  <dependency>
    <groupId>com.github.seeseemelk</groupId>
    <artifactId>MockBukkit-v1.16</artifactId>
    <version>0.5.0</version>
    <scope>test</scope>
  </dependency>
  </dependencies>
</project>

好的,錯誤只是我必須更改.classpath,因為我剛剛將默認的 eclipse 項目轉換為 Maven 項目,所以我不得不刪除構建路徑中的默認 src 文件夾並添加不同的 Z91175C109804AC13777F744C 文件夾。 沒有它,Maven 試圖將每個 class 編譯為正常的,而不是作為使它們失敗的測試。

如果有人感興趣,這里是兩個.classpath 文件之間的比較:

老的:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
        <attributes>
            <attribute name="module" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

新的:

<classpath>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
        <attributes>
            <attribute name="module" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java">
        <attributes>
            <attribute name="test" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" path="src/main/resources"/>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM