eclipse Maven 突然想要 src/ 而不是 src/main/java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34015536/
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 suddenly wants src/ instead of src/main/java
提问by Aaron Digulla
I'm using Maven 3.3.3 together with Eclipse Mars and m2e. Yesterday, I created a new Java project and converted it into a Maven one.
我将 Maven 3.3.3 与 Eclipse Mars 和 m2e 一起使用。昨天,我创建了一个新的 Java 项目并将其转换为 Maven 项目。
Afterwards, the classpath contained only src
instead of the standard src/main/java
. I was surprised but I simply created the missing folders and ran m2e's "Update Project". This didn't solve the problem; m2e still insisted on using only src
.
之后,类路径只包含src
而不是标准的src/main/java
. 我很惊讶,但我只是创建了丢失的文件夹并运行了 m2e 的“更新项目”。这并没有解决问题;m2e 仍然坚持只使用src
.
So I edited the classpath manually to end up with with the usual
所以我手动编辑了类路径以得到通常的
src/test/java
src/test/resources
src/main/java
src/main/resources
When I try m2e's "Update Project" now, I get this error:
当我现在尝试 m2e 的“更新项目”时,出现此错误:
Cannot nest 'foo/src/test/java' inside 'foo/src'. To enable the nesting, exclude 'test/' from 'foo/src'
sighI went to the command line next and ran mvn eclipse:eclipse
to get this classpath:
叹气我接下来去了命令行并运行mvn eclipse:eclipse
以获取此类路径:
src/test/java
src/test/resources
src
src/main/resources
Now, I'm completely stumped. Why is that happening?
现在,我完全被难住了。为什么会这样?
回答by Aaron Digulla
Open your POM in the POM editor and click on the "Effective POM" tab. Search for sourceDirectory
. You'll probably see something like this:
在 POM 编辑器中打开您的 POM,然后单击“Effective POM”选项卡。搜索sourceDirectory
。你可能会看到这样的事情:
<sourceDirectory>src</sourceDirectory>
When you first converted the Java project to Maven, m2e tried to keep the classpath the same. Eclipse Java projects have a different layout by default. They use src/
instead of src/main/java/
. There is no test folder since Eclipse projects usually put their tests into a different project.
当您第一次将 Java 项目转换为 Maven 时,m2e 尝试保持类路径相同。默认情况下,Eclipse Java 项目具有不同的布局。他们使用src/
代替src/main/java/
. 没有测试文件夹,因为 Eclipse 项目通常将它们的测试放入不同的项目中。
To fix the issue:
要解决此问题:
- Delete the
sourceDirectory
element from the POM (Note: It might be in a parent POM). - Go to the project
- Select all the source folders
- Remove them from the build path (context menu -> Build Path -> Remove From Build Path).
- Update the project
sourceDirectory
从 POM 中删除元素(注意:它可能在父 POM 中)。- 前往项目
- 选择所有源文件夹
- 从构建路径中删除它们(上下文菜单 -> 构建路径 -> 从构建路径中删除)。
- 更新项目
The error should now be gone and the classpath should be correct.
错误现在应该消失了,类路径应该是正确的。
回答by Gene
There are two types of folders that you can create: 1) Package 2) Source Folder
您可以创建两种类型的文件夹:1) 包 2) 源文件夹
To create a package, right click on the folder you want (in my example, I right clicked on Java Resources) --> New --> Package
要创建包,请右键单击所需的文件夹(在我的示例中,我右键单击 Java 资源)--> 新建 --> 包
A pop up window that looks like this should appear
应该出现一个看起来像这样的弹出窗口
“Source folder:” That is where you choose your project. If you don't see anything there, click on “Browse…” and find your project. If I click “Browse…” it shows this:
“源文件夹:”这是您选择项目的地方。如果您在那里没有看到任何内容,请单击“浏览...”并找到您的项目。如果我单击“浏览...”,它会显示:
“Name:” is the name you will give your package.
“名称:”是您为包裹取的名称。
Note: Packages should be within Source Folders. Source Folders are the parent folder.
注意:包应该在源文件夹中。源文件夹是父文件夹。
2) Source Folders look like this:
To create a Source Folder, right click on the folder you want (in my example, I right clicked on Java Resources) --> New --> Source Folder
要创建源文件夹,请右键单击所需的文件夹(在我的示例中,我右键单击 Java 资源)--> 新建 --> 源文件夹
Under “Project name:” write your project's name. If you need help, click “Browse…”
在“项目名称:”下写下您的项目名称。如果您需要帮助,请单击“浏览...”
Under “Folder name:” write the folder name you want.
在“文件夹名称:”下写下您想要的文件夹名称。
Error: If you're trying to create a “src/main/resources” folder and you're running into this error “Cannot nest ‘ProjectName' inside ‘ProjectName'. To enable the nesting exclude ‘main/' from
错误:如果您尝试创建“src/main/resources”文件夹并且遇到此错误“无法在“ProjectName”中嵌套“ProjectName”。启用嵌套排除 'main/' from
Solution:
解决方案: