167

I am about to start a Java project just for practice. I've read about Maven, but I don't actually understand when it is meant to be used.

Can you give me some practical tips? Does Maven help a lot? What does Maven actually do for my project?

Sled
  • 18,541
  • 27
  • 119
  • 168
Piotr Krysiak
  • 2,815
  • 3
  • 23
  • 35
  • @ArtB gives a good explanation of what Maven is. However, as i say in my comment to his answer, i don't think you should use it. If you want a build tool, use Gradle. Download it, install it, then in your project create a file called `build.gradle` that just contains one line saying `apply plugin: 'java'`. Assuming your source code is in the usual place (`src/main/java`, unit tests in `src/test/java`), you can now build, test, and package your code by saying `gradle build`. Easy! – Tom Anderson Nov 11 '12 at 21:20
  • 2
    To use Gradle you'll need to learn Groovy and you definitely _will_ come across project using Maven so it's still worth learning. Also, understanding Maven will make understanding other tools that came after it easier. – Sled Nov 11 '12 at 21:50
  • Hej, have a look at [this question](http://stackoverflow.com/questions/7189127/sts-spring-with-roo-and-maven-too-slow-tips-and-tricks-needed) of mine about Maven, Spring and Roo. I used Maven a lot when working with Eclipse and in order to speed up development time. But there are some issues. I think you need to understand Java annotation system together with Maven. But have a look at the question I linked.. – mm24 Nov 11 '12 at 20:56
  • What does Maven have to with Java annotations at all? – Sled Nov 11 '12 at 21:51
  • 1
    +1 for Gradle, not only it is closer to home for java developers, it can reuse maven artifacts and it can make non standard builds easily http://stackoverflow.com/questions/1163173/why-use-gradle-instead-of-ant-or-maven – Kalpesh Soni Mar 12 '15 at 19:55
  • I wrote briefly what is it on my blog. You can check it here https://eagertoit.com/2018/09/02/maven/ – Michal Cholewiński Sep 04 '18 at 12:24

3 Answers3

193

What it does

Maven is a "build management tool", it is for defining how your .java files get compiled to .class, packaged into .jar (or .war or .ear) files, (pre/post)processed with tools, managing your CLASSPATH, and all others sorts of tasks that are required to build your project. It is similar to Apache Ant or Gradle or Makefiles in C/C++, but it attempts to be completely self-contained in it that you shouldn't need any additional tools or scripts by incorporating other common tasks like downloading & installing necessary libraries etc.

It is also designed around the "build portability" theme, so that you don't get issues as having the same code with the same buildscript working on one computer but not on another one (this is a known issue, we have VMs of Windows 98 machines since we couldn't get some of our Delphi applications compiling anywhere else). Because of this, it is also the best way to work on a project between people who use different IDEs since IDE-generated Ant scripts are hard to import into other IDEs, but all IDEs nowadays understand and support Maven (IntelliJ, Eclipse, and NetBeans). Even if you don't end up liking Maven, it ends up being the point of reference for all other modern builds tools.

Why you should use it

There are three things about Maven that are very nice.

  1. Maven will (after you declare which ones you are using) download all the libraries that you use and the libraries that they use for you automatically. This is very nice, and makes dealing with lots of libraries ridiculously easy. This lets you avoid "dependency hell". It is similar to Apache Ant's Ivy.

  2. It uses "Convention over Configuration" so that by default you don't need to define the tasks you want to do. You don't need to write a "compile", "test", "package", or "clean" step like you would have to do in Ant or a Makefile. Just put the files in the places in which Maven expects them and it should work off of the bat.

  3. Maven also has lots of nice plug-ins that you can install that will handle many routine tasks from generating Java classes from an XSD schema using JAXB to measuring test coverage with Cobertura. Just add them to your pom.xml and they will integrate with everything else you want to do.

The initial learning curve is steep, but (nearly) every professional Java developer uses Maven or wishes they did. You should use Maven on every project although don't be surprised if it takes you a while to get used to it and that sometimes you wish you could just do things manually, since learning something new sometimes hurts. However, once you truly get used to Maven you will find that build management takes almost no time at all.

How to Start

The best place to start is "Maven in 5 Minutes". It will get you start with a project ready for you to code in with all the necessary files and folders set-up (yes, I recommend using the quickstart archetype, at least at first).

After you get started you'll want a better understanding over how the tool is intended to be used. For that "Better Builds with Maven" is the most thorough place to understand the guts of how it works, however, "Maven: The Complete Reference" is more up-to-date. Read the first one for understanding, but then use the second one for reference.

kgui
  • 4,015
  • 5
  • 41
  • 53
