Hướng dẫn cài đặt và cấu hình Nexus làm kho lưu trữ Maven nội bộ trên Linux

Bài viết này trình bày chi tiết các bước để thiết lập một kho lưu trữ Maven nội bộ (Nexus) trên hệ thống Linux, giúp quản lý và phân phối các artifact của dự án hiệu quả hơn.

1. Chuẩn bị môi trường Java

Nexus yêu cầu môi trường Java để hoạt động. Bạn cần cài đặt một bộ JDK phù hợp.

Tải xuống và giải nén bộ JDK:


wget https://dragonwell.oss-cn-shanghai.aliyuncs.com/8/8.6.5-GA/Alibaba_Dragonwell_8.6.5_GA_Linux_x64.tar.gz
tar -zxvf Alibaba_Dragonwell_8.6.5_GA_Linux_x64.tar.gz
    

Cấu hình biến môi trường Java bằng cách chỉnh sửa file /etc/profile:


export JAVA_HOME=/root/jdk8u282-b1
export PATH=$PATH:$JAVA_HOME/bin
    

Áp dụng thay đổi và kiểm tra phiên bản Java:


source /etc/profile
java -version
    

Kết quả mong đợi:


openjdk version "1.8.0_282"
OpenJDK Runtime Environment (Alibaba Dragonwell 8.6.5) (build 1.8.0_282-b1)
OpenJDK 64-Bit Server VM (Alibaba Dragonwell 8.6.5) (build 25.282-b1, mixed mode)
    

2. Cài đặt và cấu hình Nexus

Tải xuống gói Nexus Repository Manager OSS:


# Sử dụng phiên bản cụ thể hoặc tải phiên bản mới nhất từ trang chủ Sonatype
wget https://www.sonatype.org/downloads/nexus-2.1.2-bundle.tar.gz
# Hoặc tải phiên bản mới hơn (ví dụ: 3.29.2-02)
# wget http://www.sonatype.org/downloads/nexus-3.29.2-02-unix.tar.gz
    

Giải nén Nexus:


# Thay thế bằng tên file đã tải về
tar -zxvf nexus-3.29.2-02-unix.tar.gz
    

Khởi chạy Nexus:


cd nexus-3.29.2-02/bin
./nexus start
    

Nexus không khuyến khích chạy với quyền root. Để khắc phục cảnh báo này, bạn có thể sửa đổi file cấu hình nexus.rc:


vim nexus.rc
# Thay đổi hoặc thêm dòng sau, ví dụ chạy với user 'nexususer' (cần tạo user này trước)
# run_as_user="nexususer"
# Nếu bạn vẫn muốn chạy với root (không khuyến khích), hãy bỏ comment dòng sau
# RUN_AS_USER="root"
    

Khởi động lại Nexus sau khi thay đổi cấu hình.

Kiểm tra trạng thái Nexus:


./nexus status
    

Kiểm tra port Nexus đang lắng nghe (mặc định là 8081):


ss -ntlp|grep java
    

Dừng Nexus:


./nexus stop
    

Nếu cần dừng khẩn cấp, bạn có thể dùng pkill:


pkill -9 java
    

Cài đặt lại Nexus (Ví dụ với phiên bản 2.11.2-03)

Tải xuống và giải nén:


wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar.gz
mkdir -p /usr/local/nexus
tar -zxvf nexus-2.11.2-03-bundle.tar.gz -C /usr/local/nexus/
    

Chỉnh sửa file cấu hình nexus.properties trong thư mục conf:


cd /usr/local/nexus/nexus-2.11.2-03/conf/
vim nexus.properties

# Cấu hình cổng và host
application-port=8081
application-host=0.0.0.0
nexus-webapp-context-path=/nexus

# Cấu hình thư mục làm việc của Nexus
nexus-work=${bundleBasedir}/../sonatype-work/nexus
    

Chỉnh sửa file ../bin/nexus để chỉ định thư mục cài đặt và user chạy:


cd ../bin/
vim nexus
# Đảm bảo NEXUS_HOME trỏ đúng đến thư mục cài đặt
NEXUS_HOME="/usr/local/nexus/nexus-2.11.2-03"
# Đặt RUN_AS_USER nếu cần
RUN_AS_USER=root # (Không khuyến khích)
    

Khởi động Nexus:


./nexus start
    

