Tech/Gradle

SpirngBoot 기준 Gradle 멀티 모듈 세팅

소프 2021. 12. 17.

gradle init

 

 

settings.gradle 설정

/*
 * This file was generated by the Gradle 'init' task.
 *
 * The settings file is used to specify which projects to include in your build.
 *
 * Detailed information about configuring a multi-project build in Gradle can be found
 * in the user manual at https://docs.gradle.org/6.8.3/userguide/multi_project_builds.html
 */

rootProject.name = 'security'

["comp", "web", "server"].each {

    //폴더 생성
    def compDir = new File(rootDir, it)
    if(!compDir.exists()){
        compDir.mkdirs()
    }

    compDir.eachDir {subDir ->

        //build.gradle 파일 생성
        def gradleFile = new File(subDir.absolutePath, "build.gradle")
        if(!gradleFile.exists()){
            //build.gradle 파일 내용 저장
            gradleFile.text = 
                    """

                    dependencies {

                    }

                    """.stripIndent(20)
        }

        //배열에 선언한 폴더 생성
        [
                "src/main/java/com/sp/fc",
                "src/main/resources",
                "src/test/java/com/sp/fc",
                "src/test/resources"
        ].each {srcDir->
            def srcFolder = new File(subDir.absolutePath, srcDir)
            if(!srcFolder.exists()){
                srcFolder.mkdirs()
            }
        }

        //역할 : 만든 폴더를 gradel 모듈로 변경해주는 것같음
        //it : root 폴더, subDir.name = root 바로 밑의 폴더 이름
        //ex) server-kwc-test(gradle 모듈 이름)
        def projectName = ":${it}-${subDir.name}";
		//만들어진 서브 모듈들을 include 시킨다
        include projectName
        //subDir : root 폴더 밑의 gradle 모듈 절대 경로
        project(projectName).projectDir = subDir
    }
}

 

gradle reload

 

comp, web, server 폴더 생성

 

ex) comp 폴더 밑에 서브 폴더 생성 후 gradle reload

 

위와같은 폴더 및 build.gradle 자동 생성

 

 

root build.gradle 설정

 

buildscript {
    //변수같은 개념?
    //서브 build.gradle 에서 ${이름} 형식으로 사용 가능
    ext {
        spring = "2.4.1"
        boot = "org.springframework.boot"
        lombok = "org.projectlombok:lombok"
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("$boot:spring-boot-gradle-plugin:$spring")
    }
}

allprojects {
    group = "com.sp.fc"
    version = "1.0.0"
}

subprojects {

    apply plugin: "java"
    apply plugin: boot
    apply plugin: "io.spring.dependency-management"
    apply plugin: "idea"

    repositories {
        mavenCentral()
    }

    configurations {
        developmentOnly
        runtimeClasspath {
            extendsFrom developmentOnly
        }
    }

    dependencies {
       // developmentOnly("$boot:spring-boot-devtools")
        implementation "$boot:spring-boot-starter-security"
        implementation 'com.fasterxml.jackson.core:jackson-annotations'

        compileOnly lombok
        testCompileOnly lombok
        annotationProcessor lombok
        testAnnotationProcessor lombok

        testImplementation "$boot:spring-boot-starter-test"
    }

    test {
        useJUnitPlatform()
    }

}


["comp", "web"].each {
    def subProjectDir = new File(projectDir, it)
    subProjectDir.eachDir {dir->
        def projectName = ":${it}-${dir.name}"
        //@SpringBootApplication이 없는 프로젝트는 bootJar.enabled(false)
        //공통 모듈이 빌드된 배포본에 포함시키기 위해선 jar.enabled(true) 설정
        project(projectName){
            bootJar.enabled(false)
            jar.enabled(true)
        }
    }
}
["server"].each {
    def subProjectDir = new File(projectDir, it)
    subProjectDir.eachDir {dir->
        def projectName = ":${it}-${dir.name}"
        //@SpringBootApplication이 존재하기 때문에 bootJar.enabled(false) X
        project(projectName){

        }
    }
}

help.enabled(false)

 

 

root > server에 속한 모듈에서 다른 모듈의 코드를 사용하기 위해선?

ex) server > basic-test 에서 comp > common-utils의 코드를 사용하고 싶음

 

basic-test > build.gradle 설정

dependencies {

    implementation("$boot:spring-boot-starter-web")

	//타 모듈 포함시킴
    compile project(":comp-common-utils");
}

 

 

 

참고

https://gist.github.com/ihoneymon/a2ed116069af470fec0d08110604c5db

 

스프링 부트 그레이들 플러그인의 bootJar 비활성화 후 jar 파일이 생성안된다면...

스프링 부트 그레이들 플러그인의 bootJar 비활성화 후 jar 파일이 생성안된다면... - 20180702_springboot_gradle_plugin_jar_not_working.asc

gist.github.com

 

https://gitlab.com/jongwons.choi/spring-boot-security-lecture/-/blob/master/part0/1%20gradle%ED%94%84%EB%A1%9C%EC%A0%9D%ED%8A%B8%EA%B5%AC%EC%84%B1.md

 

part0/1 gradle프로젝트구성.md · master · 옥탑방개발자 / spring-boot-security-lecture

스프링 부트 시큐리티 강의 자료입니다.

gitlab.com

 

'Tech > Gradle' 카테고리의 다른 글

git submodule 추가후 빌드시 resources 폴더에 yml 추가  (0) 2021.12.18

댓글