9

I have a few interdependent Eclipse projects. When I am trying to build my workspace, I am running into multiple issues. Supposedly, the issues should go if I do Maven clean, update, install, and Project clean, refresh, build all (I am doing everything in Eclipse through Run and Project menus correspondingly).

However, I am confused what should be the best order to perform these actions? Also, can anybody briefly explain what each of them does so I could get better understanding and make sense when I can skip some?

More specifically: I was told that running Maven Clean & Maven Install would suffice. Although Maven Install ends in 'Build Success', I still have errors on the Problems tab and a nasty error "Could not find or load main class export" when trying to run some classes in a particular project. I try running Project Clean couple times and then Project Build another couple times, and the errors might or might no go. I was advised to introduce Maven Update -> all projects and select all projects > Refresh (F5) steps, but I am not sure when to perform them (after what clean or install or build).

Is there any 'generally correct' order of how to perform these actions? So that if I perform it and some errors stay, it will become clear that something is wrong within my version rather than simply with the dependencies? I have pulled the projects from RTC (a software development team collaboration tool) and my colleagues do not have those problems. However, I just pulled it recently.

alisa
  • 1,336
  • 2
  • 15
  • 21

3 Answers3

15

First, you have to understand that maven and eclipse are two different things. Yes, there is the m2e plugin (that now comes bundled in eclipse) that does a very good job in configuring eclipse projects based on maven projects. But, sometimes eclipse gets messed up (almost always because of refresh problems).

Second, you may have to learn about Maven Build Lifecycle Basics:

There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's site documentation.

So, basically:

  • clean is a phase of the clean lifecycle, deletes your target directory
  • install is a phase of the default lifecycle, does a lot of things because triggers all the previous phases (generates sources, compiles, run tests, etc, and ends up putting your packages in your target directory and putting the artifacts in the local repository.

Third, about eclipse. You don't have to run Build if you have checked the "Project -> Build Automatically" option. When eclipse gets messed up, I found that executing the m2e command "Maven -> Update project" and then a Refresh, it's enough. This command regenerates some files in the .settings folder. In some rare cases, I have had to remove all the files in the .settings folder by myself, and then run the "Update Project" command.

Conclusion: mvn clean install should do the job, if not eclipse -> Refresh, if still not working eclipse - > Maven -> Update Project

I also recommend using the last version of eclipse and m2e plugin.

Pablo Lascano
  • 662
  • 6
  • 14
  • Agree, but just to add if its a maven based project (dependencies are taken from mvn repo) so we need to use mvn install as it will download all dependencies mentioned in pom but if all dependencies are available then eclipse clean -> build will also work. – Sanchi Girotra Aug 06 '18 at 06:21
  • what is the difference between mvn clean install and eclipse =>maven => update project – Indrajeet Gour Sep 05 '19 at 10:58
4

Specifically in my case, the following sequence of actions worked:

  • Project -> uncheck Build Automatically
  • [accept incoming changes] if applies
  • Project -> clean
  • Run -> Run As -> Maven clean
  • Run -> Run As -> Maven install
  • Project -> check build automatically
  • if errors on Problems Tab appear:
    • select all projects, Refresh F5
    • if still errors:
      • delete errors manually on Problems Tab (it might be that Eclipse has not updated the dependencies, etc.)
      • select all projects, Refresh F5
alisa
  • 1,336
  • 2
  • 15
  • 21
  • As for `Project -> Clean` step, I can see (in Eclipse Neon) its dialog has an option (a checkbox) `Start a build automatically` with two sub-options (radio buttons): (a) `Build the entire workspace` and (b) `Build only the selected projects`. What do you think about that? I assume the option `Start building automatically` should be unchecked, since you told us you have kept the `Build automatically` option off till you you've finished both `Maven clean` and `Maven install` – KiriSakow Nov 27 '17 at 09:18
  • 1
    @KiriSakow You need to uncheck it (`build automatically`) first, and then check it back as indicated in Step (1). Otherwise it'll try to build after each change and everything becomes messy and underscored in red... – alisa Dec 05 '17 at 15:51
-1

A best practice from devonfw.com is to separate and decouple command-line maven and eclipse build. This saved my life. The magic trick is simple and can be found here: https://github.com/devonfw/devon4j/blob/develop/pom.xml#L544

BTW: devonfw has tons of great patterns to learn from. This is just one out of a million.

Jörg
  • 822
  • 1
  • 10
  • 13