How Maven Enforces Good Habits
One of the biggest leaps forward in my productivity and enjoyment as a developer came when I learned about and adopted Maven for my Java projects.
For various reasons I was relatively late to the Maven party, but nowadays I could not imagine developing enterprise Java projects without it. When I am working with other languages and toolsets, I always look to adopt similar tools to automate my workflows.
If you are not familiar, Maven is a tool for managing the lifecycle of a Java project. Think compilation, testing, packaging, release, deployment and dependency management. All of the stuff that goes around the edge of simply writing code.
This can be an incredible time saver, helping to automate builds and making them much more repeatable. However, I am also struck by the way the Maven workflow enforces good habits:
- Automation. The number one win with Maven is the automation it gives you. Without writing a line of code or configuration, you can compile, build project artifacts and run tests by running a single command. This works on your machine, your colleagues machines, and on your build and CI servers.
- Convention over configuration. Maven has opinions about where source code, tests and resources live. Those opinions remove a great deal of ceremony and make a project easier for a new developer to read.
- Testing. Maven has a specific phase explicitly for running tests. If you do not have unit tests, Maven makes you feel guilty each time you see that phase go by. If you do have them, they had better be up to scratch, because by default it will fail the build if your tests fail. Maven drills home the fact that testing and verification are part of what we are doing.
- Separate integration testing. Maven specifically separates out the integration testing phase of the lifecycle and gives you the tools to perform good integration testing, for example by bringing up and tearing down environments cleanly.
- Small, loosely coupled components. Maven encourages you to produce small defined components which are bundled into separate artifacts. These are then reusable.
- Consistency. Maven encourages consistency within the project team. It reduces the special cases that people need to know about how things are built and deployed.
- Documentation. Maven incorporates a site feature which encourages documentation. Developers do not always like writing documentation, but the site can give us information on the codebase and bring in reports such as code coverage.
- Release management. It gives some structure to your releases. Artifacts are versioned, the snapshot system helps you manage work in progress, and you can define dependencies clearly.
- Code quality plugins. It is simple to integrate Maven with code quality tools such as coverage and static analysis tools, and possibly route these into your site.
Maven has its critics and I do not want to be a tool zealot, but I am frankly baffled that Maven cannot deliver value to the average Java team who are using Ant or working in an even more ad hoc style.
If this is a topic you enjoy, my CI tools note looks at the next piece of the automation puzzle, and my agile reading list includes the books that shaped how I think about build and release work.
Reference shelf
Sources and further reading for this note.
- Maven lifecycleThe phases that shape a Maven build.Opens in a new tab
- Apache MavenThe project home and documentation.Opens in a new tab
- Jenkins documentationThe build server that runs the lifecycle.Opens in a new tab
- JUnit 5The testing library for Java teams.Opens in a new tab
- Pro GitThe book on version control.Opens in a new tab
- Continuous integrationMartin Fowler on the habit behind the tool.Opens in a new tab