it-source

Gradle과 동등한 Maven 프로파일

criticalcode 2023. 2. 25. 21:21
반응형

Gradle과 동등한 Maven 프로파일

봄 부트 프로젝트 구축에서는 의존관계를 포함/제외하고 환경에 따라 전쟁 또는 항아리를 패키징하는 간단한 시나리오를 실현하려고 합니다.

예를 들어, 환경을 위해devdevtools 및 패키지 항아리 포함, 대상prod패키지 전쟁 등

XML 기반 구성이 아닌 것을 알고 있습니다.기본적으로 build.grading 스테이트먼트에 쓸 수 있는 방법이 있습니까?

여러 빌드 파일을 생성하는 대신 몇 가지 공통 종속성을 선언하고 단일 파일로 참조할 수 있습니까?

빌드 타깃 환경에 따라 빌드 구성을 변경하는 베스트 프랙티스가 있습니까?

ext {
    devDependencies = ['org.foo:dep1:1.0', 'org.foo:dep2:1.0']
    prodDependencies = ['org.foo:dep3:1.0', 'org.foo:dep4:1.0']
    isProd = System.properties['env'] == 'prod'
    isDev = System.properties['env'] == 'dev'
}

apply plugin: 'java'

dependencies {
    compile 'org.foo:common:1.0'
    if (isProd) {
       compile prodDependencies
    }
    if (isDev) {
       compile devDependencies
    }
}

if (isDev) tasks.withType(War).all { it.enabled = false }

내 버전(Lance Java의 답변에서 영감을 얻음):

apply plugin: 'war'

ext {
  devDependencies = {
    compile 'org.foo:dep1:1.0', {
      exclude module: 'submodule'
    }
    runtime 'org.foo:dep2:1.0'
  }

  prodDependencies = {
    compile 'org.foo:dep1:1.1'
  }

  commonDependencies = {
    compileOnly 'javax.servlet:javax.servlet-api:3.0.1'
  }

  env = findProperty('env') ?: 'dev'
}

dependencies project."${env}Dependencies"
dependencies project.commonDependencies

if (env == 'dev') {
  war.enabled = false
}

파일에 코드 행을 추가하여 다른 빌드 파일 간에 완전히 전환하는 것도 도움이 됩니다.settings.gradle이 솔루션은 환경변수를 읽습니다.BUILD_PROFILE에 삽입합니다.buildFileName:

# File: settings.gradle
println "> Processing settings.gradle"
def buildProfile = System.getenv("BUILD_PROFILE")
if(buildProfile != null) {
    println "> Build profile: $buildProfile"
    rootProject.buildFileName = "build-${buildProfile}.gradle"
}
println "> Build file: $rootProject.buildFileName"

그런 다음 gradle을 이렇게 실행합니다. 예를 들어,build-local.gradle:

$ BUILD_PROFILE="local" gradle compileJava
> Processing settings.gradle
> Build profile: local
> Build file: build-local.gradle

BUILD SUCCESSFUL in 3s

이 접근방식은 품질 게이트 체크 등 시간이 걸리는 작업을 로컬에서 실행하고 싶지 않은 작업을 추가하는 CI/CD 파이프라인에서도 사용할 수 있습니다.

언급URL : https://stackoverflow.com/questions/40659986/maven-profiles-equivalent-of-gradle

반응형