23

We are getting a very weird behavior in our development environment that is consistent with all of our developers on different operating systems.

We have about 20+ Maven (3.0.4) projects in the development environment, all of them are open projects in Eclipse (Indigo) with sonatype m2e (0.12.0) handling dependencies as usual. (m2e 1.0 is causing us more problems than solutions)

Out of all our 20+ projects there is one project that's acting weird. When performing mvn clean install on that project, even though Maven passes successfully, it causes 4 java files (in the unit tests, if it makes any difference) to show errors in Eclipse.

The errors are of the type "SomeNameOfClass cannot be resolved to a type" although opening the file and pressing F3 (Open declaration) on the erroneous class reference finds the class without a problem.

mvn clean is the problem, if we just run mvn install this doesn't happen.

Eclipse's Project -> Clean clears the errors and everything is ok.

This is not an operational problem that actually prevents me from working or anything like that, I can solve it simply by cleaning in Eclipse, I just hate doing that every time and I can't stand red Xs in my projects even if they have no effect.

I'm just really really curious why this is happening at all, why specifically those 4 classes? why why why? :)

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Enrico
  • 621
  • 1
  • 4
  • 12
  • Can you give more precisions about those test classes? are there other test classes that behave as expected ? do they have a specific path ? – Olivier.Roger Mar 14 '12 at 08:52
  • As mentioned before you should give more details..Are those files generated ? – khmarbaise Mar 14 '12 at 08:55
  • The files are regular java files, not generated. They are suffixed with Test and contain a few methods that are annotated with @Test as normal JUnit classes are.. The class that is referenced that is shown as `cannot be resolved to a type` is abstract – Enrico Mar 14 '12 at 09:13
  • Another things to notice is that while errors in java file X are `Y cannot be resolved to a type`, there are no errors in Y. – Enrico Mar 14 '12 at 09:17
  • Do you import your project as a Java or Maven project? you can tell this by check the small project icon in Package Explorer: J on the top right for Java or M on the top left for Maven. – yorkw Mar 14 '12 at 09:43
  • Both. The icons are not determined by how you import your project, it is determined by the project natures defined in the .project file, our projects have both natures. – Enrico Mar 14 '12 at 11:46
  • The style of .project file with some other IDE config files are generated based on how you import your project, I don't know how you did this, but normally we don't manually alter .project to use both natures, as the IDE build life cycle could be malformed by multiple natures (your malformed .project file probably come from SVN checkout, which IMO put those IDE generated file into SVN is a very bad practice). Check out my answer [here](http://stackoverflow.com/questions/9457860/importing-projects-into-eclipse/9460188#9460188) too see how to import project to IDE properly. – yorkw Mar 14 '12 at 20:32
  • you saved my day with your "Eclipse's project->clean clears the errors and everything is ok." THANK YOU! – damko May 31 '12 at 07:12

4 Answers4

24

We have had the exact same problem a while ago. We had more than 20 projects giving the same kind of error. From the investigation we did, we concluded that when maven clean install is run, eclipse loses track of the class files and thinks some of them are undefined. The solution we had is to issue the following at the command line:

mvn eclipse:clean
mvn clean install
mvn eclipse:eclipse
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
GETah
  • 20,922
  • 7
  • 61
  • 103
  • 6
    If m2e or m2eclipse is used mvn eclipse:... shouldn't be used anymore. The better way would be to do an Update-Project-Configuration in the Maven - Context menu. – khmarbaise Mar 14 '12 at 09:05
  • 2
    @GETah While what you're suggesting removes the errors, it screws up the .project and .classpath. Those files are in our source control and changing them on every build is not a good option. I suppose I could do that and after everything perform a `git checkout -- .project` and `git checkout -- .classpath` but that seems a bit cumbersome. – Enrico Mar 14 '12 at 09:10
  • 7
    @Enrico I think having the eclipse settings under version control is not a good idea. You should only keep your source code along with the pom files under source control. – GETah Mar 14 '12 at 09:31
  • 2
    @GETah having the .project and .classpath in the source control synchronizes everyone's settings, it allows our non java savvy developers to simply import a project and have everything just work.. – Enrico Mar 14 '12 at 09:34
  • I Chose a correct answer because it does solve the problem if you're not source controlling the .project and .classpath... – Enrico Mar 20 '12 at 09:32
  • It seems mvn eclipse:clean can still be helpful in an m2e environment. Sure enough, it deleted my .project and .classpath files, but when I checked them out again (discarded their deletions in git), Eclipse rebuilt the project successfully! Maven → Update Project Configuration and Maven → Update Dependencies had previously failed, as had cleaning and a whole bunch of other things I tried. I'm pretty sure I hadn't changed those files, so mvn eclipse:clean apparently gave m2e a helpful kick up the bum. – Michael Scheper Jan 28 '14 at 02:09
  • agreed. we used run out memory in m2e, command line outside the eclipse helped. – Junchen Liu Apr 08 '15 at 13:35
  • Thanks. It helped me. And if running those commands convert your maven project to normal java project. Convert back it to maven project by - Right click on project -> Configure -> Convert to Maven Project – JavaYouth May 26 '17 at 06:31
0

This is now possible to specify that project(s) needs a refresh upon completion under Refresh tab in Run configuration. You will have to check Refresh resources upon completion and select which behavior below.

Ludovic Guillaume
  • 3,237
  • 1
  • 24
  • 41
0

I've found that having a jar in maven as 'test' scope will cause the "unresolved type" error and nothing I've found yet will fix it other than take it out of test scope in the pom file.

0

If you want to get rid of errors through UI, do the following steps:

Select your project in eclipse and do ALT + ENTER OR right click on project -> Build path -> Configure Build Path

Select Java Build Path option and Source tab.

Select node(s) for which it shows error e.g. src/main/java and click Remove and Click Apply.

Again add the same(removed) node(s) using Add Folder option and click Apply and Close button.

Error(s) should have disappeared!

Abhi
  • 53
  • 1
  • 7