Download and Configure Maven on Ubuntu 22.04

Download and Configure Maven on Ubuntu 22.04

Steps to Download and Configure Maven on Ubuntu Server Running Jenkins

Pre-requisites:

Before installing Maven on your Ubuntu server where Jenkins is running, ensure that Java is already installed. If not, you need to install Java first. Follow the steps below:

Installing Java on Ubuntu

  1. Update package lists:

     sudo apt-get update
    
  2. Install OpenJDK 17:

     sudo apt install -y --no-install-recommends openjdk-17-jdk-headless
    
  3. Verify Java installation:

     java -version
    

Download and Install Maven

  1. Download Maven: Visit the official Maven site here to obtain the latest Maven release.

  2. Copy Binary Tarball Link: Copy the link for the binary tar.gz archive from the Maven website.

  3. Navigate to Opt Directory: Switch to the /opt directory using the terminal:

     sudo su -
     cd /opt
    
  4. Download Maven Archive: Use wget to download the Maven binary tar.gz archive:

     wget <paste_binary_tarball_link_here>
     wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
    
  5. Extract Archive: Extract the downloaded tarball:

     tar -xvzf apache-maven-<version_number>-bin.tar.gz
     tar -xvzf apache-maven-3.9.6-bin.tar.gz
    
  6. Rename Directory: Rename the extracted directory for convenience:

     mv apache-maven-<version_number> maven
     mv apache-maven-3.9.6 maven
    
  7. Set Environment Variables: Open .bash_profile in a text editor:

     sudo vi ~/.bash_profile
    

    Add the following lines to set Java and Maven environment variables:

     export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
     export M2_HOME=/opt/maven
     export M2=/opt/maven/bin
     export PATH=$PATH:$M2_HOME:$M2:$JAVA_HOME
    

    Save and exit the editor.

  8. Reload Profile: Apply the changes to the current session:

     source ~/.bash_profile
    

Configure Jenkins

  1. Install Maven Integration Plugin: Navigate to Jenkins dashboard -> Manage Jenkins -> Manage Plugins -> Available. Search for "Maven Integration" plugin, install, and restart Jenkins.

  2. Configure Maven and Java Paths: Navigate to Jenkins dashboard -> Manage Jenkins -> Global Tool Configuration. Add JDK and Maven installations, specifying the paths set earlier.

Build Maven Project in Jenkins

  1. Create Maven Project: In Jenkins, create a new item and select "Maven Project".

  2. Project Configuration:

    • Provide a name and description for the project.

    • Choose the Git repository for your project.

    • Select "Build Root POM" and specify the path to pom.xml in your repository.

  3. Build Configuration:

    • In the build configuration, define the Maven goals and options such as clean package or clean install.

  4. Save and Build: Save the configuration and trigger a build to test the setup.

Additional Information

  • For more details on Maven lifecycle, refer to the Maven documentation.

By following these optimized steps, you can efficiently download, install, and configure Maven on your Ubuntu server running Jenkins, ensuring smooth project builds and deployments.