Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用gradle执行不在项目根目录中的webpack_Java_Gradle_Webpack - Fatal编程技术网

Java 使用gradle执行不在项目根目录中的webpack

Java 使用gradle执行不在项目根目录中的webpack,java,gradle,webpack,Java,Gradle,Webpack,我的项目结构类似于以下内容: -- root -- gradlew -- build.gradle -- src -- main -- frontend -- webpack.config.js -- package.json -- src -- node_modules -- java -- resources -- static

我的项目结构类似于以下内容:

-- root
  -- gradlew
  -- build.gradle
  -- src
     -- main
       -- frontend
          -- webpack.config.js
          -- package.json
          -- src
          -- node_modules
       -- java
       -- resources
          -- static
我想让gradle运行
root/src/main/frontend
文件夹中的
webpack.config.js
,并将其写入
src/main/java/resources/static
中的
bundle.js
。我尝试了以下脚本:

task myNpmInstall(type: NodeTask) {
    args = ['install']
    inputs.file file('src/main/frontend/package.json')
    outputs.dir file('src/main/frontend/node_modules')
}

task webpack(type: NodeTask, dependsOn: 'myNpmInstall') {
    script = file('src/main/frontend/node_modules/.bin/webpack')
}

processResources.dependsOn 'webpack'
这是我的
包的一部分。json

  "engines": {
    "npm": "5.5.1",
    "node": "8.9.1"
  },
  "scripts": {
    "build": "node ./node_modules/webpack/bin/webpack.js",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
    "test:watch": "npm run test -- --watch"
  },
这是
webpack.config.js

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      test: /\.js$/,
      exclude: /node_modules/,
      loader: 'babel-loader'
    }]
  }
};
我得到这个错误:

Caused by: java.lang.IllegalStateException: Required script property is not set.
No configuration file found and no output filename configured via CLI option.
A configuration file could be named 'webpack.config.js' in the current directory.
Use --help to display the CLI options.

Execution failed for task ':webpack' Caused by: org.gradle.process.internal.ExecException: Process 'command '/home/arian/workspace/myproj/.gradle/nodejs/node-v8.9.1-lin‌​ux-x64/bin/node'' finished with non-zero exit value 255
我注意到,即使在运行
/gradlew myNpmInstall
时,也会出现相同的错误。我怎样才能解决这个问题

更新1 我将gradle文件内容更新为

node {
    version = '8.9.1'
    npmWorkDir = file('src/main/frontend')
    nodeModulesDir = file('src/main/frontend')
    download = true
}

task webpack(type: NodeTask, dependsOn: 'npmInstall') {
    script = file("$projectDir/src/main/frontend/node_modules/.bin/webpack")
}

processResources.dependsOn 'webpack'
现在我得到了这个错误:

Caused by: java.lang.IllegalStateException: Required script property is not set.
No configuration file found and no output filename configured via CLI option.
A configuration file could be named 'webpack.config.js' in the current directory.
Use --help to display the CLI options.

Execution failed for task ':webpack' Caused by: org.gradle.process.internal.ExecException: Process 'command '/home/arian/workspace/myproj/.gradle/nodejs/node-v8.9.1-lin‌​ux-x64/bin/node'' finished with non-zero exit value 255

尝试将配置节添加到节点:

node {
    version = '6.11.0'
    npmWorkDir = file('src/main/frontend')
    nodeModulesDir = file('src/main/frontend')
    download = true
}
使用
node
插件,您已经有了
npmInstall
task,因此您可以删除task
myNpmInstall

请参见

首先,这里是 您可以使用
nodeModulesDir

node {
    download = true
    // Set the work directory where node_modules should be located
    nodeModulesDir = file("${project.projectDir}/src/main/frontend")
}
NodeTask
用于运行类似于
/node script
的程序。这就是为什么它抱怨没有设置
必需的脚本属性。
。但通常您真正想要的是运行
package.json

我更喜欢使用雅兰塔斯克,因为它让我感觉更快

task webpack(type: YarnTask, dependsOn: yarn) {
    args = ['build-spring'] 
    // 'build-spring' is script in your package.json you want to run
}
对于npm,它看起来像

task webpack(type: NpmTask, dependsOn: npmInstall) {
    args = ['run','build-spring']
}

什么是NodeTask?我现在得到了这个错误:
org.gradle.api.tasks.TaskExecutionException:任务执行失败:webpack'
由以下原因引起:org.gradle.process.internal.ExecutionException:process'command'/home/arian/workspace/myproj/.gradle/nodejs/node-v8.9.1-linux-x64/bin/node''完成,退出值为非零255
(我不得不更改节点版本,我得到了与您建议的版本相同的错误,因此它与版本无关)@ArianHosseinzadeh是的,但是如果你真的想在你的
包中运行一些任务,为什么你要运行
NodeTask
。json
你应该运行
NpmTask
或者
YarnTask
NodeTask
之类的东西,你真的想要它吗?如果是的,那为什么?谢谢!它成功了。你是对的,de>NodeTask需要脚本,而我的
包中有
build
设置。json
我是否需要保留
processResources.dependsOn'webpack'
?@ArianHosseinzadeh它将在每个gradle构建中运行
webpack
任务,所以我通常只为生产和调试客户端分别启用它。这是说的您可以设置“在未进行任何更改的情况下避免执行节点/npm/warn任务”,但我从未使用过它。