관리 메뉴

IT.FARMER

gradle nexus repository upload, maven-publish 연동방법 본문

Auto Build(CI SCM GIT)

gradle nexus repository upload, maven-publish 연동방법

아이티.파머 2023. 5. 25. 20:57
반응형

gradle nexus repository upload, maven-publish 연동방법

gradle 에서 6.0까지만 plug-in 으로 maven (apply maven)을 지원하고 v7 부터는 지원하지 않아 maven-publish 를 사용하게 되었다. 사실 기존 방식이 조금더 직관적이었다고 생각이 든다. 셋팅도 좀더 편하고 말이다. 하지만 maven-publish 를 사용하면 사용할수있는 기능이 좀더 많다고 생각이 든다.

Maven 게시 플러그인 (gradle.org) << 해당 페이지에서 좀더 자세한 내용을 확인 할 수있다.

  • gradle v7
  • nexus v3.17
  • java 17

사설 (Nexus) Repository 설정

  • 스크립트 선언 후 다운로드 받을때 사설 레파지토리 이용하기.
apply plugin: 'java'
apply plugin: 'maven-publish'

repositories {
    def nexusUrl = 'http://<<domain>>:8081'
    def nexusId = 'id'
    def nexusPassword = 'password'

    maven {
        url = uri("http://<<domain>>:8081/repository/mezzo-group/")
        allowInsecureProtocol = true
        credentials {
            username = nexusId
            password = nexusPassword
        }
    }
}
  • 플러그인으로 apply plugin: maven-publish 을 선언해준다.
  • 설치된 레파지토리의 도메인정보와 아이디 비밀번호를 입력한다.
  • 기본 https를 이용하나 http 도 이용할수 있도록 allowInsecureProtocol = true
    내용을 추가한다.

사설 Repository 로 Library upload 하기

앞서 설정한 내용에 이어 하위에 작성하면 된다. 마찬가지로 ‘maven-publish’ plug-in 을 사용한다.


publishing {

    def nexusId = 'media_report'
    def nexusPassword = 'media_report'

    publications {

        // nexus 3.6 사용으로 put method를 사용하는 maven-publish 에서는 에러가남
        //  - 대안1) nexus 를 업데이트 한다.
        //  - 대안2) post 로 날리는 task 를 별도로 만든다.
        mavenPublication(MavenPublication) {
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
        }

    }
    repositories {
        def nexusUrl = 'http://<<domain>>:8081'
        maven {

            def releasesRepoUrl = layout.buildDirectory.dir('repos/releases')
            def snapshotsRepoUrl = layout.buildDirectory.dir('repos/snapshots')

            def nexusReleaseUrl = uri("${nexusUrl}/repository/mediareport-release/")
            def nexusSnapshotsUrl = uri("${nexusUrl}/repository/mediareport-snapshots/")

            println "version :  ${version}"
            url = version.endsWith('SNAPSHOT') ? nexusSnapshotsUrl : nexusReleaseUrl
            println "url :  ${url}"

            credentials {
                username = nexusId
                password = nexusPassword

            }
            allowInsecureProtocol = true
            authentication {
                basic(BasicAuthentication)
            }
        }
    }
}
  • publications
    • 빌드되기 위한 정보들을 담는다. 예제에서는 선언하지 않았으나, 아카이브 파일에 대한 여러가지 정보를 담을수있다. 예를들어 아카이브를 만든 사용자 , 혹은 라이선스정보 등을 선언해줄수있다.
  • repositories
    • gradlew publish 명령어를 사용하여 업로드할 private repository 정보를 기입한다.
  • nexusReleaseUrl
    • RELEASE 아카이브가 업로드될 url 정보
  • nexusSnapshotsUrl
    • SNAPSHOT 아카이브가 업로드될 넥서스 URL 정보
  • credentials
    • 넥서스 레파지토리에 접근하기 위한 인증정보
  • allowInsecureProtocol
    • http 프로토콜을 사용할수있도록 true 로 설정한다. (기본 https 로 설정하지 않으면, 보안오류발생)

SpringBoot 스크립트 예제

아래는 Springboot를 이용하여 Parent-Chield 형태의 Module을 구성하고 만든 최종 스크립트 정보이다.

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin'
    }
}

plugins {
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
    id 'maven-publish'
}

apply from: './version.gradle'

allprojects {

    group = group_id
    version = storage_version
    sourceCompatibility = '17'

    //this.group = ext.grg
    //this.version = version

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

    repositories {
        def nexusUrl = 'http://<<domain>>8081'
        def nexusId = 'id'
        def nexusPassword = 'password'

        maven {
            url = uri("http://<<domain>>:8081/repository/mezzo-group/")
            allowInsecureProtocol = true
            credentials {
                username = nexusId
                password = nexusPassword
            }
        }
    }

    subprojects {
        apply plugin: 'java'
        apply plugin: 'maven-publish'

        group = group_id
        version = storage_version

        publishing {

            def nexusId = 'media_report'
            def nexusPassword = 'media_report'

            publications {

                // nexus 3.6 사용으로 put method를 사용하는 maven-publish 에서는 에러가남
                //  - 대안1) nexus 를 업데이트 한다.
                //  - 대안2) post 로 날리는 task 를 별도로 만든다.
                mavenPublication(MavenPublication) {
                    from components.java
                    versionMapping {
                        usage('java-api') {
                            fromResolutionOf('runtimeClasspath')
                        }
                        usage('java-runtime') {
                            fromResolutionResult()
                        }
                    }
                }

            }
            repositories {
                def nexusUrl = 'http://<<domain>>:8081'
                maven {

                    def releasesRepoUrl = layout.buildDirectory.dir('repos/releases')
                    def snapshotsRepoUrl = layout.buildDirectory.dir('repos/snapshots')

                    def nexusReleaseUrl = uri("${nexusUrl}/repository/mediareport-release/")
                    def nexusSnapshotsUrl = uri("${nexusUrl}/repository/mediareport-snapshots/")

                    println "version :  ${version}"
                    url = version.endsWith('SNAPSHOT') ? nexusSnapshotsUrl : nexusReleaseUrl
                    println "url :  ${url}"

                    credentials {
                        username = nexusId
                        password = nexusPassword

                    }
                    allowInsecureProtocol = true
                    authentication {
                        basic(BasicAuthentication)
                    }
                }
            }
        }
    }

        // 하위 프로젝트에서 사용할 라이브러리 정의 (버전관리를 위함)
    dependencyManagement {
        dependencies {
            dependency("media.report.library:media-report-library-model:latest.integration")
        }
    }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

subprojects {
    dependencyManagement {
        dependencies {
            dependency("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3")
        }
    }
}

dependencies {
    // 버전관리
    def model_version = '1.0-SNAPSHOT'
    def repository_version = '1.0-SNAPSHOT'

    // 내부 라이브러리 설정
    implementation ("media.report.library:media-report-library-model") {changing(true)}

    // 내부 라이브러리 설정
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-cassandra-reactive'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    implementation 'org.springframework.kafka:spring-kafka'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.kafka:spring-kafka-test'
    testImplementation 'io.projectreactor:reactor-test'

}

tasks.named('test') {
    useJUnitPlatform()
}

// 최상위 프로젝트는 빌드 하지 않음
bootJar.enabled=false
jar {
    enabled = false
}
반응형

'Auto Build(CI SCM GIT)' 카테고리의 다른 글

gradle nexus 연동  (0) 2023.05.25
linux (centos) gradle 설치  (0) 2020.07.28