具有多个应用程序的 Gradle 根项目
Posted
技术标签:
【中文标题】具有多个应用程序的 Gradle 根项目【英文标题】:Gradle root project with multiple applications 【发布时间】:2019-03-18 04:13:37 【问题描述】:如何创建一个包含多个使用相同库的应用程序的 gradle 根项目。我希望得到包含以下内容的 tar 和 zip 文件:
RootProject/
bin/
Command1
Command2
...
lib/
Library.jar
..
我创建了一个 gradle 根项目、库子项目和几个应用程序子项目:
RootProject/
settings.gradle
build.gradle
common.gradle
Library/
build.gradle
src/main/java/Library.java
Command1/
build.gradle
src/main/java/Command1.java
Command2/
build.gradle
src/main/java/Command2.java
我当前根项目的 build.gradle:
apply plugin: 'base' // To add "clean" task to the root project.
allprojects
repositories
mavenCentral();
subprojects
apply plugin: 'java'
group = 'edu.csusb.ecs.gmailsettings'
version = '1.0-SNAPSHOT'
dependencies
testCompile group: 'junit', name: 'junit', version: '4.10'
task build(type: Copy)
dependsOn subprojects.build
from subprojects.collect it.tasks.withType(Jar)
into project.rootProject.buildDir.toString() + "/libs"
task clean(group:project.rootProject.ext.group, type: Delete, description:"clean jars from subprojects")
delete fileTree(project.buildDir).include('**/*')
导致错误
org.gradle.tooling.BuildException:无法运行构建操作使用 梯度分布 'https://services.gradle.org/distributions/gradle-4.5.1-bin.zip'。在 org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:51) 在 org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:29) 在 org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:41) 在 org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:57) 在 org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) 在 org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 在 org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) 在 java.lang.Thread.run(Thread.java:748) 在 org.gradle.tooling.internal.consumer.BlockingResultHandler.getResult(BlockingResultHandler.java:46) 在 org.gradle.tooling.internal.consumer.DefaultBuildActionExecuter.run(DefaultBuildActionExecuter.java:60) 在 org.netbeans.gradle.model.GenericModelFetcher.getModels(GenericModelFetcher.java:168) 在 org.netbeans.gradle.project.model.NbGradle18ModelLoader$ProjectModelFetcher.getModels(NbGradle18ModelLoader.java:379) 在 org.netbeans.gradle.project.model.NbGradle18ModelLoader.loadModels(NbGradle18ModelLoader.java:68) 在 org.netbeans.gradle.project.model.DefaultGradleModelLoader.loadModelWithProgress(DefaultGradleModelLoader.java:559) 在 org.netbeans.gradle.project.model.DefaultGradleModelLoader.access$1600(DefaultGradleModelLoader.java:64) 在 org.netbeans.gradle.project.model.DefaultGradleModelLoader$6.run(DefaultGradleModelLoader.java:419) 在 org.netbeans.gradle.project.tasks.GradleDaemonManager.runNonBlockingGradleTask(GradleDaemonManager.java:35) 在 org.netbeans.gradle.project.tasks.GradleDaemonManager.access$100(GradleDaemonManager.java:22) 在 org.netbeans.gradle.project.tasks.GradleDaemonManager$2.execute(GradleDaemonManager.java:125) 在 org.jtrim.concurrent.AbstractTaskExecutorService$FunctionWrapper.execute(AbstractTaskExecutorService.java:270) 在 org.jtrim.concurrent.AbstractTaskExecutorService$TaskOfAbstractExecutor.execute(AbstractTaskExecutorService.java:340) 在 org.jtrim.concurrent.Tasks$RunOnceCancelableTask.execute(Tasks.java:342) 在 org.jtrim.concurrent.SingleThreadedExecutor$QueuedItem.runTask(SingleThreadedExecutor.java:919) 在 org.jtrim.concurrent.SingleThreadedExecutor$QueuedItem.access$1200(SingleThreadedExecutor.java:898) 在 org.jtrim.concurrent.SingleThreadedExecutor$Impl$Worker.executeTask(SingleThreadedExecutor.java:815) 在 org.jtrim.concurrent.SingleThreadedExecutor$Impl$Worker.processQueue(SingleThreadedExecutor.java:827) 在 org.jtrim.concurrent.SingleThreadedExecutor$Impl$Worker.run(SingleThreadedExecutor.java:861) 在 org.jtrim.concurrent.SingleThreadedExecutor$Impl$1.run(SingleThreadedExecutor.java:453) 在 java.lang.Thread.run(Thread.java:748) 引起: org.gradle.internal.exceptions.LocationAwareException:构建文件 '/Users/000017913/Development/GmailSettings/build.gradle' 行:20 A 评估根项目“GmailSettings”时出现问题。在 org.gradle.initialization.DefaultExceptionAnalyser.transform(DefaultExceptionAnalyser.java:74) 在 org.gradle.initialization.MultipleBuildFailuresExceptionAnalyser.transform(MultipleBuildFailuresExceptionAnalyser.java:47) 在 org.gradle.initialization.StackTraceSanitizingExceptionAnalyser.transform(StackTraceSanitizingExceptionAnalyser.java:30) 在 org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:137) 在 org.gradle.initialization.DefaultGradleLauncher.getConfiguredBuild(DefaultGradleLauncher.java:104) 在 org.gradle.internal.invocation.GradleBuildController$2.call(GradleBuildController.java:87) 在 org.gradle.internal.invocation.GradleBuildController$2.call(GradleBuildController.java:84) 在 org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:152) 在 org.gradle.internal.invocation.GradleBuildController.doBuild(GradleBuildController.java:100) 在 org.gradle.internal.invocation.GradleBuildController.configure(GradleBuildController.java:84) 在 org.gradle.tooling.internal.provider.runner.ClientProvidedBuildActionRunner.run(ClientProvidedBuildActionRunner.java:64) 在 org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35) 在 org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35) 在 org.gradle.tooling.internal.provider.ValidatingBuildActionRunner.run(ValidatingBuildActionRunner.java:32) 在 org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner$1.run(RunAsBuildOperationBuildActionRunner.java:43) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110) 在 org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner.run(RunAsBuildOperationBuildActionRunner.java:40) 在 org.gradle.tooling.internal.provider.SubscribableBuildActionRunner.run(SubscribableBuildActionRunner.java:51) 在 org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:49) 在 org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:32) 在 org.gradle.launcher.exec.BuildTreeScopeBuildActionExecuter.execute(BuildTreeScopeBuildActionExecuter.java:39) 在 org.gradle.launcher.exec.BuildTreeScopeBuildActionExecuter.execute(BuildTreeScopeBuildActionExecuter.java:25) 在 org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:80) 在 org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:53) 在 org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:57) 在 org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:32) 在 org.gradle.tooling.internal.provider.GradleThreadBuildActionExecuter.execute(GradleThreadBuildActionExecuter.java:36) 在 org.gradle.tooling.internal.provider.GradleThreadBuildActionExecuter.execute(GradleThreadBuildActionExecuter.java:25) 在 org.gradle.tooling.internal.provider.ParallelismConfigurationBuildActionExecuter.execute(ParallelismConfigurationBuildActionExecuter.java:43) 在 org.gradle.tooling.internal.provider.ParallelismConfigurationBuildActionExecuter.execute(ParallelismConfigurationBuildActionExecuter.java:29) 在 org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:64) 在 org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:29) 在 org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:59) 在 org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:44) 在 org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:45) 在 org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:30) 在 org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:67) 在 org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74) 在 org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72) 在 org.gradle.util.Swapper.swap(Swapper.java:38) 在 org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.LogAndCheckHealth.execute(LogAndCheckHealth.java:55) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:62) 在 org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:82) 在 org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 在 org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:122) 在 org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50) 在 org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:295) 在 org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) 在 org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46) 在 org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) 引起:org.gradle.api.GradleScriptException:出现问题 评估根项目“GmailSettings”。在 org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:92) 在 org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl$2.run(DefaultScriptPluginFactory.java:204) 在 org.gradle.configuration.ProjectScriptTarget.addConfiguration(ProjectScriptTarget.java:77) 在 org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:209) 在 org.gradle.configuration.BuildOperationScriptPlugin$1.run(BuildOperationScriptPlugin.java:61) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110) 在 org.gradle.configuration.BuildOperationScriptPlugin.apply(BuildOperationScriptPlugin.java:58) 在 org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:41) 在 org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:26) 在 org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34) 在 org.gradle.configuration.project.LifecycleProjectEvaluator.doConfigure(LifecycleProjectEvaluator.java:64) 在 org.gradle.configuration.project.LifecycleProjectEvaluator.access$100(LifecycleProjectEvaluator.java:34) 在 org.gradle.configuration.project.LifecycleProjectEvaluator$ConfigureProject.run(LifecycleProjectEvaluator.java:110) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110) 在 org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:50) 在 org.gradle.api.internal.project.DefaultProject.evaluate(DefaultProject.java:667) 在 org.gradle.api.internal.project.DefaultProject.evaluate(DefaultProject.java:136) 在 org.gradle.execution.TaskPathProjectEvaluator.configure(TaskPathProjectEvaluator.java:35) 在 org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:60) 在 org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:38) 在 org.gradle.initialization.DefaultGradleLauncher$ConfigureBuild.run(DefaultGradleLauncher.java:249) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110) 在 org.gradle.initialization.DefaultGradleLauncher.configureBuild(DefaultGradleLauncher.java:167) 在 org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:126) ... 62 更多原因:org.gradle.api.InvalidUserDataException: 使用标准 Gradle 生命周期时声明自定义“构建”任务 插件是不允许的。在 org.gradle.language.base.plugins.LifecycleBasePlugin$7.execute(LifecycleBasePlugin.java:142) 在 org.gradle.language.base.plugins.LifecycleBasePlugin$7.execute(LifecycleBasePlugin.java:138) 在 org.gradle.internal.ImmutableActionSet$CompositeSet.execute(ImmutableActionSet.java:196) 在 org.gradle.internal.MutableActionSet.execute(MutableActionSet.java:35) 在 org.gradle.api.internal.DefaultDomainObjectCollection.doAdd(DefaultDomainObjectCollection.java:204) 在 org.gradle.api.internal.DefaultDomainObjectCollection.add(DefaultDomainObjectCollection.java:198) 在 org.gradle.api.internal.DefaultNamedDomainObjectCollection.add(DefaultNamedDomainObjectCollection.java:93) 在 org.gradle.api.internal.tasks.DefaultTaskContainer.addTask(DefaultTaskContainer.java:101) 在 org.gradle.api.internal.tasks.DefaultTaskContainer.create(DefaultTaskContainer.java:81) 在 org.gradle.api.internal.project.DefaultProject.task(DefaultProject.java:1209) 在 org.gradle.internal.metaobject.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:479) 在 org.gradle.internal.metaobject.BeanDynamicObject.tryInvokeMethod(BeanDynamicObject.java:191) 在 org.gradle.internal.metaobject.CompositeDynamicObject.tryInvokeMethod(CompositeDynamicObject.java:98) 在 org.gradle.internal.metaobject.MixInClosurePropertiesAsMethodsDynamicObject.tryInvokeMethod(MixInClosurePropertiesAsMethodsDynamicObject.java:30) 在 org.gradle.groovy.scripts.BasicScript$ScriptDynamicObject.tryInvokeMethod(BasicScript.java:134) 在 org.gradle.internal.metaobject.AbstractDynamicObject.invokeMethod(AbstractDynamicObject.java:160) 在 org.gradle.groovy.scripts.BasicScript.invokeMethod(BasicScript.java:83) 在 build_dycfbq39lcq7wcyu7h089vex6.run(/Users/000017913/Development/GmailSettings/build.gradle:20) 在 org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:90) ... 94 更多
【问题讨论】:
您需要一个包含所有Command*
的单一发行版?为什么?它是如何工作的?每个Command*
都是一个可运行的应用程序?
我希望 NetBeans 8.2(带有 Gradle 支持插件)构建一个包含命令集合的发行版。
【参考方案1】:
在我的根项目 build.gradle 中,我定义了一个任务,将所有子项目的 jar 文件复制到根项目的 /build/libs
文件夹中,如下所示:
task build(type: Copy)
dependsOn subprojects.build
// from subprojects.collect it.tasks.withType(AbstractArchiveTask)
from subprojects.collect it.tasks.withType(Jar)
into project.rootProject.buildDir.toString() + "/libs"
task clean(group:project.rootProject.ext.group, type: Delete, description:"clean jars from subprojects")
delete fileTree(project.buildDir).include('**/*')
您应该能够以类似的方式将任何其他构建工件复制到根项目。
【讨论】:
我收到 org.gradle.tooling.BuildException。 可能您的根项目没有 ext.group 属性或没有多项目? 你的根项目没有ext.group 你也可以去掉ext.【参考方案2】:感谢大家的帮助。从您的输入中得出的以下内容似乎有效。
task copyJars(type: Copy)
dependsOn subprojects.build
from subprojects.collect it.tasks.withType(Jar)
into project.rootProject.buildDir.toString() + "/libs"
task copyDependencies(type: Copy)
from subprojects.collect it.configurations.runtime
into project.rootProject.buildDir.toString() + "/libs"
task copyStartScripts(type: Copy)
dependsOn copyJars
from subprojects.collect it.tasks.withType(CreateStartScripts)
into project.rootProject.buildDir.toString() + "/scripts"
task createDistribution()
apply plugin: 'distribution'
distributions
main
contents
from(copyStartScripts)
into "bin"
fileMode = 0755
from(copyJars)
into "lib"
from(copyDependencies)
into "lib"
【讨论】:
以上是关于具有多个应用程序的 Gradle 根项目的主要内容,如果未能解决你的问题,请参考以下文章