What is Maven and How Does it Work With Jenkins?
by Iris Chubb, on Oct 19, 2021 11:03:56 PM
Searching up “What does Maven do” will leave you with a feeling like you know less.
You've probably know that Maven has something to do with building Java applications Jenkins... or maybe that it acts as a public repository where artifacts are published…but past that it gets a bit fuzzy. And that’s a big ask, to adopt a technology hoping it’ll become clearer as you use it.
In this article, I’ll break down a little more clearly what Maven is, how it works with Jenkins, and how you can publish your Maven projects.
What is Maven and Why Do Teams Pair it With Jenkins?
A POM is the core of a project's configuration in Maven. It’s an XML file containing info about the project, configuration details, and default values for most projects.
Maven helps developers maintain Java-based applications through projects that organize codefiles and build scripts to run compiler tools, version numbers for compiled code, and dependency management that lets one project reference a version of another project.
Jenkins allows you to run Maven, and decide when to call a POM file, what condition to call, and what to do with the outcome. And since Jenkins can listen to different events, e.g svn commit, the current time is 12:00 AM, etc. Jenkins and Maven can become quite a powerful duo. For example, you can ask Jenkins to trigger a build or run through all JUnit tests whenever a new code is committed and then, if the unit tests are passed, deploy on a target machine.
Maven projects are then published to a Maven Repository, which are essentially like a web-based file share. There are index files in Maven repositories that list what projects and versions are stored in the repository, as well as metadata files that describe what each project is.
Adding Maven to your Jenkins is just as simple as adding any other plugin. Simply install the Maven plugin in Jenkins. This will add a "Build" section to your projects where you can specify exactly what to execute.
What exactly is a Maven Repository?
It really is just a file share with a bunch of files and folders. There are a lot of rules about how the files and folders need to be arranged, and what the contents of certain files (called index files) must look like.
The most well-known repository is called Maven Central, and it's a public - which means it's the entire world can freely download anything of the files in it.
There are a lot of public Maven repositories, hosted by a lot of different vendors and projects. The website mvnrepository.com is an index of these repositories, and allows you to search across 1,350 public repositories.
You most certainly don't want to publish your Maven projects to one of these public repositories. If you did, then it would mean not only a third-party would owns/curate your artifacts, but that your your proprietary software is out there for the world to see.
Instead, you need a private Maven repository.
Publish to a Private Maven Repository
Storing your Maven projects in a private repository like ProGet removes the access risks that standard Maven repositories have. It also means that you can create feeds separated by project, team, or even by who created it (first-party or third-party.) This enables your team to practice CI/CD best practices and develop faster and higher-quality code.
For this example, we’ll use ProGet. First, download and install ProGet on Windows or Linux. In ProGet click Feeds in the ribbon and set up a Maven Feed.
After selecting Maven Artifacts (found under the Developer Libraries Section), fill out the Feed Name, Description, and Feed Usage.
And that's it! Your private Maven Repository is now set up for your team to start adding artifacts.
The next steps in Jenkins are to install the ProGet plug-in and add the steps to your Jenkins project. This gives you a "ProGet Upload Package" for Freestyle Jobs and the `uploadProgetPackage` Pipeline step. For a more detailed step-by-step guide, check out the Jenkins ProGet plugin page.
Maven is a very powerful build tool that, when combined Jenkins, can simplify the Java building process. However, when it comes to operating in the enterprise, Maven's repositories aren't great.
Download ProGet for free and set up your private universal package server.