본문 바로가기

Project/Table_of_Organization_Management_System

NEXUS - 사용해보기

728x90

 

현업에서는 여러가지 서비스들이 어우러지는경우가 많다. 때문에 공통로직을 한곳에 모아놓고 쓰는경우가 많은데,

이때 Gradle을 사용해서 private한 nexus repository에 jar형태로 deploy하는 방법을 정리해볼까 한다.

Nexus 는 다양한 포맷(Maven, Docker, NPM 등)의 저장소를 지원하고, 현업에서는 Maven 에서 사용할 수 있는 가장 널리 사용되는 무료 저장소로 잘 알려져있다.

 

여기서는 두개의 프로젝트를 두고 한 프로젝트에서는 Nexus 에 배포하는 작업을 한 프로젝트에서는 Nexus 에 접근해 공통로직을 사용하는 실습을 진행해 보자.

 

1. Nexus 다운로드

 

- nexus-3.xx~ :
Nexus Repository Manager 어플리케이션 폴더
- sonatype-work
Data-store로 저장소, 설정, 캐시 등의 모든 데이터가 이 폴더 하위에 저장

--- 추가적으로 Sonatype 에서 제공하는 압축파일를  Docker 를 이용해 설치 할 수도 있다. https://github.com/sonatype/docker-nexus3 

 

 

1-1. Nexus 실행 포트 변경(필요시)

  • nexus-3.xx~/etc 하위에 nexus-default.properties 파일을 수정한다.
application-port=9999

 

 

2. Nexus 실행하기

---- 추가적으로 만약 MacOS에서 설치 후 ‘jre.bundle’은(는) 손상되었기 때문에 열 수 없습니다. 해당 항목을 휴지통으로 이동해야 합니다." 메시지가 나오는 경우 실행하면 정상적으로 실행은 됩니다만, 실행할 때 마다 메시지가 나타납니다. 그럴 경우 다음과 같은 $ xattr -cr "/{Full Path}/nexus/nexus-3.36.0-01/.install4j/jre.bundle" 명령어를 실행해 준다.

 

다음과 같은 로그가 뜨면 성공!

 

 

3. 로그인 후 Admin 패스워드 초기화

 

  • http://localhost:9999 로 접속 sign-in을 클릭
  • 최초 접속 시 admin 비밀번호가 있는 위치를 알려 준다.
  • 아이디 : admin, 비밀번호 : vi sonatype-work/nexus3/admin.password 파일에서 확인
  • 첫 로그인 시 admin 비밀번호를 변경하고 로그인 하면 접속 완료

 

4. Maven Repository 구축

 

 

  • Admin으로 로그인해서 환경설정 메뉴로 이동한다.
  • 그리고, Maven Repository로 Release, Snapshot, Group을 각각 생성한다.

- "Release" policy 생성

--> 'Create repository' 클릭

----> maven2(hosted) 선택

------> Release-Name 을 작성한뒤 'Release' version policy 를 선택후 생성

 

 

- "Snapshot" policy 생성

--> 'Create repository' 클릭

----> maven2(hosted) 선택

------> Snapshot-Name 을 작성한뒤 'Snapshot' version policy 를 선택후 생성

Relase policy 와 Snapshot policy 의 차이
- A "release" is the final build for a version which does not change.
- A "snapshot" is a build which can be replaced by another build which has the same name.

 

- Group 생성

--> Release와 Snapshot Repository URL을 하나로 묶어주는 public url 생성.

----> Create repository를 통해 **maven2(group)**을 선택한다.

 

 

5. Security Role과 User 생성

  • 위에서 생성한 Maven Repository에 대한 접근 권한과 역할을 생성한다.

- Role 생성

---> Roles> Create role> Nexus role을 클릭

------> Role ID, name을 입력하고, Privileges(권한)을 아래와 같이 선택한다.

---------> 권한은 view 권한으로 위에서 생성한 Repository에 대한 권한을 부여한다.

 

 

 

- User 생성

--> Users> Create local user를 클릭합니다.

