ETC

Gradle + Nexus Repository library 사용 / Nexus 개인정보 암호화

keepbang 2024. 3. 14. 13:58

Spring으로 개발을 하다보면 공통된 기능들을 따로 모아서 여러 프로젝트에 사용한다. 그럴때 많이 사용하는게 Nexus이다.

 

이번 글에서는 아래 두가지에 대해 알아볼려고 한다.

  1. Nexus 개인정보 암호화 하는 방법
  2. Nexus에 올린 library를 가져와서 사용하는 방법

개발환경

macOS(M2 Pro)
IntelliJ IDEA
gradle 8.1.1

 

 

이 방법을 적용하면서 번거로운 부분도 있었다.

  1. 전체 프로젝트에 적용이 된다.
  2. 패스워드나 아이디, url이 변경될때마다 값을 바꿔줘야한다.
  3. 프로젝트를 처음 받는 사람은 설정하기 까다롭다.

그냥 이런 방법도 있구나~~하는 마음으로 봐주시길 바랍니다.


1.  Nexus 개인정보 암호화 하는 방법

 

Nexus library를 사용하기 위해서 build.gradle에 nexus정보를 입력해서 사용게 되는데 private한 정보가 누출될 가능성이 있으므로 암호화하여 관리하는 방안이 필요하다.

 

gradle에서 nexus library를 가져오기 위해서 아래 코드블럭처럼 build.gradle을 작성해주면 된다.

buildscript {

	...

    repositories {
        gradlePluginPortal()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        maven {
            url "$nexusBaseUrl/maven-releases/"
            allowInsecureProtocol = true // nexus url이 http일 경우 추가
            credentials {
                username = "$nexusId"
                password = "$nexusPassword"
            }
        }
    }
    
    dependencies {
    	classpath 'com.keepbang:common-library:1.0.0'
    }
}

 

nexusBaseUrl, nexusId, nexusPassword global로 관리하여 코드상에 노출되는 개인정보는 없다.

 

global로 관리되는 정보의 위치는 macOS 기준 ~/.gradleinit.gradle 파일을 생성해서 값을 입력해두면 된다.

 

init.gradle 파일

allprojects {
	project.ext.set('nexusBaseUrl', 'https://keepbang.nexus.co.kr/repository')
	project.ext.set('nexusId', 'nexusId')
	project.ext.set('nexusPassword', 'nexusPassword')
}

 

이렇게 하면 모든 gradle file에서 위에 입력한 nexus 정보들을 가져가 쓸 수 있다.

 

하지만 위와 같이 사용하면 프로젝트 코드에 직접적인 노출은 피할 수 있지만 저기에 적혀있는 개인 정보도 가리고 싶어졌다...

 


gradle 자격증명 플러그인으로 Nexus 정보 암호화하기

명령어를 사용하여 gradle 프로젝트를 생성한다.

% mkdir gradle-credentials-plugin-tutorial
% cd gradle-credentials-plugin-tutorial
% gradle init

 

build.gradle, setting.gradle에 자격증명 플러그인을 추가해야 addCredentials명령어를 사용 할 수 있다.

plugins {
    id 'nu.studer.credentials' version '3.0'
}

 

gradle addCredentials 명령어를 사용해서 ~/.gradle 디렉토리에 gradle.encrypted.properties 파일로 입력한 내용을 암호화해서 만들어진다.

 

암호화된 파일의 값을 사용하기 위해서 일단 init.gradle 파일도 아래처럼 변경해 둔다.

gradle addCredentials --key nexusUrl --value 'https://keepbang.nexus.co.kr/repository'
gradle addCredentials --key nexusId --value 'nexusId'
gradle addCredentials --key nexusPassword --value 'nexusPassword'

[gradle.encrypted.properties]
nexusUrl=ZlSkq1CQaJUSUofILEU/dr0l0qL2u2qWB3QVno9dHgulM3v3rVBxG5cWtU7fnAVW
nexusId=MBz7bWcyIuz29IxmqXmfnA\=\=
nexusPassword=8IYzQf9R016gbkzuzVCcFg\=\=

[init.gradle]
allprojects {
	project.ext.set('nexusBaseUrl', gradle.ext.nexusUrl)
	project.ext.set('nexusId', gradle.ext.nexusId)
	project.ext.set('nexusPassword', gradle.ext.nexusPassword)
}

 


2. Nexus에 올린 library를 가져와서 사용하기

위에서 이미 library를 가져와서 사용하는 방법을 적었지만 암호화된 정보를 사용하기 위해선 따로 설정을 추가해줘야한다.

 

settings.gradle 파일에 암호화된 키값을 가져와서 gradle 변수로 등록만 해주면된다.

[setting.gradle]

...

plugins {
    id 'nu.studer.credentials' version '3.0'
}
ext {
    def nexusUrl = credentials.forKey('nexusUrl')
    def nexusId = credentials.forKey('nexusId')
    def nexusPassword = credentials.forKey('nexusPassword')
    gradle.ext.nexusUrl = nexusUrl
    gradle.ext.nexusId = nexusId
    gradle.ext.nexusPassword = nexusPassword
}

...

 

 


 

참고

자격증명 플러그인

 

GitHub - etiennestuder/gradle-credentials-plugin: Gradle plugin to store and access encrypted credentials for use in Gradle buil

Gradle plugin to store and access encrypted credentials for use in Gradle builds. - etiennestuder/gradle-credentials-plugin

github.com

 

gradle install

 

Gradle | Installation

Install the Gradle build tool on Linux, macOS or Windows, either manually or using a package manager like SDKMAN! or Homebrew.

gradle.org