Setting Up Nexus Repository on AWS EC2 with Terraform and Publishing a Custom Gradle Plugin

1. Introduction to Sonatype Nexus What is Sonatype Nexus? Sonatype Nexus is a powerful, open-source repository manager that allows organizations to store and manage software artifacts. It supports both internal and external repositories for various package formats such as Maven, npm, NuGet, Docker, and more. Nexus helps in managing and distributing binary artifacts, ensuring efficient version control and secure access to packages within an organization. Why Use Nexus? Nexus Repository provides several benefits: Centralized Artifact Management: Nexus stores artifacts like libraries, dependencies, and Docker images, making them easy to share and reuse across different projects. Secure Artifact Distribution: It allows for secure access to artifacts through permissions and policies, preventing unauthorized access and ensuring the integrity of software artifacts. Supports Multiple Formats: Nexus supports different repository formats such as Maven, npm, Docker, etc., making it flexible and adaptable for various development environments. Integration with Build Systems: Nexus can be integrated into CI/CD pipelines for automated artifact management and deployment. 2. Project Overview In this project, we automated the installation of Sonatype Nexus on an AWS EC2 instance using Terraform and demonstrated the publishing and usage of a custom Gradle plugin through Nexus. The project is divided into three major parts: Deploy Nexus via Terraform Create and Publish a Custom Gradle Plugin to Nexus Use the Published Plugin in a Spring Boot Backend Project 3. Deploying Nexus Using Terraform We created a Terraform configuration to: Launch an EC2 instance Install Java and Nexus via a shell script Set up Nexus as a system service Open port 8081 to access Nexus UI Key Terraform Files: main.tf provider "aws" { region = "us-east-1" # Adjust region if necessary } resource "aws_instance" "nexus" { ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI (adjust for your region) instance_type = "t3.medium" # Adjust instance type as per your requirements key_name = "your-ssh-key" # Replace with your SSH key name # Security Group to allow access to port 8081 (Nexus) security_group = aws_security_group.nexus_sg.name # Instance metadata and user data to run the Nexus installation script user_data =

Apr 13, 2025 - 18:16
 0
Setting Up Nexus Repository on AWS EC2 with Terraform and Publishing a Custom Gradle Plugin

1. Introduction to Sonatype Nexus

What is Sonatype Nexus?

Sonatype Nexus is a powerful, open-source repository manager that allows organizations to store and manage software artifacts. It supports both internal and external repositories for various package formats such as Maven, npm, NuGet, Docker, and more. Nexus helps in managing and distributing binary artifacts, ensuring efficient version control and secure access to packages within an organization.

Image nexus-repo

Why Use Nexus?

Nexus Repository provides several benefits:

  • Centralized Artifact Management: Nexus stores artifacts like libraries, dependencies, and Docker images, making them easy to share and reuse across different projects.
  • Secure Artifact Distribution: It allows for secure access to artifacts through permissions and policies, preventing unauthorized access and ensuring the integrity of software artifacts.
  • Supports Multiple Formats: Nexus supports different repository formats such as Maven, npm, Docker, etc., making it flexible and adaptable for various development environments.
  • Integration with Build Systems: Nexus can be integrated into CI/CD pipelines for automated artifact management and deployment.

2. Project Overview

In this project, we automated the installation of Sonatype Nexus on an AWS EC2 instance using Terraform and demonstrated the publishing and usage of a custom Gradle plugin through Nexus.

The project is divided into three major parts:

  1. Deploy Nexus via Terraform
  2. Create and Publish a Custom Gradle Plugin to Nexus
  3. Use the Published Plugin in a Spring Boot Backend Project

3. Deploying Nexus Using Terraform

We created a Terraform configuration to:

Key Terraform Files:

main.tf

provider "aws" {
  region = "us-east-1"  # Adjust region if necessary
}

resource "aws_instance" "nexus" {
  ami           = "ami-0c55b159cbfafe1f0"  # Amazon Linux 2 AMI (adjust for your region)
  instance_type = "t3.medium"  # Adjust instance type as per your requirements
  key_name      = "your-ssh-key"  # Replace with your SSH key name

  # Security Group to allow access to port 8081 (Nexus)
  security_group = aws_security_group.nexus_sg.name

  # Instance metadata and user data to run the Nexus installation script
  user_data = <<-EOF
              #!/bin/bash
              set -e
              # Update system and install Java
              sudo apt update && sudo apt upgrade -y
              sudo apt install openjdk-8-jdk -y

              # Create nexus user
              sudo adduser --disabled-password --gecos "" nexus
              sudo usermod -aG sudo nexus

              # Download and install Nexus
              cd /opt
              sudo wget https://download.sonatype.com/nexus/3/nexus-3.70.4-02-java8-unix.tar.gz
              sudo tar -xvzf nexus-3.70.4-02-java8-unix.tar.gz
              sudo mv nexus-3.70.4-02 nexus
              sudo chown -R nexus:nexus /opt/nexus

              # Configure Nexus to run as nexus user
              echo 'run_as_user="nexus"' | sudo tee /opt/nexus/bin/nexus.rc

              # Create systemd service for Nexus
              cat <