28

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?

FuriousGeorge
  • 4,561
  • 5
  • 29
  • 52
  • can you figure out then what's the difference between those two projects, the one where you do get RunAsJunit and the one where you don't get it? pom.xml probably has the answer. – milan Jan 17 '12 at 16:20

17 Answers17

42

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

Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
  • 2
    I found just deleting the .project files and re-importing the poms also fixed this - I cannot figure out how it gets into this bad state though. – bacar Apr 15 '15 at 17:37
  • 1
    But now all my imports and I have over 4000 build errors. – demongolem Feb 06 '17 at 22:24
  • @demongolem: Sorry, but how are your build errors related to my answer? If you're having build problems after adding a Java nature to your project, you may want to ask a separate question. – Amos M. Carpenter Feb 07 '17 at 03:37
  • @Amos I do not have a question. I am just saying that your answer does not work under all circumstances. – demongolem Feb 07 '17 at 12:59
  • 2
    @demongolem: Of course my suggestion won't fix any build issues "your imports and you" may have. (That's a completely separate problem, which is why I suggested a separate question.) What it will do is add a Java nature to a project that was previously not recognised as a Java project. Obviously once Eclipse tries to rebuild it, you may have to do other things to get it to build without errors, and obviously, if your project didn't build at all before and it does after you've added a Java nature, you'll have to resolve any build errors. My answer never pretended to resolve those for you... – Amos M. Carpenter Feb 07 '17 at 13:56
  • This answer is not efficient with a big project that has several modules. By adding a java facet editting the .project or with the user interface would make me go through each module of the project setting up the build path. With mvn eclipse:eclipse that is automatic. – nessa.gp Apr 01 '20 at 09:58
  • 1
    @nessa.gp I suggest you file a bug report with Eclipse, because this answer is essentially telling you how to work around an Eclipse bug – Jonathan Benn Jul 03 '20 at 12:29
21

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

Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
fmucar
  • 14,361
  • 2
  • 45
  • 50
  • I have the same problem, there is no Facets in properties – Amir-Mousavi Sep 15 '18 at 13:14
  • 2
    My project (imported as a Maven project and converted to a faceted project) only have Javascript and Static Web Module options - no Java facet. Any idea why the Java facet may be missing? I'm using Oxygen.2 Release (4.7.2). Thanks. – jsaven Oct 12 '18 at 21:28
15

mvn eclipse:eclipse will create necessary .project files which will add java nature & other properties to the project

V N
  • 353
  • 3
  • 8
  • 1
    It may also be necessary to perform a `mvn eclipse:clean` prior to the `mvn eclipse:eclipse` command. Finally `alt+F5` and the project was back on track in my eclipse workspace – Ghurdyl Apr 24 '18 at 14:23
  • This answer worked well me. Also I think its the cleanest approach. thanks ! – Tarun Gupta Jul 05 '19 at 09:38
  • 1
    This is such a better answer than the top one. By adding a java facet editting the .project or with the user interface made me go through each module of the project setting up the build path. With this instead it is automatic. – nessa.gp Apr 01 '20 at 09:56
  • [**Apache Maven Eclipse Plugin (RETIRED) - Note: This plugin is retired. It is no longer maintained.**](https://maven.apache.org/plugins/maven-eclipse-plugin/) – Gerold Broser Feb 20 '23 at 14:33
6

I was facing the same problem and the steps below solved it in my case:

1- Open the pom.xml of the problematic project.

2 -In the overview tab, check the Artifact/Packaging settings.

3- If it is set to "pom" change it to "jar".

4- Save your changes and then delete the project from eclipse only and re-import it.

Hope that helps!

Rami
  • 7,162
  • 1
  • 22
  • 19
  • That solved my problem. My pom was copied from somewhere and I didn't realize it contained "pom" until I found this answer. – Patter Apr 07 '22 at 13:35
4

It depends on the Eclipse project facet properties (if you are using Java EE), I suggest you right click on project properties and see which facet is defined for your project.

Peter Ford
  • 583
  • 4
  • 6
RoiG
  • 478
  • 3
  • 11
3

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.

Re-importing maven project after deleting ".settings" folder and ".project" file did not work. Re-importing after a workspace switch worked for me.

chao_chang
  • 778
  • 8
  • 14
2

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.

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.

FuriousGeorge
  • 4,561
  • 5
  • 29
  • 52
1

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

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

<?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> 
kanaparthikiran
  • 523
  • 12
  • 15
1

I know that this question was a long time ago...

I just resolved the issue by these 2 steps below.

  1. run cmd "mvn eclipse:eclipse" in the root directory of the project
  2. right-click on the project in eclipse then select "import as project"
Dharman
  • 30,962
  • 25
  • 85
  • 135
Hieu
  • 41
  • 2
  • [**Apache Maven Eclipse Plugin (RETIRED) - Note: This plugin is retired. It is no longer maintained.**](https://maven.apache.org/plugins/maven-eclipse-plugin/) – Gerold Broser Feb 20 '23 at 15:21
1

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:

<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

Community
  • 1
  • 1
Richard Neish
  • 8,414
  • 4
  • 39
  • 69
0

My five cents... In my case I was 'losing' always the 'Java' nature of the project because the pom file was set to 'package as ear'. There was no error or warning message though, which would be nice to have. As soon as I changed the package to 'jar' I could set the nature of the project to Java and it is persisted.

daniel sp
  • 937
  • 1
  • 11
  • 29
0

I also tried all the approaches described in this thread. Modifying only .project is not enough because the maven libraries will not be used for building the project and the project build will fail. mvn eclipse:eclipse will create a long and useless .classpath without actually using m2e.

The easiest way is to create a new java project, convert it to the Maven project and open the .project and .classpath for this new project. Then make similar changes for the imported Maven project.

The important changes for the .project file have been described but in order to use the Maven dependencies in Eclipse something like this should be in the .classpath file

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>

Using facets is not an option in this case because with facets Eclipse doesn't use m2e and the maven dependencies. Again the dependencies with facets have to be managed manually.

I tried all this with Eclipse 4.6.2 Neon.

Xaltotun
  • 264
  • 2
  • 7
0

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.

Serge Bogatyrev
  • 818
  • 4
  • 5
0

If you have tried Amos M. Carpenter's solutions on the .project and didn't work, go to Project -> Properties -> Project Nature and Add... Java, Apply and Close button.

0

Nothing above helped me. After hours of trying (shortly before crying) I imported the projects not by "Import from existing maven projects" but instead with "Projects from Git".

Everything is fine now. The Java Projects are Java Projects again!

  • ... and when the projects finally don't compile, don't be afraid, switch to a new workspace (copy your preferences) and import the projects again. Good luck! – Martin J Schmidt Jan 28 '22 at 21:27
0

Right click the project and click on the Properties --> Select Maven --> (In active maven profiles) give "pom.xml", Apply and close. enter image description here

0

Go to u r project folder and delete .settings and rebuild the maven application then try with running project

Good Luck

ANiket Chavan
  • 99
  • 1
  • 3