[Nexus]bom을 이용한 implementation 방법

WonDDak·2022년 12월 13일
0

Nexus

목록 보기
1/1

bom을 이용한 버전관리방법입니다
다수의 프로젝트에서 동일한 버전의 라이브러리를 사용할때 편리합니다.

pom과 bom

  • POM : 프로젝트의 정보를 가진 XML파일
  • BOM : Bill of Materials의 약자로 Dependency버전 정보들로만 구성된 특수한 pom 파일

implementation platform

기본적으로 안드로이드에서 라이브러리 사용시 dependencies에 implementation하여 사용할것입니다.
그런데 종종 다음과 같이 버전이 명시되어있지 않고 platform을 이용한 라이브러리를 볼 수 있습니다.

    dependencies {
       // define a BOM and its version
       implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))

       // define any required OkHttp artifacts without version
       implementation("com.squareup.okhttp3:okhttp")
       implementation("com.squareup.okhttp3:logging-interceptor")
    }
dependencies {
  // Import the BoM for the Firebase platform
  implementation platform('com.google.firebase:firebase-bom:31.1.1')

  // Declare the dependencies for the desired Firebase products without specifying versions
  // For example, declare the dependencies for Firebase Authentication and Cloud Firestore
  implementation 'com.google.firebase:firebase-auth'
  implementation 'com.google.firebase:firebase-firestore'
}

이는 gralde에서 지원하는 platform이라는 함수를 통해 Bom파일을 효과적으로 가져올수있습니다.
참고 URL

Bom 만들기

Bom은 pom파일만 존재하는 프로젝트입니다.
터미널을 실행하고 다음 과정을 따라 프로젝트를 생성합니다,

  1. gradle 프로젝트 init
    gradle init
  1. 프로젝트 타입 선택
  • Basic으로 생성한다.
Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4]
  1. 언어 선택
  • Groovy > build.gradle
  • Kotlin > build.gradle.kts
  • 1을 선택하여 진행
Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2]
  1. 이후 프로젝트 생성
  • 필요시 이름 설정을 한다
Generate build using new APIs and behavior (some features may change in the next

Project name (default: bomtest):
  1. 프로젝트를 안드로이드 스튜디오로 열어줍니다.
    build.gradle로 이동하여 다음과 같이 작성합니다.
//publish 플러그인 적용
plugins {
    id 'maven-publish'
}

publishing {
    repositories {
        //업로드할 nexus 위치 지정
        maven {
            url "nexus 주소"
            //nexus 접근 계정 설정
            credentials {
                username System.getenv("NEXUS_USR")
                password System.getenv("NEXUS_PSW")
            }
        }
    }
    def group = "그룹명"

    publications {
        release(MavenPublication) {
            groupId = group
            //bom파일 이름 (보통 XXX-bom)형태로 사용
            artifactId = "thirdparty-bom"
            //bom파일 버전
            version = "1.0.0"
            //pom 파일 override
            pom.withXml {
                asNode().children().last() + {
                    resolveStrategy = Closure.DELEGATE_FIRST
                    //pom파일에 적용시킬 라이브러리 정보들 입력
                    //nexus에 업로드한 두개의 라이브러리를 예시로함
                    dependencyManagement {
                        dependencies {
                            //그룹명:NAME1:X.Y.Z 의 정보를 입력함
                            dependency {
                                groupId group
                                artifactId "NAME1"
                                version "X.Y.Z"
                            }
                            //그룹명:NAME2:A.B.C 의 정보를 입력함
                            dependency {
                                groupId group
                                artifactId "NAME2"
                                version "A.B.C"
                            }
                        }
                    }
                }
            }
        }
    }
}

이후 완료되었다면

gradle publish

명령어를 통해 nexus로 업로드 함

사용하기

원하는 프로젝트로 이동하여 다음과 같이 사용한다.

//setting.gradle
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven {
            url System.getenv("NEXUS_PUBLIC") //public nexus 주소
            credentials {
                //접근계정
                username System.getenv("NEXUS_USR") 
                password System.getenv("NEXUS_PSW")
            }
        }
    }
}
//app/build.gradle
dependencies {
    implementation(platform("그룹명:thirdparty-bom:1.0.0"))
    implementation("그룹명:Name1")
    implementation("그룹명:Name2")
}

이후 sync 하면 정상적으로 라이브러리가 추가되는 것을 알수있다.

특정버전 사용

bom파일에 지정된 버전이 아닌 다른 버전을 사용하고싶다면 다음과 같이 버전을 명시해주면된다.
(하지만 권장하지 않는 방법이다.)

//app/build.gradle
dependencies {
    implementation(platform("그룹명:thirdparty-bom:1.0.0"))
    implementation("그룹명:Name1:X.Y.W")
    implementation("그룹명:Name2")
}

이상입니다.

profile
안녕하세요. 원딱입니다.

0개의 댓글

관련 채용 정보