如何从gradle任务运行mvn命令?

时间:2020-02-09 15:13:17

标签: java maven gradle

我有一个结构如下的项目:

enter image description here

maven-as-submodule -仅仅是一个gradle项目; graph-gen -是gradle项目中的Maven模块(不是子模块)

好吧,我的目标是运行命令:mvn graphwalker:generate-sources 从gradle任务开始。所以我写了一些:

task generateGraphBasedInterfaces(type: Exec) {
    workingDir './graph-gen'
    commandLine 'mvn graphwalker:generate-sources'
}

一切似乎都还好,我可以在/ graph-gen目录中运行命令mvn graphwalker:generate-sources而不会出现问题。

但是当我尝试通过gradle任务运行它时-最终会出错。因此,这是一些堆栈跟踪:

  • 异常是:org.gradle.api.tasks.TaskExecutionException:任务':generateGraphBasedInterfaces'的执行失败。 在org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda $ executeIfValid $ 1(ExecuteActionsTaskExecuter.java:187) 在org.gradle.internal.Try $ Failure.ifSuccessfulOrElse(Try.java:263) 在org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:185) 在org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:166) 在org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:109) 在org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46) 在org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:62) 在org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57) 在org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:56) 在org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36) 在org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter $ 1.executeTask(EventFiringTaskExecuter.java:77) 在org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter $ 1.call(EventFiringTaskExecuter.java:55) 在org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter $ 1.call(EventFiringTaskExecuter.java:52) 在org.gradle.internal.operations.DefaultBuildOperationExecutor $ CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:416) 在org.gradle.internal.operations.DefaultBuildOperationExecutor $ CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:406)处 在org.gradle.internal.operations.DefaultBuildOperationExecutor $ 1.execute(DefaultBuildOperationExecutor.java:165) 在org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250) 在org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158) 在org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:102) 在org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)处 在org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52) 在org.gradle.execution.plan.LocalTask​​NodeExecutor.execute(LocalTask​​NodeExecutor.java:41) 在org.gradle.execution.taskgraph.DefaultTaskExecutionGraph $ InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:374)处 在org.gradle.execution.taskgraph.DefaultTaskExecutionGraph $ InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:361)处 在org.gradle.execution.taskgraph.DefaultTaskExecutionGraph $ BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:354)处 在org.gradle.execution.taskgraph.DefaultTaskExecutionGraph $ BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:340)处 在org.gradle.execution.plan.DefaultPlanExecutor $ ExecutorWorker.lambda $ run $ 0(DefaultPlanExecutor.java:127)中 在org.gradle.execution.plan.DefaultPlanExecutor $ ExecutorWorker.execute(DefaultPlanExecutor.java:191)处 在org.gradle.execution.plan.DefaultPlanExecutor $ ExecutorWorker.executeNextNode(DefaultPlanExecutor.java:182) 在org.gradle.execution.plan.DefaultPlanExecutor $ ExecutorWorker.run(DefaultPlanExecutor.java:124) 在org.gradle.internal.concurrent.ExecutorPolicy $ CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64) 在org.gradle.internal.concurrent.ManagedExecutorImpl $ 1.run(ManagedExecutorImpl.java:48) 在org.gradle.internal.concurrent.ThreadFactoryImpl $ ManagedThreadRunnable.run(ThreadFactoryImpl.java:56) 引起原因:org.gradle.process.internal.ExecException:一个问题 发生启动过程'命令'MVN graphwalker:generate-sources' 在org.gradle.process.internal.DefaultExecHandle.execExceptionFor(DefaultExecHandle.java:241) 在org.gradle.process.internal.DefaultExecHandle.setEndStateInfo(DefaultExecHandle.java:218) 在org.gradle.process.internal.DefaultExecHandle.failed(DefaultExecHandle.java:372) 在org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:87) 在org.gradle.internal.operations.CurrentBuildOperationPreservingRunnable.run(CurrentBuildOperationPreservingRunnable.java:42) ... 3更多原因:net.rubygrapefruit.platform.NativeException:无法启动'mvn graphwalker:generate-sources' 在net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27) 在net.rubygrapefruit.platform.internal.WrapperProcessLauncher.start(WrapperProcessLauncher.java:36) 在org.gradle.process.internal.ExecHandleRunner.startProcess(ExecHandleRunner.java:98) 在org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:71) ... 4更多原因:java.io.IOException:无法运行程序“ mvn graphwalker:generate-sources”(在目录中) “ /用户/ Sanchellios / WORK_DIR / experiments / maven-as-submodule / graph-gen”): 错误= 2,没有这样的文件或目录 在net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25) ... 7更多原因:java.io.IOException:错误= 2,没有这样的文件或目录 ...还有8个

我什至尝试在 graph-gen 模块内部生成Maven包装器-它没有帮助。

将任务更改为

任务runMvnInterfaceGenerator(type:Exec){ DependOn'copyGraphsToGenerator' workingDir'./graph-gen' args'graphwalker:generate-sources' 可执行文件“ MVN” }

运作良好。那么,commandLine有什么问题?

1 个答案:

答案 0 :(得分:1)

commandLine参数需要单独传递:

task generateGraphBasedInterfaces(type: Exec) {
    workingDir rootProject.getProjectDir().getAbsolutePath() + "/graph-gen"
    commandLine "mvn", "graphwalker:generate-sources"
}

还要确保$PATH上有mvn / mvn.cmd