This is the first article of the Maven tutorial series for beginners. For those interested in building their Java projects on top of Maven structure, the articles in the series may help.
Preface
For Java developers, Maven is not an alien word. It is much known as a very powerful tool used to build and manage Java projects with varying size and complexities. Aside from being powerful, it is also pervasive, which is exhibited by substantial presence across projects and entities. It is used by individual developers whose projects are being shared in the public repositories to big enterprises with a walled garden protecting the codebase.
This article is targeted to Maven first-timers who are fiddling with Maven on Windows and are in need of guide to installing, configuring, and using Maven for their next Java project. Even though Maven can work across different versions of Windows, the articles and reference images were created in the environment as specified below:
Operating System: Windows 7
Maven version: 3.x.x
Java version: 1.7+
Quick Introduction of Maven.
Maven divides a Java project into several stages in a lifecycle and help developers navigate through each stage until the piece of software is readily installed, deployed, released or distributed. As its name hints, it may amaze the users with what it offers to ease and simplify the Java project development and management. In some circumstances, however, it may surprisingly baffle and seem inflexible; it lets the users get deserted in complexities and adamancy in which users are enforced to accomplish things by going the Maven way instead of creatively going through users’ own approach.
Despite the mixed feelings of love and hate, Maven indeed can do a lot especially when developing a large project with multiple modules. Its biggest strength -but sometimes is also a weakness- can probably be the dependency management feature. In a large Java project setting, dependencies can be complex and hard to manage over a long period of time. Each module oftentimes requires additional libraries a.k.a. “the dependencies” to build. When the project is big, the size of the dependency list grows naturally -and painfully. Maven comes to relieve by providing a mechanism to resolve the dependencies through a set of conventions thus reducing time required for manual supervision of the dependency list.
Yet, Maven is not only about dependency management, it’s a full-fledged project management tool that assist developers from source code compilation to distribution of output packages (jars, zip, binary, etc) into designated repository. To achieve such purpose, Maven incorporates several phases built into its operational cycle. This cycle is also known as Maven build lifecycle. The default lifecycle consists of the following phases:
- validate: validate the appropriateness of the project
- compile: compile the project source code
- test: test compiled source code with written unit tests
- package: package the compiled code in distributable format, e.g. JAR
- integration-test: conduct an integration test by deploying the package into a test environment
- verify: ensure the package is valid by running some checks
- install: install the package into local repository
- deploy: copy the final package to remote repository for sharing with other developers and projects
The phase should be executed in sequential order. When executing a phase (for example by invoking the command “mvn install”), Maven will execute all the preceding phases in sequence until it reaches the phase represented by the command. Additional option can also be supplied when invoking the command. The supplied option will configure and alter the behavior of Maven when executing its build lifecycle. For example, adding -o option as in “mvn install -o” will make Maven execute all the phases in offline mode. When a module lists some dependencies in its build, these dependencies will be searched from local repository (i.e. local disk) only and no internet connection will be made to the public repositories to retrieve the dependencies.
To prevent too much elaboration about the concept behind Maven, it’s recommended for those who are interested in minuscule details to refer to the official documentation. The philosophical read about Maven can be found inside its guide page.