我是Gradle的新手。我昨天开始阅读它。我找到了一个构建节点应用程序的示例build.gradle。我对文件的内容有些困惑。我不确定哪些是保留字或预定义字。字符串之一是node
。它没有在某处使用,但我发现它是节点插件所需要的。
buildscript {
repositories {
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'com.moowork.gradle:gradle-node-plugin:1.2.0'
}
}
apply plugin: 'base'
apply plugin: 'com.moowork.node' // gradle-node-plugin
node {
/* gradle-node-plugin configuration
https://github.com/srs/gradle-node-plugin/blob/master/docs/node.md
Task name pattern:
./gradlew npm_<command> Executes an NPM command.
*/
// Version of node to use.
version = '10.14.1'
// Version of npm to use.
npmVersion = '6.4.1'
// If true, it will download node using above parameters.
// If false, it will try to use globally installed node.
download = true
}
npm_run_build {
// make sure the build task is executed only when appropriate files change
inputs.files fileTree('public')
inputs.files fileTree('src')
// 'node_modules' appeared not reliable for dependency change detection (the task was rerun without changes)
// though 'package.json' and 'package-lock.json' should be enough anyway
inputs.file 'package.json'
inputs.file 'package-lock.json'
outputs.dir 'build'
}
// pack output of the build into JAR file
task packageNpmApp(type: Zip) {
dependsOn npm_run_build
baseName 'npm-app'
extension 'jar'
destinationDir file("${projectDir}/build_packageNpmApp")
from('build') {
// optional path under which output will be visible in Java classpath, e.g. static resources path
into 'static'
}
}
// declare a dedicated scope for publishing the packaged JAR
configurations {
npmResources
}
configurations.default.extendsFrom(configurations.npmResources)
// expose the artifact created by the packaging task
artifacts {
npmResources(packageNpmApp.archivePath) {
builtBy packageNpmApp
type 'jar'
}
}
assemble.dependsOn packageNpmApp
String testsExecutedMarkerName = "${projectDir}/.tests.executed"
task test(type: NpmTask) {
dependsOn assemble
// force Jest test runner to execute tests once and finish the process instead of starting watch mode
environment CI: 'true'
args = ['run', 'test']
inputs.files fileTree('src')
inputs.file 'package.json'
inputs.file 'package-lock.json'
// allows easy triggering re-tests
doLast {
new File(testsExecutedMarkerName).text = 'delete this file to force re-execution JavaScript tests'
}
outputs.file testsExecutedMarkerName
}
check.dependsOn test
clean {
delete packageNpmApp.archivePath
delete testsExecutedMarkerName
}
此外,如何解析build.gradle?我还想知道它如何神奇地下载节点和npm工具。
答案 0 :(得分:1)
这是一个非常笼统的提要:
*.gradle
文件包含配置块(closures
),以指定应运行 HOW 逻辑。com.moowork.node
插件是通过node
扩展块配置的。download = true
配置告诉插件下载项目根目录中的节点(version = '10.14.1'
)和nmp(npmVersion = '6.4.1'
)(除非您也覆盖其默认值)。希望这会有所帮助。
答案 1 :(得分:0)
在您的代码段中,只有 true
是关键字,其他的是来自 Gradle 或 Node JS 插件的方法或 getter:
apply plugin: ...
是来自 org.gradle.api.Project.apply(java.util.Map<String, ?>)
node
是 Gradle 自动生成的方法,签名为 void node(Closure<com.moowork.gradle.node.NodeExtension>)
(接受代码块的方法),参见 https://github.com/srs/gradle-node-plugin/blob/master/src/main/groovy/com/moowork/gradle/node/NodeExtension.groovynode { version = ... }
- version
、npmVersion
是来自 NodeExtension
类的字段