Maven编译给出:找不到符号 – 对于坐在同一个应用程序中的类

我遇到这样令人费解的问题已经有一段时间了。 我有一个类引用另一个坐在同一个应用程序中的另一个包中的类,也就是说,不在另一个jar存档文件中。

包含类是learnintouch-rest / src / test / java / com / thalasoft / learnintouch / rest / acceptance / AbstractControllerTest.java

包含的类是/home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/config/WebTestConfiguration.java

在Eclipse下,编辑器中没有问题,也没有编译错误。

但是运行Maven构建会产生编译错误:

mvn clean test-compile -Pacceptance [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project learnintouch-rest: Compilation failure: Compilation failure: [ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[16,46] cannot find symbol [ERROR] symbol: class WebTestConfiguration [ERROR] location: package com.thalasoft.learnintouch.rest.config [ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[21,116] cannot find symbol [ERROR] symbol: class WebTestConfiguration 

这是包含类的代码:

 @RunWith(SpringJUnit4ClassRunner.class) @Transactional @WebAppConfiguration @ContextConfiguration(classes = { ApplicationConfiguration.class, WebSecurityConfig.class, WebConfiguration.class, WebTestConfiguration.class }) public abstract class AbstractControllerTest { 

此抽象测试类位于验收测试目录下,该目录要求在运行Maven命令时显式激活-Pacceptance配置文件。

默认配置文件不运行此验收测试,而只运行某些集成测试。

需要注意的一点是,这个抽象类看起来像集成测试中使用的一个抽象类。

这是集成测试的包含类:

 import com.thalasoft.learnintouch.rest.config.WebTestConfiguration; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = {ApplicationConfiguration.class}, WebSecurityConfig.class, WebConfiguration.class, WebTestConfiguration.class }) @Transactional public abstract class AbstractControllerTest { 

如你所见,它看起来很像另一个。

我也可以给pom.xml文件内容,如果它可以帮助:

   rest  true   src/test/java/com/thalasoft/learnintouch/rest    acceptance  false   src/test/java/com/thalasoft/learnintouch/rest/acceptance    

如果您对此有任何线索,我们将不胜感激。

使用maven,您的测试可以访问项目的所有源代码(src / main / java)和所有测试源代码(默认为src / test / java)。

在这里,您的配置文件将测试源目录定义为src / test / java / com / thalasoft / learnintouch / rest因此,您的测试代码可以访问src / main / java中的所有内容以及src / test / java / com / thalasoft中的所有内容/ learnintouch /rest

您的配置文件接受将测试源目录定义为src / test / java / com / thalasoft / learnintouch / rest / acceptance所以,您的测试代码可以访问src / main / java中的所有内容以及src / test / java / com / thalasoft中的所有内容/ learnintouch / rest / acceptance 。 WebTestConfiguration 无法访问,因为它位于上面的包中。

要使用不同的配置文件运行特定测试,我建议配置负责运行测试的surefire插件。 这里有一个很好的例子: https : //weblogs.java.net/blog/carcassi/archive/2011/04/21/running-integration-tests-and-unit-tests-separately-maven

问题描述:

我的项目与下面的结构有类似的问题。

 project -pom.xml -common-module --src, pom.xml, etc -web-module --src, pom.xml, etc (but dependent on 'common') 

mvn install对于common-module运行正常,但是对于使用common-module中的java类( cannot find symbol )的web模块代码会产生编译错误。 我使用的是JDK 8 + Maven 3.2.5 ,但是在pom编译器中,杠杆设置为7。

MY解决方案:

我安装了JDK 7Maven 3.1.1mvn installmvn install 。 答对了..! 每个人都工作得很好并且建立成功。

我无法在任何地方找到我的问题的解决方案,所以虽然在这里发布我的修复,因为它是相关的。 (也许可以帮助一些人)

尝试在pom.xml中添加以下行,我觉得它缺失了

   acceptance  false   src/test/java/com/thalasoft/learnintouch/rest/config