buildscript {
ext {
springBootVersion = '2.4.5.RELEASE'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
apply false를 선언하고, 사용할 서브 프로젝트에서만 apply plugin 구문을 명시함으로써 해당 플러그인의 적용 범위를 제한할 수 있음 plugins {
id 'java'
id 'application'
id 'org.springframework.boot' version '2.4.5.RELEASE' apply false
}
subprojects {
if (name.startsWith('api')) {
apply plugin: 'org.springframework.boot'
}
}
buildscript는 빌드 시 가장 먼저 실행되는 블록
build.gradle 스크립트에서 필요한 의존성은 buildscript 블록에 작성해야
buildscript 블록은 스크립트 최상단에 정의해야 함
build.gradle이 들어있는 프로젝트에서 사용하는 라이브러리의 위치를 지정
jcenter(), mavenCentral(), google() 등 주로 인터넷에 있는 공개용 라이브러리 저장소를 사용함
repository에서 jcenter, mavenCentral, google등의 외부 라이브러리 레파지토리를 지정했다면 해당 레파지토리에서 찾을 라이브러리 명을 입력해야 함
repositories{}에서 Gradle이 의존성 모듈을 가져올 저장소를 선언함repositories{}에서 다운 받은 jar는 $USER_HOME/.gradle/caches/modules-2/files-2.1/ 경로에 캐시됨
// build.gradle
repositories {
mavenLocal() // Maven 로컬 저장소
mavenCentral() // Maven 중앙 저장소
maven{ url "http://ropo.company.com/maven" }
jcenter() // JCenter 저장소
}
💡 저장소 (repository)
💡 targetCompatibility
// build.gradle
// Include dependent libraries in archive.
mainClassName = "com.company.application.Main"
jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Gradle은 java의 의존성 관리를 위해 다양한 구성을 제공함
implementation : 프로젝트 컴파일 과정에서 필요한 라이브러리
providedComplie : 컴파일시에는 필요하지만 , 배포시에는 제외될 dependency를 설정함
providedRuntime : 런타임시에만 필요하고, 실행 환경에서 제공되는 dependency를 설정함
testImplementation : 테스트시에 필요한 dependency 관리
providedComplie와 providedRuntime은 war plugin이 설정된 경우에만 사용 가능함
💡 implementation vs api , complie
실제 프로젝트에서는..? 🧐
// all buildscript {} blocks must appear before any plugins {} blocks in the script
// 내용이 없을 경우 생략 가능
buildscript {
repositories {
jcenter() // 인터넷의 jcenter 레파지토리 사용 (https://bintray.com/bintray/jcenter)
}
}
plugins {
id 'java-library'
id 'com.github.johnrengelman.shadow' version '6.1.0'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter() // 기본 레파지토리
}
// JAVA 1.8 사용
sourceCompatibility = 1.8
dependencies {
// https://mvnrepository.com/artifact/com.google.cloud/google-cloud-speech
compileOnly('com.google.cloud:google-cloud-speech:1.29.2')
// https://mvnrepository.com/artifact/com.google.cloud/google-cloud-texttospeech
implementation 'com.google.cloud:google-cloud-texttospeech:1.1.0'
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0'
// https://mvnrepository.com/artifact/net.sourceforge.argparse4j/argparse4j
implementation group: 'net.sourceforge.argparse4j', name: 'argparse4j', version: '0.8.1'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
api는 라이브러리 소비자에게 전이적으로 노출되며, 이와 같이 소비자의 컴파일 클래스 경로에 나타남implementation은 소비자에게 노출되지 않으므로 소비자의 컴파일 클래스 경로로 누출되지 않는다출처
https://blog.naver.com/sqlpro/222665643369
https://willbesoon.tistory.com/93
https://docs.gradle.org/current/userguide/java_library_plugin.html
https://velog.io/@billion109/플링크-Gradle-Jar-빌드시-생기는-에러-Gradle-ShadowJar-만들기
https://kotlinworld.com/317