top of page

Jenkins-Day-3

### Installing Maven with YUM, Compatible Java Version, and Configuring JAVA_HOME on Amazon Linux 2 EC2


#### Scenario Overview

You are setting up a Java development environment on an Amazon Linux 2 EC2 instance. The setup involves installing a compatible version of Java and Apache Maven using YUM, followed by configuring the `JAVA_HOME` environment variable to ensure Maven can locate Java.


1. Launch and Access the EC2 Instance


Step 1.1: Launch an EC2 Instance

- In the AWS Management Console, launch an EC2 instance using the Amazon Linux 2 AMI.

- Ensure the security group is configured to allow SSH access (port 22).


Step 1.2: Connect to the EC2 Instance

- Use SSH to connect to your EC2 instance:

ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-dns

2. Install a Compatible Java Version


Step 2.1: Update the System Packages

- Start by updating the YUM package manager to ensure all packages are up to date:

sudo yum update -y

Step 2.2: Install OpenJDK

- Install OpenJDK 11, which is a widely used version compatible with most Maven projects:

sudo amazon-linux-extras install java-openjdk11 -y

Step 2.3: Verify Java Installation

- Verify the installation by checking the Java version:

java -version

You should see output similar to `openjdk version "11.x.x"`.


3. Install Apache Maven Using YUM


Step 3.1: Install Maven

- Install Apache Maven directly from the default repositories:

sudo yum install maven -y

Step 3.2: Verify Maven Installation

- Check if Maven is installed correctly:

mvn -version

The output should display the Maven version along with the Java version it's using.


4. Configure the JAVA_HOME Environment Variable


Step 4.1: Locate the Java Installation Path

- Find the path where Java is installed:

readlink -f $(which java) | sed "s:bin/java::"


Step 4.2: Set the JAVA_HOME Variable

- Open the `.bash_profile` or `.bashrc` file to add the `JAVA_HOME` environment variable:

sudo vim ~/.bash_profile

- Add the following line at the end of the file:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-<version>

export PATH=$JAVA_HOME/bin:$PATH

Replace `<version>` with the correct version directory name from Step 4.1.


Step 4.3: Apply the Changes

- Apply the changes to the current session:

source ~/.bash_profile


Step 4.4: Verify JAVA_HOME Configuration

- Confirm that `JAVA_HOME` is set correctly:

echo $JAVA_HOME

The output should show the path to your Java installation.


#### Conclusion

You have successfully set up a Java development environment on an Amazon Linux 2 EC2 instance by installing a compatible Java version, installing Maven using YUM, and configuring the `JAVA_HOME` environment variable. This setup ensures that Maven can compile and build Java applications on your EC2 instance.


 

### Simple Project: Cloning a GitHub Repository, Compiling, Unit Testing, and Packaging on Amazon Linux 2 EC2


#### Scenario

You are tasked with cloning a Java-based project from a GitHub repository to your local machine, compiling the code, running unit tests, and packaging the application. The project is hosted at `https://github.com/preethid/addressbook.git`.


1. Launch and Access the EC2 Instance


Step 1.1: Launch an EC2 Instance

- Use the AWS Management Console to launch an EC2 instance with the Amazon Linux 2 AMI.

- Ensure that your security group is configured to allow SSH access (port 22).


Step 1.2: Connect to the EC2 Instance

- Use SSH to connect to your EC2 instance:

ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-dns

2. Install Required Tools


Step 2.1: Update System Packages

- Start by updating the YUM package manager to ensure all packages are up to date:

sudo yum update -y

Step 2.2: Install Git

- Git is needed to clone the repository:

sudo yum install git -y

Step 2.3: Install Java

- Install OpenJDK 11, which is required for compiling and running Java applications:

sudo amazon-linux-extras install java-openjdk11 -y

Step 2.4: Install Maven

- Maven is used for compiling, running unit tests, and packaging the application:

sudo yum install maven -y

3. Clone the GitHub Repository


Step 3.1: Clone the Repository

- Use Git to clone the repository to your local directory:

git clone https://github.com/preethid/addressbook.git

Step 3.2: Navigate to the Project Directory

- Change the directory to the cloned repository:

cd addressbook

4. Compile the Project


Step 4.1: Compile the Code

- Use Maven to compile the Java code:

mvn compile

This command compiles the source code of the project.


5. Run Unit Tests


Step 5.1: Execute Unit Tests

- Run the unit tests included in the project:

mvn test

This command executes the test cases in the project, ensuring the code works as expected.


6. Package the Application


Step 6.1: Package the Code

- Package the compiled code into a JAR file:

