Maven无法解析类

时间:2019-06-06 16:37:22

标签: java maven intellij-idea deployment build

在测试和部署期间,我对maven感到好奇。

项目结构

src / main / java / com /../ models / Employee.java

src / test / java / com /../ models / EmployeeTest.java

员工

public class Employee {
    private final String name;
    private final Dollars salary;

    public Employee(String name, Dollars salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public Dollars getSalary() {
        return this.salary;
    }
}

美元

public class Dollars {

    private final BigDecimal value;
    public Dollars(BigDecimal value) {
        this.value = value;
    }

    public BigDecimal getValue() {
        return value;
    }
}

EmployeeTest

public class EmployeeTest {

    @Test
    public void justASimpleTest() {
        Employee employee = new Employee("David", new Dollars(BigDecimal.valueOf(100)));
        Employee updatedEmployee = updateSalary(BigDecimal.valueOf(200)).apply(employee);

        assertEquals(BigDecimal.valueOf(200), updatedEmployee.getSalary().getValue());
    }

    private Function<Employee, Employee> updateSalary(final BigDecimal value) {
        return employee -> new Employee(employee.getName(), new Dollars(value));
    }
}

问题

当我运行“测试”,“验证”或“安装”时,出现以下错误:

cannot find symbol: class Employee

仅适用于功能!

private Function<Employee, Employee> updateSalary(final BigDecimal value) {...}
                 ^^^^^^^^  ^^^^^^^^

但是intelliJ确实可以解析引用,因为我没有IntelliJ提供的错误,而且可以通过maven进行构建并使用在IntelliJ中运行的JUnit进行测试!

经过几次尝试,我注意到当 packaging 设置为 jar 时,会出现此问题, <packaging>pom</packaging>一切正常。

尽管通过使用“ mvn全新安装”的cmd,通过IntelliJ内的maven和CI / CD(teamcity)上的maven仍然存在此问题

POM

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

    <packaging>jar</packaging>

    <groupId>XXX</groupId>
    <artifactId>YYY</artifactId>
    <version>ZZZ</version>

    <artifactId>sample</artifactId>

    <properties>
        <junit.version>4.11</junit.version>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <proc>only</proc>
                    <generatedSourcesDirectory>${project.build.directory}/generated-sources/annotations
                    </generatedSourcesDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

有人可以向我解释为什么会这样吗? 谢谢:)

1 个答案:

答案 0 :(得分:0)

我发现了问题!

跳过<packaging>pom</packaging>测试,这就是为什么它可以工作,但这不是解决方案!

问题是我正在使用<proc>only</proc>。当该属性具有“ only”值时,意味着将仅执行注释过程,而不进行编译。这解释了为什么在安装阶段找不到Employee类。

有关proc配置HERE的更多详细信息