Maven project not being treated as Java in Eclipse

后端 未结 14 712
星月不相逢
星月不相逢 2021-02-05 04:30

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

相关标签:
14条回答
  • 2021-02-05 05:09

    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.

    0 讨论(0)
  • 2021-02-05 05:10

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

    0 讨论(0)
  • 2021-02-05 05:13

    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> 
    
    0 讨论(0)
  • 2021-02-05 05:15

    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!

    0 讨论(0)
  • 2021-02-05 05:15

    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

    0 讨论(0)
  • 2021-02-05 05:17

    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.

    0 讨论(0)
提交回复
热议问题