----> ID, First name, Last name, Email, Password를 입력하고 status> Active를 선택하고 Roles는 1번에서 생성한 Role name을 선택하여 권한을 부여한다. (ID 와 Password 중요)

 

 

6. Gradle 설정 ( 배포 하는 곳 )

-> publishing 은 './gradlew clean build publish' 명령어로 가능하다.

 

- Root

build.gradle

...

ext.projectVersion = { // Semantic Versioning
    Properties versionProps = new Properties()
    versionProps.load(new FileInputStream(file('version.properties')))
    def (major, minor, patch) = [versionProps['VERSION_MAJOR'].toString(), versionProps['VERSION_MINOR'].toString(), versionProps['VERSION_PATCH'].toString()]
    return "${major}.${minor}.${patch}-SNAPSHOT"
}

subprojects {
    group = 'blog'
    project.version = projectVersion()

    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'java'
    apply plugin: 'maven-publish'

    def javaVer = '11'
    sourceCompatibility = javaVer
    targetCompatibility = javaVer
    compileJava.options.encoding = 'UTF-8'

    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }

    def nexusBaseUrl = "http://localhost:9999/repository"
    def NEXUS_ID = "test-blog"
    def NEXUS_PASSWORD = "test"
    repositories {
        mavenCentral()
        maven {
            credentials {
                username = NEXUS_ID         // System.getenv("NEXUS_ID") : 추후 하드코딩이 아닌 환경설정으로 처리
                password = NEXUS_PASSWORD   // System.getenv("NEXUS_PASSWORD")
            }
            url "${nexusBaseUrl}/blog-public/"
            allowInsecureProtocol = true    // http 허용, https로 변경하면 필요없음.
        }
    }
    publishing {
        repositories {
            maven {
                credentials {
                    username = NEXUS_ID
                    password = NEXUS_PASSWORD
                }
                def releasesRepoUrl = nexusBaseUrl + "/blog-releases"
                def snapshotsRepoUrl = nexusBaseUrl + "/blog-snapshots"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                allowInsecureProtocol = true    // http 허용, https로 변경하면 필요없음.
            }
        }
    }

...

}

 

 

- 배포할 모듈

build.gradle

...

bootJar.enabled = false // 실행가능한 아카이브로 main-class 가 있는 모듈인경우
jar.enabled = true // PLAIN 으로 생성 실행이 불가능한 일반 아카이브

jar { // 배포할 jar 이름
    archiveName("${project.name}-${version}.jar")
}

description = 'domain module for blog'

dependencies {
	...
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId group
            artifactId project.name
            version version

            artifact("build/libs/$project.name-$version"+".jar") {
                extension 'jar'
            }
        }
    }
}

 

 

6. Gradle 설정 ( 사용하는 곳)

- gradle 설정후 gradle reload 해줘야한다.

 

- Root

build.gradle

...

ext {
    nexusVersion = "0.1.1"
}

subprojects {
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'

    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'java'

    sourceCompatibility = '11'
    targetCompatibility = '11'
    compileJava.options.encoding = 'UTF-8'

    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }

    def nexusBaseUrl = "http://localhost:9999/repository"
    def NEXUS_ID = "test-blog"
    def NEXUS_PASSWORD = "test"
    repositories {
        mavenCentral()
        maven {
            credentials {
                username = NEXUS_ID         // System.getenv("NEXUS_ID") : 하드코딩이 아닌 환경설정으로 처리
                password = NEXUS_PASSWORD   // System.getenv("NEXUS_PASSWORD")
            }
            url "${nexusBaseUrl}/blog-public/"
            allowInsecureProtocol = true    // http 허용, https로 변경하면 필요없음.
        }
    }

	...

}

 

- Nexus 에 있는 모듈을 가지고와서 사용할 모듈

build.gradle

...

dependencies {
    implementation ("blog:domain:${nexusVersion}")
    
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

...

 

 

참고 블로그1 , 참고 블로그2 , 참고 블로그3  stackoverflow1 , stackoverflow2 , MavenPublication

728x90