【发布时间】: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