2012-10-10 59 views
4

使用下面的搖籃動作,停止Windows服務不運行的原因構建失敗

developerBuild << { 
    exec{ 
     commandLine = ['cmd', '/c', 'net', 'stop', 'Foo'] 
    } 
} 

如果我執行這項任務,Windows服務Foo沒有運行,我會得到以下例外,

The Foo service is not started. 

More help is available by typing NET HELPMSG 3521. 

FAILURE: Build failed with an exception. 

* Where: 
Build file 'C:\...\build.gradle' line: .. 

* What went wrong: 
Execution failed for task ':developerBuild'. 
> Command 'cmd' finished with (non-zero) exit value 2. 

* Try: 
Run with --info or --debug option to get more log output. 

* Exception is: 
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':developerBuild'. 
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:68) 
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46) 
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:34) 
    at org.gradle.api.internal.changedetection.CacheLockHandlingTaskExecuter$1.run(CacheLockHandlingTaskExecuter.java:34) 
    at org.gradle.cache.internal.DefaultCacheAccess$2.create(DefaultCacheAccess.java:200) 
    at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(DefaultCacheAccess.java:172) 
    at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(DefaultCacheAccess.java:198) 
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.longRunningOperation(DefaultPersistentDirectoryStore.java:111) 
    at org.gradle.api.internal.changedetection.DefaultTaskArtifactStateCacheAccess.longRunningOperation(DefaultTaskArtifactStateCacheAccess.java:83) 
    at org.gradle.api.internal.changedetection.CacheLockHandlingTaskExecuter.execute(CacheLockHandlingTaskExecuter.java:32) 
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:55) 
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:57) 
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:41) 
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:51) 
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:52) 
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:42) 
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:247) 
    at org.gradle.execution.DefaultTaskGraphExecuter.executeTask(DefaultTaskGraphExecuter.java:192) 
    at org.gradle.execution.DefaultTaskGraphExecuter.doExecute(DefaultTaskGraphExecuter.java:177) 
    at org.gradle.execution.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:83) 
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:36) 
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61) 
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23) 
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:67) 
    at org.gradle.api.internal.changedetection.TaskCacheLockHandlingBuildExecuter$1.run(TaskCacheLockHandlingBuildExecuter.java:31) 
    at org.gradle.cache.internal.DefaultCacheAccess$1.create(DefaultCacheAccess.java:111) 
    at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:126) 
    at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:109) 
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.useCache(DefaultPersistentDirectoryStore.java:103) 
    at org.gradle.api.internal.changedetection.DefaultTaskArtifactStateCacheAccess.useCache(DefaultTaskArtifactStateCacheAccess.java:79) 
    at org.gradle.api.internal.changedetection.TaskCacheLockHandlingBuildExecuter.execute(TaskCacheLockHandlingBuildExecuter.java:29) 
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61) 
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23) 
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:67) 
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32) 
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61) 
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:54) 
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:155) 
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:110) 
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:78) 
    at org.gradle.tooling.internal.provider.ExecuteBuildAction.run(ExecuteBuildAction.java:31) 
    at org.gradle.tooling.internal.provider.ConfiguringBuildAction.run(ConfiguringBuildAction.java:94) 
    at org.gradle.launcher.daemon.protocol.Build.run(Build.java:68) 
    at org.gradle.launcher.daemon.protocol.Build.run(Build.java:64) 
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:45) 
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:45) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:24) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.StartStopIfBuildAndStop.execute(StartStopIfBuildAndStop.java:28) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.ReturnResult.execute(ReturnResult.java:34) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$4.call(ForwardClientInput.java:116) 
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$4.call(ForwardClientInput.java:114) 
    at org.gradle.util.Swapper.swap(Swapper.java:38) 
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:114) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:60) 
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:61) 
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy.doBuild(StartBuildOrRespondWithBusy.java:49) 
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.HandleStop.execute(HandleStop.java:34) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.CatchAndForwardDaemonFailure.execute(CatchAndForwardDaemonFailure.java:32) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.HandleClientDisconnectBeforeSendingCommand.execute(HandleClientDisconnectBeforeSendingCommand.java:21) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.StopConnectionAfterExecution.execute(StopConnectionAfterExecution.java:27) 
    at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 
    at org.gradle.launcher.daemon.server.exec.DefaultDaemonCommandExecuter.executeCommand(DefaultDaemonCommandExecuter.java:55) 
    at org.gradle.launcher.daemon.server.Daemon$1$1.run(Daemon.java:123) 
    at org.gradle.messaging.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66) 
Caused by: org.gradle.process.internal.ExecException: Command 'cmd' finished with (non-zero) exit value 2. 
    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:339) 
    at org.gradle.process.internal.DefaultExecAction.execute(DefaultExecAction.java:39) 
    at org.gradle.api.internal.file.DefaultFileOperations.exec(DefaultFileOperations.java:160) 
    at org.gradle.api.internal.project.AbstractProject.exec(AbstractProject.java:818) 
    at org.gradle.api.Project$exec.call(Unknown Source) 
    at org.gradle.api.Project$exec.call(Unknown Source) 
    at org.gradle.groovy.scripts.DefaultScript.exec(DefaultScript.groovy:161) 
    at build_3fpei35pj0kg0lnicte94004ar$_run_closure5_closure11.doCall(C:\Users\meherts\workspace\SSAgentPlugin\build.gradle:67) 
    at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:452) 
    at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:436) 
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:60) 
    ... 79 more 


BUILD FAILED 

這會導致整個任務的執行停止,爲什麼?無論操作中的命令行調用的退出代碼如何,是否有辦法繼續執行該任務?

回答

6

您可以在一個ExecSpec禁止出口值檢查的過程如下:

developerBuild << { 
    exec{ 
    commandLine = ['cmd', '/c', 'net', 'stop', 'Foo'] 
    ignoreExitValue = true 
    } 
} 
+0

非常好,謝謝! – user1329572

2

這可能是一個黑客位的,但我認爲它的工作原理:

developerBuild << { 
    try { 
     exec{ 
      commandLine = ['cmd', '/c', 'net', 'stop', 'Foo'] 
     } 
    } catch (TaskExecutionException e) { 
     //Do whatever you want 
    } 
} 

它看起來像gradle這個默認的執行失敗拋出異常(這是我會做什麼了,我實現它) 。解決方案是捕捉這個「業務異常」。

+0

光滑!我不知道這種能力。 :) – user1329572

+0

你的解決方案也工作,但接受的答案是一個更健全的方法恕我直言。再次感謝你! :D – user1329572

+1

我同意。當你不需要處理失敗的命令行執行時,我的工作會過度。 – mathiasbn

相關問題