gpt4 book ai didi

node.js - 使用 Gradle 构建 NodeJS

转载 作者:搜寻专家 更新时间:2023-11-01 00:48:12 24 4
gpt4 key购买 nike

我是 Gradle 的新手。我昨天开始阅读它。我找到了一个构建 Node 应用程序的示例 build.gradle。我对文件的内容有点困惑。我不确定哪些是保留词或预定义词。其中一个字符串是 node。它没有在某处使用,但我发现 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是怎么解析的?我还想知道它是如何神奇地下载 Node 和 npm 工具的。

最佳答案

这是一个非常笼统的概要:

  • Gradle 旨在向开发人员隐藏逻辑。
  • 大多数 *.gradle 文件包含配置 block (闭包)以指定如何逻辑应该运行。
  • 插件通过更多可配置逻辑增强了 gradle。
  • 此外,“约定优于配置”是 gradle 及其插件中强调的一种做法,提供合理的默认值以最大程度地减少开发人员的配置工作。
  • com.moowork.node插件通过 node 扩展 block 配置。
  • Extension blocks是 gradle 允许插件向标准 gradle 模型添加更多“保留”词的方式。
  • download = true 配置告诉插件下载 Node (version = '10.14.1') 和 nmp (npmVersion = '6.4.1' ) 在项目的根目录中(除非您也覆盖其默认值)。
  • 这些工具的下载将在调用任何插件任务时发生。

希望这对您有所帮助。

关于node.js - 使用 Gradle 构建 NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56086170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com