使用Gradle构建NodeJS

10

我对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 工具的。

1
这是一个非常广泛的问题,我建议您将其缩小到一个具体的问题。您的 build.gradle 脚本是用 Groovy 编写的。在 Gradle 中,“node” 关键字实际上不是关键字,它是一个“任务”,并且由您在问题中引用的依赖项提供。这是一个相当复杂的示例,用于学习 Gradle,您可能需要退一步,先通过一些基本的 Gradle 教程。 - Jake Holzinger
明白了。我不知道我找到的是一个复杂的例子。谢谢。 - devwannabe
1
我大部分同意@JakeHolzinger的观点,但是在这个例子中node不是一个任务,而是一个扩展,用于定义“'com.moowork.node'”插件的整体设置。这实际上展示了新用户在Gradle中遇到的主要问题之一。它需要对Gradle功能和应用插件的概念有广泛的理解。 - Lukas Körfer
嘿@devwannabe, 只有一个愚蠢的问题。packageNpmApp代表什么? 在我的情况下,这应该更改对吧?这是项目名称吗?就像我的springboot的名称是Test1,现在我已经创建了名为frontend的react-app。在这种情况下我应该使用什么? - Bug
这就像一个函数名。顺便说一下,我记得在发布那篇文章几个月后就不再使用build.gradle了。我从我的队友那里发现他只是作为概念验证才这样做的。我又回到了使用Unix shell脚本构建Node.js应用程序。 - devwannabe
显示剩余2条评论
2个回答

3

这是一个非常通用的简介:

  • Gradle旨在将逻辑隐藏在开发者之外。
  • 大多数*.gradle文件包含配置closures),用于指定逻辑应如何运行。
  • 插件通过更多可配置的逻辑增强gradle。
  • 此外,“约定优于配置”是gradle及其插件中强调的一种实践,提供合理的默认值以最小化开发人员的配置工作。
  • com.moowork.node插件是通过node扩展块进行配置的。
  • 扩展块是gradle允许插件向标准gradle模型添加更多“保留”词汇的方式。
  • download = true配置告诉插件在您项目的根目录中下载node(version = '10.14.1')和nmp(npmVersion = '6.4.1')(除非您也覆盖了它的默认值)。
  • 这些工具的下载将在任何插件任务被调用时发生。
希望这有所帮助。

2

在您的代码片段中,只有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.groovy
  • node { version = ... } - versionnpmVersionNodeExtension类中的字段
  • 其他内容同样如此,所有内容都是方法或字段。如果您使用的是IntelliJ,请使用Ctrl +鼠标单击导航到原始方法/字段声明。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接