mvn package

This command creates a JAR file in the `target` directory of the project, which can be deployed as a complete application.


7. Verify the Process


Step 7.1: List the Contents of the Target Directory

- Check that the JAR file has been successfully created:

ls target/

You should see a JAR file named something like `addressbook-1.0-SNAPSHOT.jar`.


8. Clean Up (Optional)


Step 8.1: Clean the Maven Build

- To clean up the project and remove the `target` directory:

mvn clean


#### Conclusion

You have successfully cloned a project from a GitHub repository to your local EC2 instance, compiled the code, ran unit tests, and packaged the application using Maven. This process is essential for ensuring that the project is correctly set up and that the code is working as intended before deployment.


 

### Simple Project: Configuring a Freestyle Job to Compile Java Code from GitHub with Automatic Maven Installer and Manually Set Java Path on Jenkins GUI


#### Scenario

You need to create a Jenkins Freestyle job that pulls a Java project from a GitHub repository (`https://github.com/preethid/addressbook.git`), compiles the code using Maven, and uses a manually set Java path for an already installed Java version. Maven will be configured as an automatic installer in Jenkins.


1. Prerequisites


Step 1.1: Install Jenkins (if not already installed)

- Ensure Jenkins is installed on your server and accessible via a web browser.

- Start Jenkins if it's not running:

sudo systemctl start jenkins

- Access Jenkins through your browser by navigating to `http://<your-server-ip>:8080`.


Step 1.2: Install Java on Your Server

- Ensure that Java is installed on the server where Jenkins is running. Verify the installation:

java -version

- If Java is not installed, install it according to your operating system's instructions.


2. Configure Jenkins for Java and Maven


Step 2.1: Log into Jenkins

- Open your web browser and log into Jenkins using your credentials.


Step 2.2: Navigate to Global Tool Configuration

- From the Jenkins dashboard, go to `Manage Jenkins` > ` Tools`.


Step 2.3: Configure JDK Installation

- Scroll down to the `JDK` section.

- Add JDK:

- Click `Add JDK`.

- Name: Name the JDK, e.g., `JDK 11`.

- Uncheck the `Install automatically` box (since Java is already installed on your server).

- JAVA_HOME: Enter the path to the Java installation on your server. You can find this path with the following command:

readlink -f $(which java) | sed "s:/bin/java::"

- Example path: `/usr/lib/jvm/java-11-openjdk-11.0.11.9-0.el7_9.x86_64`.


Step 2.4: Configure Maven Installation

- Scroll down to the `Maven` section.

- Add Maven:

- Click `Add Maven`.

- Name: Name the Maven installation, e.g., `Maven 3.8.7`.

- Install automatically: Leave this option checked.

- Version: Select the Maven version you want Jenkins to automatically install.


Step 2.5: Save the Configuration

- Click `Save` to apply the changes.


3. Create and Configure the Jenkins Freestyle Job


Step 3.1: Create a New Job

- From the Jenkins dashboard, click `New Item`.

- Enter a name for your project (e.g., `AddressBook-Build`) and select `Freestyle project`. Click `OK`.


Step 3.2: Configure the Source Code Management

- In the job configuration screen, under the `Source Code Management` section, select `Git`.

- Repository URL: Enter the GitHub repository URL: `https://github.com/preethid/addressbook.git`.


Step 3.3: Configure the Build Environment

- Scroll down to the `Build Environment` section.

- Ensure that `Provide Node & Maven tool installers` is checked.


Step 3.4: Configure the Build

- Scroll down to the `Build` section.

- Add Build Step: Click `Add build step` > `Invoke top-level Maven targets`.

- Maven Version: Select the Maven installation you configured earlier (`Maven 3.8.7`).

- Goals: Enter `clean compile` to clean the workspace and compile the code.


Step 3.5: Save the Job Configuration

- Click `Save` to create the job.


4. Build and Validate the Job


Step 4.1: Run the Job

- From the job’s dashboard, click `Build Now`.

- Monitor the build progress by clicking on the build number under the `Build History` section and then selecting `Console Output`.


Step 4.2: Validate the Output

- Ensure the build completes successfully, with the code compiled without errors.


Step 4.3: Check Maven and Java Versions

- In the console output, verify that Jenkins used the correct versions of Maven and Java as specified during the configuration.


#### Conclusion

You have successfully configured a Jenkins Freestyle job to clone a Java project from GitHub, compile the code using Maven, and utilize a manually set Java path for an already installed Java version. This setup allows you to automate the build process for Java projects, ensuring consistency across builds.

bottom of page