Java Maven 编译给出:找不到符号 - 对于坐在同一个应用程序中的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23327920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven compile gives: Cannot find symbol - For a class sitting in the same app
提问by Stephane
It's a while since I met such puzzling issue. I'm having a class that references another one sitting in another package in the same application, that is, NOT in another jar archive file.
我已经有一段时间没有遇到这样令人费解的问题了。我有一个类引用位于同一个应用程序中另一个包中的另一个类,即不在另一个 jar 存档文件中。
The including class is learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java
包含的类是learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java
The included class is /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/config/WebTestConfiguration.java
包含的类是 /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/config/WebTestConfiguration.java
Under Eclipse there is no issue and no compilation error in the editor.
在 Eclipse 下,编辑器中没有问题也没有编译错误。
But running a Maven build gives a compilation error:
但是运行 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
Here is the code of the including class:
这是包含类的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@WebAppConfiguration
@ContextConfiguration(classes = {
ApplicationConfiguration.class,
WebSecurityConfig.class,
WebConfiguration.class,
WebTestConfiguration.class
})
public abstract class AbstractControllerTest {
This abstract test class is sitting under the acceptance test directory, which mandates the explicit activation of the -Pacceptance profile when running the Maven command.
这个抽象测试类位于验收测试目录下,它要求在运行 Maven 命令时显式激活 -Pacceptance 配置文件。
The default profile does not run this acceptance test, but only some integration test.
默认配置文件不运行此验收测试,而只运行一些集成测试。
One thing to note, is that this abstract class looks like the one abstract class used in the integration test.
需要注意的一件事是,这个抽象类看起来像集成测试中使用的一个抽象类。
Here is the including class of the integration test:
这是集成测试的包含类:
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 {
As you can see, it looks much like the other one.
如您所见,它看起来很像另一个。
I can also give the pom.xmlfile content, if it can help:
我还可以提供pom.xml文件内容,如果可以的话:
<profiles>
<profile>
<id>rest</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<test.source.dir>src/test/java/com/thalasoft/learnintouch/rest</test.source.dir>
</properties>
</profile>
<profile>
<id>acceptance</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<test.source.dir>src/test/java/com/thalasoft/learnintouch/rest/acceptance</test.source.dir>
</properties>
</profile>
</profiles>
If you have any clue on this one, it'd be greatly appreciated.
如果您对此有任何线索,将不胜感激。
采纳答案by Melou
With maven your tests can access to all the source code of your project (src/main/java) and all the test source code (default is src/test/java).
使用 maven,您的测试可以访问项目的所有源代码(src/main/java)和所有测试源代码(默认为 src/test/java)。
Here, your profile restdefines the test source directory as src/test/java/com/thalasoft/learnintouch/restSo, your test code can access everything in src/main/java and everything in src/test/java/com/thalasoft/learnintouch/rest
在这里,您的配置文件rest将测试源目录定义为src/test/java/com/thalasoft/learnintouch/rest因此,您的测试代码可以访问 src/main/java 中的所有内容以及src/test/java/com/thalasoft 中的所有内容/学习接触/休息
Your profile acceptancedefines the test source directory as src/test/java/com/thalasoft/learnintouch/rest/acceptanceSo, your test code can access everything in src/main/java and everything in src/test/java/com/thalasoft/learnintouch/rest/acceptance. WebTestConfiguration is not accessiblesince it's in a package above.
您的配置文件接受将测试源目录定义为src/test/java/com/thalasoft/learnintouch/rest/acceptance因此,您的测试代码可以访问 src/main/java 中的所有内容以及src/test/java/com/thalasoft 中的所有内容/learnintouch/rest/acceptance。WebTestConfiguration不可访问,因为它位于上面的包中。
To run specific tests with different profiles, I recommend to configure the surefire plugin in charge of running tests. A good example is available here : https://weblogs.java.net/blog/carcassi/archive/2011/04/21/running-integration-tests-and-unit-tests-separately-maven
要使用不同的配置文件运行特定的测试,我建议配置负责运行测试的 surefire 插件。这里有一个很好的例子:https: //weblogs.java.net/blog/carcassi/archive/2011/04/21/running-integration-tests-and-unit-tests-separately-maven
回答by Karibasappa G C
try to add below line in pom.xml which i feel it is missing
尝试在 pom.xml 中添加以下行,我觉得它丢失了
<profile>
<id>acceptance</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<test.source.dir>src/test/java/com/thalasoft/learnintouch/rest/config</test.source.dir>
</properties>
</profile>
回答by vijay
Problem Description:
问题描述:
I had similar problem with my project with below structure.
我的项目有以下结构的类似问题。
project
-pom.xml
-common-module
--src, pom.xml, etc
-web-module
--src, pom.xml, etc (but dependent on 'common')
mvn install
would run fine for common-module but gives compilation error for web-module code that uses java class from common-module (cannot find symbol
). I was using JDK 8 + Maven 3.2.5, but in pom compiler lever is set to 7.
mvn install
对于 common-module 会运行良好,但对于使用 common-module ( cannot find symbol
) 中的java 类的 web 模块代码会出现编译错误。我使用的是JDK 8 + Maven 3.2.5,但在 pom 编译器中杠杆设置为 7。
MY Solution:
我的解决方案:
I have installed JDK 7and Maven 3.1.1and did a mvn install
. Bingo..! Every with worked fine & Build Successful.
我已经安装了JDK 7和Maven 3.1.1并做了一个mvn install
. 答对了..!每一个都工作正常并成功构建。
I couldn't find a solution for my problem any where, so though of posting my fix here as it is related. (may be it could help some one)
我无法在任何地方找到我的问题的解决方案,所以尽管在这里发布我的修复程序,因为它是相关的。(也许它可以帮助某人)