Gradle 项目取决于兄弟项目创建的工件 [英] Gradle projects depending on artifacts created by sibling projects

查看:15
本文介绍了Gradle 项目取决于兄弟项目创建的工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有四个项目的 Gradle 设置,一个带有三个孩子的父级,其中 Java Servlet JSON后端"内置到一个战争文件中,然后一个静态 HTML5前端"使用它被内置到一个 zip 中.这两个都将他们的工件安装"到本地 maven 存储库.

I have this Gradle setup with four projects, a parent with three children, where a Java Servlet JSON 'backend' is built into a war-file, and then a static HTML5 'frontend' consuming this is built into a zip. Both these "installs" their artifcats to the local maven repo.

第三个兄弟项目合并"依赖于这两个工件,通过简单地将它们压缩在一起"来构建合并"战争.

但是,一旦我按预期启动并运行它,我显然必须通过从本地存储库中删除工件来测试引导场景.

However, once I had this up and running as intended, I obviously had to test the bootstrap-scenario by deleting the artifacts from the local repo.

现在我突然得到Artifact 'no.company:frontend:1.0-SNAPSHOT@zip' not found".

Now I suddenly get "Artifact 'no.company:frontend:1.0-SNAPSHOT@zip' not found".

不可能依赖当前构建将生成的工件吗?

Is it not possible to depend on artifacts which will be produced by the current build?

基于另一个想法(以及 Peter 的回复不鼓励这种 Maven 逻辑),这个版本看起来很有希望,而不是遍历 Maven(注意:它有效!):

Based on another idea (and the reply from Peter discouraging this Maven logic), this version looks promising, not traversing Maven (note: it works!):

// ## From frontend's build.gradle:
task zipFrontend(dependsOn: 'buildFrontend',  type: Zip) {
    from ('dist/')
}

// ## From backend's build.gradle:
apply plugin: 'war'

// ## From merger's build.gradle:
task mergeFrontAndBack(dependsOn: [':backend:war', 
                                   ':frontend:zipFrontend'], type: War) {
    from zipTree(project(':frontend').tasks['zipFrontend'].archivePath)
    from zipTree(project(':backend').tasks['war'].archivePath)
    destinationDir(buildDir)
}

编辑 2:

根据 Peter 关于未触及兄弟姐妹的项目结构的评论和他的具体建议,这是最终的作品(注意:它有效!):

Based upon Peter's comment about not reaching into siblings' project structure and his concrete suggestions, here's the resulting piece (note: it works!):

// ## From frontend's build.gradle:
task zipFrontend(dependsOn: 'buildFrontend',  type: Zip) {
    from ('dist/')
}
configurations { zip }
artifacts { zip zipFrontend }

// ## From backend's build.gradle:
apply plugin: 'war'
configurations { jsonOnlyWar }
artifacts { jsonOnlyWar war }

// ## From merger's build.gradle:
configurations { merge }
dependencies {
    merge project(path: ':backend', configuration: 'jsonOnlyWar')
    merge project(path: ':frontend', configuration: 'zip')
}
task mergeFrontAndBack(dependsOn: configurations.merge, type: War) {
    from { configurations.merge.collect { zipTree(it) } }
    destinationDir(buildDir)
}

推荐答案

本地 Maven 存储库(和 Gradle 的 install 任务)仅应在与 Maven 构建交换工件时使用.它不用于在 Gradle 构建的项目之间交换工件,并且不会自动安装到本地 Maven 存储库中.

The local Maven repository (and Gradle's install task) should only be used when exchanging artifacts with Maven builds. It's not meant to be used for exchanging artifacts between projects of a Gradle build, and installing into the local Maven repository won't happen automatically.

相反,merger 需要在其他两个项目上声明 project dependencies.例如:

Instead, merger needs to declare project dependencies on the other two projects. For example:

configurations {
     merge
}

dependencies {
    merge project(":frontend"), project(":backend")
}

task merge(type: Zip) {
    from { configurations.merge.collect { zipTree(it) } }
}

这假设 frontendbackend 正确地声明了它们的工件.(这可能会自动发生,例如如果使用了 war 插件.)

This assumes that frontend and backend correctly declare their artifacts. (This may happen automatically, for example if the war plugin is used.)

您可以在 Gradle 用户指南 中找到更多相关信息,尤其是多项目构建章节.

You'll find much more on this in the Gradle User Guide, in particular the multi-project builds chapter.

这篇关于Gradle 项目取决于兄弟项目创建的工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