Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven project not being treated as Java in Eclipse

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.

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?

like image 828
FuriousGeorge Avatar asked Jan 17 '12 16:01

FuriousGeorge


People also ask

How do I add a Java project to a Maven project in Eclipse?

In your eclipse just right click on Java Project and click Configure and you should see “ Convert to Maven Project ” option. You should see dialogue like this below. Just add “ Name ” and you should be all set. Don't forget to add your all custom dependencies in pom.

Is Maven project a Java project?

Maven is written in Java and is used to build projects written in C#, Scala, Ruby, etc. Based on the Project Object Model (POM), this tool has made the lives of Java developers easier while developing reports, checks build and testing automation setups.


2 Answers

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.

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:

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

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

like image 154
Amos M. Carpenter Avatar answered Oct 10 '22 04:10

Amos M. Carpenter


Right-click on Project -> Properties -> Project Facets, then choose all facets that apply for your case (e.g. java).

like image 36
fmucar Avatar answered Oct 10 '22 05:10

fmucar