Sled
  • 18,541
  • 27
  • 119
  • 168
  • 14
    There are numerous professional Java developers who don't use Maven and are glad they don't; some of these simply reject its complexity and inflexibility and stick with Ant or similar, but a growing number of them are people who have moved on to Maven successors such as Gradle and Buildr. These successors inherit from Maven the idea of providing a powerful set of build steps out of the box, but make it immensely easier to add custom steps too. Given their existence, i honestly don't think there is any reason to use Maven for new projects. – Tom Anderson Nov 11 '12 at 21:16
  • 5
    There is always the question of how much detail to put in an answer, but if you are an average Java developer that in your office it's likely either Maven or shitty Ant hell (Ant + bash + perl + build computer configured with magical paths). If you are one of the lucky few working with A+ developers in a good environment with freedom to chose there are other options (Gradle being one). But if you are working in an average place Maven is the best tool that doesn't let your fellow co-workers cut themselves with something pointy. – Sled Nov 11 '12 at 21:23
  • @TomAnderson I'll add that I still prefer Maven to other tools since I like more formal models since they are easier to use correctly and harder to accidentally paint yourself into a corner or slide down the slippery-slope of building your own buggy tool-on-top-of-a-tool like I've seen with Ant (entire files using not a single stock tag besides ``)! – Sled Nov 11 '12 at 21:47
  • 4
    I don't think you have to be an A+ developer to use Gradle. I don't see why an organisation that could adopt Maven couldn't adopt Gradle. It's true that Gradle lets you shoot yourself in the foot, in the same way Ant does, but in a way that Maven makes quite hard (and i have a collection of my colleagues' shot feet to prove it); that is the tradeoff for making it possible to do things that Maven makes virtually impossible. – Tom Anderson Nov 11 '12 at 21:58
  • 4
    In terms of using something like Gradle and avoiding shot feet, i propose a triage classification of programmers. Some programmers won't be afraid to edit the build scripts, and get things more or less right. Those people are useful. Some programmers are afraid of the build scripts, and won't touch them. Those people at least aren't harmful; i've been in a 12-man team where only two of us dared touch the build, and it actually worked rather well. Some programmers aren't afraid of the build scripts, but will screw them up if they touch them. Those people are dangerous. – Tom Anderson Nov 11 '12 at 22:01
  • You need to know **a lot** of Maven to be dangerous, but with Gradle only a little. I can easily imagine coming back to project after a year to find it having decayed into a spaghetti of strangely named tasks with duplication and bizarre names. – Sled Nov 11 '12 at 22:09
  • 2
    so it's like npm for Java – KJW Oct 09 '13 at 00:00
  • 1
    @KimJongWoo If you are referring to npm files for Node.js then yes, Maven does include that, but it also manages the whole build lifecycle and is a platform for plug-ins. – Sled Oct 09 '13 at 13:40
  • 1
    @KJW if you want to compare it to the Javascript world, Maven is somewhat of a combination of NPM + Grunt/Gulp/Webpack, all rolled into one solution. It handles dependency management (NPM) as well as build management (Grunt/Gulp/Webpack). – Danny Bullis Mar 27 '18 at 15:54
11

From the Sonatype doc:

The answer to this question depends on your own perspective. The great majority of Maven users are going to call Maven a “build tool”: a tool used to build deployable artifacts from source code. Build engineers and project managers might refer to Maven as something more comprehensive: a project management tool. What is the difference? A build tool such as Ant is focused solely on preprocessing, compilation, packaging, testing, and distribution. A project management tool such as Maven provides a superset of features found in a build tool. In addition to providing build capabilities, Maven can also run reports, generate a web site, and facilitate communication among members of a working team.

I'd strongly recommend looking at the Sonatype doc and spending some time looking at the available plugins to understand the power of Maven.

Very briefly, it operates at a higher conceptual level than (say) Ant. With Ant, you'd specify the set of files and resources that you want to build, then specify how you want them jarred together, and specify the order that should occur in (clean/compile/jar). With Maven this is all implicit. Maven expects to find your files in particular places, and will work automatically with that. Consequently setting up a project with Maven can be a lot simpler, but you have to play by Maven's rules!

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • I didn't downvote it, but perhaps downvoters read your quotation from Sonatype propaganda as an implicit endorsement of Maven's totalitarian "project management tool" philosophy. – Tom Anderson Nov 11 '12 at 21:11
  • 1
    I don't feel that you answered the OP question, instead you just posted the marketing spiel... and that spiel is _rediculous_ since "project management" is so much much *more* than what Maven does it serves to confuse more than it helps, IMHO. – Sled Nov 11 '12 at 21:17
  • 2
    @Brian Sorry, if my previous comment sounds flamey, but let me rephrase it: I feel your comment only explains Maven well _if_ you already understand what Maven is, but as an initial starting point it isn't helpful. – Sled Nov 11 '12 at 21:25
7

Maven is a build tool. Along with Ant or Gradle are Javas tools for building.
If you are a newbie in Java though just build using your IDE since Maven has a steep learning curve.

CodeSlave
  • 425
  • 6
  • 21
Cratylus
  • 52,998
  • 69
  • 209
  • 339