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 Red Hat Enterprise Linux 8 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
Successful build with Maven.
Successful build with Maven.

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Red Hat Enterprise Linux 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 Red Hat Linux. We only need to enable the subsription management repositories. From there it's only a dnf command away.

  1. 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.
  2. 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
  3. 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>


  4. We add the path for the Java source code:
    mkdir -p $ mkdir -p src/main/java/org/linuxconfig
  5. We create a simple Java class that will print the time provided by the operating system. The file containg 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()));
    	}
    }
  6. We are ready to build the package:
    $ mvn package
  7. 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
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions