首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Gradle构建NodeJS

使用Gradle构建NodeJS
EN

Stack Overflow用户
提问于 2019-05-11 08:09:59
回答 2查看 10.6K关注 0票数 7

我是Gradle的新手。我昨天开始读到这篇文章。我找到了一个构建节点应用程序的示例build.gradle。我对文件的内容有点困惑。我不确定哪些是保留字,哪些是预定义字。其中一个字符串是node。它并没有在某个地方使用,但我发现它是节点插件所需要的。

代码语言:javascript
复制
    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工具的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-13 13:40:09

这是一个非常概括的概要:

  • Gradle旨在从包含configuration *.gradle (closures)的配置块文件中隐藏逻辑,以指定逻辑应该如何运行。
  • 插件为gradle增加了更多可配置的逻辑。
  • 此外,在gradle及其插件中强调“约定重于配置”,提供合理的默认值以最大限度地减少开发人员的配置工作。通过gradle扩展block.
  • Extension blocks
  • com.moowork.node插件进行配置,以允许插件向标准gradle模型添加更多保留字。
  • download = true配置告诉插件将节点(version = '10.14.1')和nmp (npmVersion = '6.4.1')下载到项目根目录中(除非您同时覆盖其默认值)。<代码>H224<代码>H125当调用插件的任何任务时,将下载这些工具。<代码>H226<代码>F227

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2021-02-05 19:53:42

在你的代码片段中,只有true是关键字,其他的是来自Gradle或Node插件的方法或getters:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56086170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档