DEV Community

Cover image for Getting Started with Maven: A Beginner's Guide to Java Build Automation
Nilanchal
Nilanchal

Posted on • Edited on • Originally published at stacktips.com

Getting Started with Maven: A Beginner's Guide to Java Build Automation

Maven is a popular build automation tool used primarily for Java projects. It is designed to manage a project's build process, including the managing dependencies, packaging, deployment and generating reports.

Maven uses an XML file called a Project Object Model pom.xml to define the project's configuration, including its dependencies, plugins, and other settings.

The POM file is typically stored in the project's root directory.

Why Use Maven?

Maven is a popular build tool for Java projects that provides a number of benefits, including:

  1. Dependency management: Maven makes it easy to manage dependencies for your project, including downloading and including libraries from remote repositories.

  2. Build automation: Maven automates the build process, including compiling the code, running tests, and packaging the application into a distributable format.

  3. Consistent project structure: Maven provides a standard project structure that makes it easy to organize your code and resources.

  4. Plugin system: Maven has a powerful plugin system that can be used to extend the functionality of the build process, such as generating documentation or deploying the application to a server.

  5. Easy project setup: Maven provides archetypes that can be used to quickly set up a new project with a predefined structure and dependencies.

  6. Reproducible builds: Maven provides a way to create reproducible builds, which means that the build process is consistent across different machines and environments.

  7. Integration with IDEs: Maven integrates with popular Java IDEs, such as Eclipse and IntelliJ, making it easy to import and work with Maven projects.

Overall, Maven can help improve the productivity of developers and make it easier to manage complex Java projects by automating common tasks and providing a standardized way to structure and build projects.

Project Identifiers in Maven?

Maven projects are identified by 3 main identifiers/coordinates:

  1. Group Id: Unique identifier for the organization. Typically, this is a reversed domain name.

  2. Artifact Id: Used to generate the build (JAR /WAR) files.

  3. Version: A version number e.g 1.0.0

Maven Repositories

Maven uses a centralised repository to manage project dependencies. The Maven Repository is divided into three types: Local, Central and Remote.

1. Maven Local Repository
The local repository is a directory on the developer's machine where all the dependencies for a specific project are stored.

The local repository in Maven is usually located in the user's home directory under the .m2 folder.

2. Central Maven Repository
The Central Repository is the default remote repository used by Maven.

It contains a vast collection of open-source libraries and dependencies, making it a one-stop shop for most projects. When Maven needs to download a dependency, it first looks in the local repository and then in the Central Repository.

The central maven repository is managed by Sonatype. There are almost all java libraries that exist in it.

When building a project, if it can not find any dependent jars in the local maven repository it will search in the central maven repository and download those JARs if found. You can also search for an artifact in central.sonatype.com if you want to download the artifact’s pom or jars.

To search/browse in the central repository you can use: central.sonatype.com

3. Remote Repository is a repository of dependencies located on a remote server, typically managed by third party organizations.

Here's an example of how to use a remote Maven repository in a Java project:

<repositories>
    <repository>
        <id>reporitory-id</id>
        <name>Repository Name</name>
        <url>your remote repository url</url>
    </repository>
</repositories>
Enter fullscreen mode Exit fullscreen mode

For every dependency defined in pom.xml file, it first search dependency in the local repository, followed by the remote repositories in the same order as defined in the pom.xml file.

If the dependency is not found in any of the remote repositories, Maven reports a build error.

Creating Maven Java Project using CommandLine

  1. Create a new project directory: Create a new directory for your project. For example, you can create a directory called "my-maven-project".
mkdir my-maven-project
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the project directory: Navigate to the newly created directory.
cd my-maven-project
Enter fullscreen mode Exit fullscreen mode
  1. Create a new Maven project: Create a new Maven project using the mvn archetype:generate command. This command generates a new Maven project based on a template or archetype.
mvn archetype:generate 
    -DgroupId=com.example.app 
    -DartifactId=my-app 
    -DarchetypeArtifactId=maven-archetype-quickstart 
    -DinteractiveMode=false
Enter fullscreen mode Exit fullscreen mode

By the way, you don’t have to remember each of these parameters provided above. Just use the interactiveMode=true, so that Maven asks for all the required parameters.

In this command, the groupId and artifactId are the unique identifiers for your project. The archetypeArtifactId specifies the archetype to use, which in this case is the maven-archetype-quickstart archetype, which creates a simple Java project with a main class.

Learn more about Maven build process, repository and build profiling here.

c01-Introduction to Maven

c02-Install and configure Maven on Windows

c03-Install and configure Maven on macOS?

c04-Maven Repository Introduction

c05-Creating Maven Java Project using CommandLine

c06-Understanding Maven Project and Dependencies

c07-Maven Build Lifecycles

c08-Popular Maven Commands

c09-Working with Profiles in Maven

Conclusion and Wrap Up

Whether you're a beginner or an experienced developer, this articles will help you to up to speed with Maven.

Top comments (2)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise

This is the default remote repository used by Maven maintained by Apache Community.

This is not correct. The central repository is maintained by SonaType which maintains the infrastructure as well as people who are handling the issues coming in etc.

Very important jcenter.bintray.com does not exist anymore and the configuration you have given for repo.maven.apache.org/maven2 is configured in Maven itself by default. That means there is no need to configure that manually ...

To search/browse in the central repository you can use: central.sonatype.com/

Collapse
 
nilan profile image
Nilanchal

Thanks @khmarbaise for your feedback. Will update my article soon.