Maven is a handy project management tool for Java projects. It helps handling multiple projects, can integrate with various IDE (Integrated Development Environment) software, and above all, simplifies build processes. In this tutorial we will install maven on a RHEL 8 / CentOS 8 Linux system, and to test the tool, we’ll build and run a simple example application.
In this tutorial you will learn:
- How to install Maven
- How to create a basic project
- How to build the project with Maven
- How to test the built application
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | RHEL 8 / CentOS 8 |
Software | Apache Maven 3.5 |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
How to install maven on Redhat 8 step by step instructions
Maven is so common it is available in the base repositories of RHEL 8 / CentOS 8. We only need to enable the subscription management repositories. From there it’s only a dnf
command away. If you don’t have Java installed on your system, the installation tool will pull it in as dependency.
- To install the required software, we use
dnf
:# dnf install maven -y
There will likely be many dependencies installed, as maven is well-equipped with tools. This is the only step that must be run with
root
privileges. - We create a directory that will hold our projects, and the test project’s directory within (testuser is a non-privileged operating system user on the lab machine):
$ mkdir -p /home/testuser/work/repository/what_time_is_it
And enter it:
$ cd /home/testuser/work/repository/what_time_is_it
- We create a basic
pom.xml
file, the heart of maven’s metadata with the following content:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.linuxconfig</groupId> <artifactId>WhatTimeIsIt</artifactId> <version>1.0.0</version> </project>
- We add the path for the Java source code:
mkdir -p $ mkdir -p src/main/java/org/linuxconfig
- We create a simple Java class that will print the time provided by the operating system. The file containing the source code will be
/home/testuser/work/repository/what_time_is_it/WhatTimeIsIt.java
with the following content:package org.linuxconfig; import java.util.Date; public class WhatTimeIsIt { public static void main(String args[]) { System.out.println("Detected system time is: " + new Date(System.currentTimeMillis())); } }
- We are ready to build the package:
$ mvn package
- On successful build we can run our example application built in the working maven installation. Notice that the main class is provided on the command line:
$ java -cp target/WhatTimeIsIt-1.0.0.jar org.linuxconfig.WhatTimeIsIt Detected system time is: Thu Jan 03 21:25:41 CET 2019