java Maven 配置文件相当于 Gradle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40659986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:23:27  来源:igfitidea点击:

Maven profiles equivalent of Gradle

javagradlespring-bootbuild.gradlespring-boot-gradle-plugin

提问by Mehmet Catalbas

I'm trying to achieve a simple scenario in my spring boot project build: including / excluding dependencies and packaging war or jar depending on the environment.

我试图在我的 Spring Boot 项目构建中实现一个简单的场景:包括/排除依赖项和根据环境打包 war 或 jar。

So for example, for the environment devinclude devtools and package jar, for prodpackage war etc.

例如,对于环境dev包括 devtools 和 package jar,对于prodpackage war 等。

I know it is not XML based configuration anymore and I can basically write if statements in my build.gradle but is there a recommended way of achieving this?

我知道它不再是基于 XML 的配置,我基本上可以在 build.gradle 中编写 if 语句,但是有推荐的方法来实现这一点吗?

Can I declare some common dependencies and refer them in a single file instead of creating multiple build files?

我可以声明一些常见的依赖项并在单个文件中引用它们而不是创建多个构建文件吗?

Is there a best practice changing build configuration based on the build target environment?

是否有根据构建目标环境更改构建配置的最佳实践?

回答by lance-java

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 }

回答by Vyacheslav Shvets

My version (inspired by Lance Java's answer):

我的版本(灵感来自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
}

回答by IPP Nerd

Sometimes it's also useful to completely switch between different build files by adding some lines of code to the file settings.gradle. This solution reads the environment variable BUILD_PROFILEand inserts it into the buildFileName:

有时,通过向文件中添加一些代码行来在不同的构建文件之间完全切换也很有用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"

Then you run gradle like this, e.g. to use build-local.gradle:

然后你像这样运行gradle,例如使用build-local.gradle

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

BUILD SUCCESSFUL in 3s

This approach also works for CI/CD pipelines where you might want to add extra tasks like checking quality gates or other time consuming things you don't want to execute locally.

这种方法也适用于 CI/CD 管道,您可能希望在其中添加额外的任务,例如检查质量门或其他不想在本地执行的耗时的事情。