What is the difference between "Maven Install" and "Maven Build" with M2Eclipse?

RockTheStar picture RockTheStar · Jun 23, 2016 · Viewed 36.8k times · Source

I tried to search about the differences between maven install and maven build in the eclipse m2e plugin (if you right click the project and click "run as", you will see them), and I still cannot find a good explanation of them (I looked through the official document as well). Anyone can help? From what I currently understand:

  • maven install: build and install the artifacts in to the local repository
  • maven build: only build but not install? what does install mean then?

Also, when you checkout a repository online, do you usually do maven install (to install everything, including the dependencies) in order to run the code?

Answer

Tunaki picture Tunaki · Jun 23, 2016

First of all, build is not a phase in the standard Maven lifecycles, whereas install is one. mvn install will invoke all the phases up to the phase install, which generally consists of compiling the source code, packaging the project and installing it in the local repository.

To be clear, we're talking about what M2Eclipse shows in the "Run As" selection.

enter image description here

What are all those options? First of all, you need to be aware that you can:

Configure custom "Run Configuration"s in Eclipse

By going to:

enter image description here

This will open a dialog where you can configure those custom configurations.

enter image description here

You can create a new "Maven Build" run configuration, by giving it:

  • a name: this will be the unique name of the configuration. You can name it as you like. Above, it is named with the goals that it will invoke.
  • the base directory: this will be the folder where Maven will be invoked in. In the above screenshot, I used the Eclipse variable ${project_loc}, which is replaced automatically by the base directory of the current selected project in the "Project Explorer" when run. (This allows to have a single run configuration for multiple projects).
  • goals, potential profiles and several options: all those options will make up for the exact command that will be launched. Adding a profile will launch Maven with a -P... attribute; checking "Update Snapshots" will launch Maven with the -U flag, etc.

So what are all those "Run As" options?

Maven install

This is the simple one: "Maven install" will launch the configured Maven installation in Eclipse with the goal install. It will have the same effect as running the command mvn install on the command-line, with an external Maven installation.

The options "Maven generate-sources", "Maven test" or "Maven clean" are actually following the same idea: all of those will directly invoke Maven with the generate-sources phase, the test phase or the clean phase.

Maven build...

This will actually launch the previous dialog where we created a new run configuration. What happens is that M2Eclipse will create a new one, that you can fill exactly like above. You could see it as a short-cut for creating custom "Maven Build" run configurations.

Maven build

This will try to launch the configured custom run configurations.

  • If you have only one custom "Maven Build" run configuration, it will launch that one.
  • If you have more than one, it will ask you for the one to run:

    enter image description here

    In the above screenshots, you can see that there was 2 custom "Maven Build" run configuration, which were named clean and clean install. As such, this pop-up is asking the user to select one.

Once the custom "Maven Build" configuration was chosen, it will then invoke Maven with the options in this run configuration.