【问题标题】:Best way to handle Gradle boilerplate?处理 Gradle 样板的最佳方法?
【发布时间】:2019-10-08 15:24:07
【问题描述】:

我正在使用双启动机器。大多数情况下,我将在 Linux (Linux Mint 18.3) 中进行编码,但偶尔会在 W10 分区中进行编码。

对于给定的 Gradle 项目,我总是希望使用“installDist”的可执行文件输出到相同的位置。因此,我在 build.gradle 中包含了以下几行:

def currentOS = org.gradle.internal.os.OperatingSystem.current()
def platform
if (currentOS.isWindows()) {
    platform = 'win'
} else if (currentOS.isLinux()) {
    platform = 'linux'
} else if (currentOS.isMacOsX()) {
    // platform = 'mac'
    throw new Exception( "not configured for Mac OS: $currentOS" )
}
else {
    throw new Exception( "attempt to run on unknown platform: $currentOS" )
}
println "OS/platform |$platform|"
String operativeDir
def homePath = System.properties['user.home']
// in W10 homePath is apparently "D:\My Documents"
// in linux homePath is "/home/mike"
// no doubt I could use java.nio.file.Path for a more elegant solution:
String pathSeparator = platform == 'linux'? '/' : '\\'
// would be better to get the My Documents path from an Env Var in Linux:
String myDoxPath = platform == 'linux'? '/media/mike/W10 D drive/My Documents' : homePath 
operativeDir = "$myDoxPath${pathSeparator}software projects${pathSeparator}operative${pathSeparator}${name}"
// get version number: must be a version.properties file at the path indicated
ConfigObject conf = new ConfigSlurper().parse( file("src/main/resources/version.properties").toURI().toURL())
version = conf.versionNumber
println "version is $version"

installDist{
    destinationDir = file( "$operativeDir/$version" )
}

...问题是,所有这些都是样板:我希望将它包含在我开发的每个项目中。目前它使我的 build.gradle 变得混乱。将这个样板作为 Gradle 的某种模块“加载”的标准/最佳方式是什么?

【问题讨论】:

  • 是的,基本思想是创建您自己的 Gradle 插件,该插件公开您需要的属性,并将其作为项目依赖项包含到您的构建中。我有使用 Gradle 的基本经验,并在付费教程中找到了这种方法。我不会发布答案,因为我缺乏编写正确代码的经验

标签: gradle boilerplate


【解决方案1】:

正如 usr-local-ΕΨΗΕΛΩΝ 在 cmets 中指出的那样,如果您发现自己在许多项目中重复构建逻辑,则自定义 Gradle 插件是解决方案。

您可以使用Build Init Plugin 生成骨架Gradle 插件。您需要全局安装 Gradle,以便在任何地方调用 gradle。请参阅here 安装 Gradle 的说明。

例如,要定义platform 属性,您的插件可以执行以下操作:

import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Plugin;
import org.gradle.internal.os.OperatingSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExamplePlugin implements Plugin<Project> {

    private static final Logger log = LoggerFactory.getLogger(ExamplePlugin.class);

    public void apply(Project project) {
        String platform = inferPlatform(project);
        project.getExtensions().getExtraProperties().set("platform", platform);
        log.info("OS/platform |{}|", platform);
    }

    private String inferPlatform(Project project) {
        // OperatingSystem is an INTERNAL package.
        // It is strongly recommended you not use internal Gradle APIs.
        OperatingSystem currentOS = OperatingSystem.current();
        if (currentOS.isWindows()) {
            return "win";
        } else if (currentOS.isLinux()) {
            return "linux";
        } else {
            throw new GradleException("unsupported OS: " + currentOS.toString());
        }
    }
}

我给出了一个 Java 示例,但您也可以使用 Groovy 或 Kotlin 编写插件。

拥有插件后,只需将其发布到本地或 Gradle 插件门户。

【讨论】:

  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
相关资源
最近更新 更多