Maven测试编译在类路径中找不到类

问题描述 投票:1回答:2

我有这个pom的一个小项目...

<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.shipcloud</groupId>
    <artifactId>shipcloud-api</artifactId>
    <description>The Java API for ShipCloud.</description>
    <version>1.0-SNAPSHOT</version>
    <prerequisites>
        <maven>3.0</maven>
    </prerequisites>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.compiler.source.version>1.8</project.compiler.source.version>
        <project.compiler.target.version>1.8</project.compiler.target.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!-- More Matchers than the default version in JUnit -->
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.0.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>2.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在Eclipse中,一切正常,测试正常(使用maven无法找到的类),但是当我尝试通过maven进行全新安装时,出现一个错误,它无法在AbstractCharSequenceAssert(AssertJ的一部分)中找到这行(这是另一个公共类下面的类):

class UrlParameterStringAssert extends AbstractCharSequenceAssert<UrlParameterStringAssert, String> {

INFO] ---------------------------------------------- ---------------[错误]编译错误:[INFO] ----------------------------------------------- -------------[错误] /home/fschaetz/workspaces/workspace5/.../shipcloud-api/src/test/java/com/example/shipcloud/rest/shipcloud/domain/AddressSearchFieldsTest.java:[103,39]错误:找不到符号[ERROR]符号:类[AbstractCharSequenceAssert

我已经查看了调试输出,并且在运行testCompile时,AssertJ jar实际上位于提供给编译器的类路径上:

-classpath ...:/ home / fschaetz / .m2 / repository / org / assertj / assertj-core / 2.2.0 / assertj-core-2.2.0.jar:...

(该文件存在,可读,包含该类并且在Eclipse中可以正常工作。]

我已经尝试过清理(maven / eclipse),在项目上运行更新等,但是似乎没有任何效果。当我尝试在jenkins,btw中运行Maven安装时,也会发生同样的事情。

麻烦类的导入是...

import static de.assona.rest.shipcloud.domain.UrlParameterStringAssert.assertThat;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.assertj.core.api.AbstractCharSequenceAssert;
import org.junit.Before;
import org.junit.Test;

有什么想法吗?

java maven assertj
2个回答
0
投票

老实说,我不知道为什么是问题所在(并且很高兴为每个人解释这个问题,但是WAS的原因是这两个类在同一文件中定义...

public class AddressSearchFieldsTest {

... some testing ...

}

class UrlParameterStringAssert extends AbstractCharSequenceAssert<UrlParameterStringAssert, String> {

... a custom Assertion class...

}

通常,我不希望这样的事情有麻烦(因为在同一个文件中声明非公共类应该没有问题),但是maven不喜欢它。将UrlParameterStringAssert移至其自己的文件可解决此问题。很想知道为什么。


0
投票

对我来说,您有一个奇怪的行家设置。

假设我们具有以下结构

src\main\java\Foo.java                                               
src\test\java\sub\optimal\mavenexample\AddressSearchFieldsTest.java

Foo.java

class Foo { }

AddressSearchFieldsTest.java

import org.assertj.core.api.AbstractCharSequenceAssert;
public class AddressSearchFieldsTest { }

class UrlParameterStringAssert extends
    AbstractCharSequenceAssert<UrlParameterStringAssert, String> {
    UrlParameterStringAssert(String s) {
        super(s, UrlParameterStringAssert.class);
    }
}

使用pom.xml跟随目标将由mvn clean compile执行>

# from the log output
maven-compiler-plugin:2.3.2:compile (default-compile)

并生成以下类文件

target\classes\Foo.class

例如,目标default-testCompilemvn clean compile test执行

# from the log output
maven-compiler-plugin:2.3.2:testCompile (default-testCompile)

并生成以下类文件

target\classes\Foo.class
target\test-classes\AddressSearchFieldsTest.class
target\test-classes\UrlParameterStringAssert.class

您应该检查为什么您的mvn clean compile试图编译测试类文件。

© www.soinside.com 2019 - 2024. All rights reserved.