Maven 项目在 Eclipse 中不被视为 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8897907/
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 project not being treated as Java in Eclipse
提问by FuriousGeorge
I'm working on a project that has lots of different Maven projects. I've been doing a bunch of JUnit testing on these projects, and usually this goes well. I open up Eclipse, right click in package explorer->Import... Existing Maven Projects and I'm able to import the project fine. I can build, drill down to src/test/java... Right click on the file and do a Run As JUnit test. Every now and then though, I can't get this to work. If I right click to do a Run As, all I get is AspectJ/Java application. There's no JUnit tests.
我正在处理一个包含许多不同 Maven 项目的项目。我一直在对这些项目进行大量 JUnit 测试,而且通常都很顺利。我打开 Eclipse,右键单击包资源管理器-> 导入...现有 Maven 项目,我可以很好地导入项目。我可以构建,深入到 src/test/java... 右键单击该文件并执行 Run As JUnit 测试。但时不时地,我无法让它发挥作用。如果我右键单击执行运行方式,我得到的只是 AspectJ/Java 应用程序。没有 JUnit 测试。
I noticed that the icon next to the project folder only has an M and a folder icon, whereas with projects that do work, there's a folder, M, AND a AJ. I've also noticed that it doesn't seem to sort the files into their packages like normal Java projects. It seems like it's not treating the project as an AspectJ project. How do I get Eclipse to recognize this Maven project as a Java project?
我注意到项目文件夹旁边的图标只有一个 M 和一个文件夹图标,而对于可以工作的项目,有一个文件夹、M 和一个 AJ。我还注意到它似乎不像普通的 Java 项目那样将文件分类到它们的包中。似乎它没有将该项目视为 AspectJ 项目。如何让 Eclipse 将此 Maven 项目识别为 Java 项目?
回答by Amos M. Carpenter
Several of the existing answers should work, but those suggesting to add a Java facet will only work if your project already is of (or you want to convert it to) a faceted nature, and the one suggesting you change your pom.xml
will only work if you then re-import your project as a maven project.
一些现有的答案应该有效,但是那些建议添加 Java 方面的建议仅在您的项目已经是(或您想将其转换为)多面性质的情况下才有效,而建议您更改的建议pom.xml
仅在以下情况下有效然后将您的项目重新导入为 Maven 项目。
If you'd like to "fix" your existing project, i.e. add a Java nature without converting it to faceted form and without deleting and re-importing, just edit your .project
file (externally, or with the Navigator view in eclipse, as it won't show up in your package explorer) and add the java builder and java nature to the existing maven builder/nature:
如果您想“修复”现有项目,即添加 Java 性质而不将其转换为分面形式,也无需删除和重新导入,只需编辑您的.project
文件(在外部,或使用 Eclipse 中的导航器视图,因为它赢得了't 出现在你的包浏览器中)并将 java builder 和 java nature 添加到现有的 maven builder/nature:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>yourprojectname</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<!-- add this build command -->
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<!-- this would've already been there -->
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<!-- add this nature -->
<nature>org.eclipse.jdt.core.javanature</nature>
<!-- this would've already been there -->
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Note the different capitalisation (javabuilder but maven2Builder, javanature but maven2Nature).
注意不同的大小写(javabuilder 但 maven2Builder,javanature 但 maven2Nature)。
As soon as you save, it should re-build automatically. You may have to manually add any source folders (Project properties
-> Java Build Path
-> Source
tab -> Add Folder...
.
保存后,它应该会自动重新构建。您可能需要手动添加任何源文件夹(Project properties
-> Java Build Path
-> Source
tab -> Add Folder...
.
回答by fmucar
Right-click on Project
-> Properties
-> Project Facets
, then choose all facets that apply for your case (e.g. java).
右键单击Project
-> Properties
-> Project Facets
,然后选择适用于您的案例的所有方面(例如 java)。
回答by V N
mvn eclipse:eclipsewill create necessary .project files which will add java nature & other properties to the project
mvn eclipse:eclipse将创建必要的 .project 文件,这些文件将向项目添加 java 自然和其他属性
回答by RoiG
It depends on the Eclipse project facetproperties (if you are using Java EE), I suggest you right click on project properties and see which facet is defined for your project.
这取决于Eclipse 项目方面的属性(如果您使用的是 Java EE),我建议您右键单击项目属性并查看为您的项目定义了哪个方面。
回答by Rami
I was facing the same problem and the steps below solved it in my case:
我遇到了同样的问题,下面的步骤在我的情况下解决了这个问题:
1- Open the pom.xml of the problematic project.
1- 打开有问题的项目的 pom.xml。
2 -In the overview tab, check the Artifact/Packaging settings.
2 - 在概览选项卡中,检查工件/包装设置。
3- If it is set to "pom" change it to "jar".
3-如果设置为“pom”,请将其更改为“jar”。
4- Save your changes and then delete the project from eclipse only and re-import it.
4- 保存更改,然后仅从 Eclipse 中删除项目并重新导入。
Hope that helps!
希望有帮助!
回答by chao_chang
I faced the same problem(after importing pom.xml to eclipse,the project is not treated as a java one,and there is only "maven project builder" in project builders property,but no "java builder") in eclipse luna.
我在eclipse luna中遇到了同样的问题(在将pom.xml导入eclipse之后,该项目不被视为java项目,项目构建器属性中只有“maven project builder”,但没有“java builder”)。
Re-importing maven project after deleting ".settings" folder and ".project" file did not work. Re-importing after a workspace switch worked for me.
删除“.settings”文件夹和“.project”文件后重新导入maven项目不起作用。在工作区切换后重新导入对我有用。
回答by FuriousGeorge
I talked to a co-worker and I was able to "fix" the problem. I did a delete from Eclipse (not from disk) and immediately re-did the Maven import and now it magically works.
我和一位同事谈过,我能够“解决”这个问题。我从 Eclipse(而不是从磁盘)中删除并立即重新进行了 Maven 导入,现在它神奇地工作了。
It seems like if there was an error with the pom.xml, particularly if the parent version was wrong, that the maven project doesn't get imported/created properly. Once I fixed the problems in the POM, the project would build fine without any problems but it was still only a Maven project. Once I removed it and re-imported it, THEN it treated it as a Maven/AspectJ project.
似乎如果 pom.xml 有错误,特别是如果父版本错误,则 Maven 项目无法正确导入/创建。一旦我修复了 POM 中的问题,项目就会正常构建而没有任何问题,但它仍然只是一个 Maven 项目。一旦我删除它并重新导入它,那么它将它视为一个 Maven/AspectJ 项目。
回答by Richard Neish
Check the pom.xml
file for projects that don't get the AspectJ nature (AJ on the project). Are they missing the AspectJ-Maven plugin? They should have a section like:
检查pom.xml
文件中没有获得 AspectJ 性质的项目(项目上的 AJ)。他们是否缺少 AspectJ-Maven 插件?他们应该有一个像这样的部分:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<complianceLevel>1.6</complianceLevel>
</configuration>
</plugin>
Also take a look at his question: Maven/AJDT project in Eclipse
也看看他的问题:Eclipse中的Maven/AJDT项目
回答by kanaparthikiran
There are two ways this can be achieved.
有两种方法可以实现这一点。
1) By Changing the project nature. Right-click on Project -> Properties -> Project Natures, then choose all natures that apply for your case (e.g. java).
1)通过改变项目性质。右键单击 Project -> Properties -> Project Natures,然后选择适用于您的案例的所有性质(例如 java)。
(OR)
(或者)
2) By updating the .project file and add all the natures that are required for the project. This .project file is located at the root folder of the project. If the file does not exist for any reason, create a new file, and add the sections for each nature that is required e.g., as follows.
2) 通过更新 .project 文件并添加项目所需的所有性质。此 .project 文件位于项目的根文件夹中。如果该文件因任何原因不存在,请创建一个新文件,并为所需的每种性质添加部分,例如,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>yourprojectName</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
回答by Serge Bogatyrev
I had the same problem. In my case it was some defect in eclipse workspace. I recreated it (deleted .metadata directory) and that fixed the issue.
我有同样的问题。就我而言,这是 eclipse 工作区中的一些缺陷。我重新创建了它(删除了 .metadata 目录)并解决了这个问题。