Difference between Eclipse Build Project and Maven Compile command

Arthas picture Arthas · May 15, 2015 · Viewed 18.9k times · Source

Is Eclipse "Build Project" command same as the Maven command "mvn compile"? Do both basically do the same thing?

If Yes, then why do I need to do a "Build Project" in STS after running "mvn clean install" in order to run the application without any issues? Running "mvn clean install" should have already compiled the project. Shouldn't Refreshing the project in STS be enough to run it?

If No, then is Eclipse build different because the Java compiler implements the Java Language Specification to build the classes? But the following Apache Maven link says that the default compiler is javax.tools.JavaCompiler (By the way I am using Java 1.6).

Answer

Kris picture Kris · May 15, 2015

The short answer is no, maven build and eclipse build are not the same.

Basically, eclipse has its own way of building things, which has little to do with maven. At the most basic level, Eclipse just does Java compilation, using its own Java compiler (part of Eclipse JDT).

A precise answer to how they differ is hard to give, the situation is quite complex, and it depends precisely on what stuff (Eclipse plugins) you have installed.

To get an as close as possible approximation so that what Eclipse does, resembles most to what maven does on the commandline you should install m2e (maven eclipse tooling).

M2E tries to make your Eclipse IDE's behavior 'emulate' as closely as possible to maven's commandline behavior. It does this by configuring your eclipse project. For example, setting source folders, classpath etc. based on the maven poms. This works pretty well if your poms don't do 'fancy' things (i.e. use some not so common maven plugins).

When you do use maven plugins in your pom to do 'special' things like maybe generate some code, or whatever, then m2e has a plugin mechanism that allows maven plugin authors to define a corresponding eclipse plugin which 'teaches eclipse' how to do the same thing.

This can get hairy, because not all maven plugins have corresponding Eclipse plugins, and even if they do, they are not automatically installed for you into your instance of Eclipse.

If you haven't got the plugins to 'teach eclipse' about some of your pom's plugin. M2e will give you an error about lifecyle mapping. This is an indication that m2e and maven commandline may not 'do the same thing' for your project, and its up to you to resolve it somehow (e.g by installing the corresponding Eclipse 'project configurator').