chopper draw lion4
chopper draw lion4

Reputation: 13507

What is the point of a Maven goal?

I come from a Ruby on Rails background and I am learning Java Spring MVC right now. When I try to compile my code using Maven in STS it wants me to include a goal. All the guides I read on this seem vague and unclear. What is the point of a goal? Why can't I just compile my source code and run it?

Upvotes: 1

Views: 71

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61198

You have to understand that Maven is complex and modular.

It does not have any concept of "compiling" your code or "running" your code. What it does is trigger plugins in the order of the build lifecycle.

Running a Maven goal triggers a particular Maven plugin. For example, running mvn compile triggers the Maven compiler plugin.

This all seems to be overkill for someone just starting in Java, and there are many "why can't Maven just do what its told" questions on SO.

Most of these stem from a fundamental misunderstanding of what Maven is. It is not (strictly speaking) designed to "compile and run" your code. It is designed to carry out a number of preconfigured steps in a particular order.

When it comes to "running your code", this gets even trickier:

  • to run your code with the maven exec plugin, call mvn exec:java. You obviously need to configure the plugin
  • to run your code as a webapp with an embedded Tomcat server, call mvn tomcat7:run-war.
  • to run your code as GWT application in devMode on an embedded webserver call mvn gwt:run

What all these goals have in common is that they trigger a specific, pre-configured, plugin that carries out the task you have asked for.

It is further worth pointing out that Maven is designed to compile, test and install your code, not really to run it. So whilst may plugins for running code do exist, the real strength of Maven comes from being able to automate the compiling, testing and deployment of code to a Maven repository.

As a final note, the massive red warning at the top of the page you linked says:

Apache Maven 1.x has reached its end of life, and is no longer supported.

Take heed. We're now on Maven 3.2.X. Don't read documentation for ancient and obsolete versions. This will serve no purpose but to confuse you.

Upvotes: 3

Related Questions