Sau khi Nexus khởi động, bạn có thể truy cập qua trình duyệt tại địa chỉ http://<your-server-ip>:8081/nexus.

Tài khoản đăng nhập mặc định:

  • Username: admin
  • Password: admin123

3. Cấu hình Maven để sử dụng Nexus

Để Maven sử dụng kho lưu trữ nội bộ của bạn, bạn cần cập nhật file settings.xml.

Cấu hình mirror trong settings.xml

Thêm các đoạn cấu hình mirror sau vào file settings.xml của bạn (thường nằm trong thư mục ~/.m2/ hoặc thư mục cài đặt Maven):


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <mirrors>
    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>*</mirrorOf>
      <name>阿里云公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

    <mirror>
      <id>nexus-snapshots</id>
      <mirrorOf>*</mirrorOf>
      <url>http://<your-nexus-ip>:8081/nexus/content/repositories/apache-snapshots/</url>
    </mirror>

    <mirror>
      <id>nexus-releases</id>
      <mirrorOf>*</mirrorOf>
      <url>http://<your-nexus-ip>:8081/nexus/content/groups/public</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://<your-nexus-ip>:8081/nexus/content/groups/public</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://<your-nexus-ip>:8081/nexus/content/groups/public</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

</settings>
    

Lưu ý: Thay thế <your-nexus-ip> bằng địa chỉ IP hoặc hostname của máy chủ Nexus.

4. Cấu hình Project Maven

Trong file pom.xml của dự án, bạn cần cấu hình các repository để trỏ về Nexus:


<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>com.lzj1234</groupId>
    <artifactId>microservice-provider-user</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.version>Hoxton.SR10</spring.cloud.version>
    </properties>

    <dependencies>
        <!-- Spring Boot Starters -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.2</version>
        </dependency>

        <!-- H2 Database -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring Boot Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.4.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>nexus-releases</id>
            <url>http://<your-nexus-ip>:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>nexus-releases</id>
            <url>http://<your-nexus-ip>:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>
    

Sau khi cấu hình, Maven sẽ ưu tiên tải các artifact từ Nexus.

5. Kiểm tra hoạt động

Chạy lệnh mvn spring-boot:run để khởi động ứng dụng Spring Boot của bạn. Maven sẽ tìm kiếm các dependency trên Nexus.

Nếu cấu hình đúng, ứng dụng sẽ khởi động thành công.

6. Thông tin về các kho lưu trữ Maven của Alibaba Cloud

Alibaba Cloud cung cấp các kho lưu trữ Maven trung gian, giúp tăng tốc độ tải và độ ổn định:

Tên Kho Lưu Trữ Địa Chỉ Alibaba Cloud Địa Chỉ Phiên Bản Cũ Địa Chỉ Nguồn
central https://maven.aliyun.com/repository/central https://maven.aliyun.com/nexus/content/repositories/central https://repo1.maven.org/maven2/
jcenter https://maven.aliyun.com/repository/public https://maven.aliyun.com/nexus/content/repositories/jcenter http://jcenter.bintray.com/
public https://maven.aliyun.com/repository/public https://maven.aliyun.com/nexus/content/groups/public Kho tổng hợp central và jcenter
google https://maven.aliyun.com/repository/google https://maven.aliyun.com/nexus/content/repositories/google https://maven.google.com/
gradle-plugin https://maven.aliyun.com/repository/gradle-plugin https://maven.aliyun.com/nexus/content/repositories/gradle-plugin https://plugins.gradle.org/m2/
spring https://maven.aliyun.com/repository/spring https://maven.aliyun.com/nexus/content/repositories/spring http://repo.spring.io/libs-milestone/
spring-plugin https://maven.aliyun.com/repository/spring-plugin https://maven.aliyun.com/nexus/content/repositories/spring-plugin http://repo.spring.io/plugins-release/
grails-core https://maven.aliyun.com/repository/grails-core https://maven.aliyun.com/nexus/content/repositories/grails-core https://repo.grails.org/grails/core
apache snapshots https://maven.aliyun.com/repository/apache-snapshots https://maven.aliyun.com/nexus/content/repositories/apache-snapshots https://repository.apache.org/snapshots/

Thẻ: Maven nexus Java linux Kho lưu trữ nội bộ

Đăng vào ngày 19 tháng 7 lúc 05:15