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

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

问题描述

我有这个Gradle设置有四个项目,一个有三个孩子的父母,其中一个Java Servlet JSON'后端'内置到一个war文件中,然后一个静态的HTML5'前端'消耗这个内置的zip。这两个安装他们的工件到本地的maven repo。



第三个兄弟项目的合并取决于这两个工件,构建一个合并战争只是简单地将它们压缩在一起。



但是,一旦我按照预期运行,我显然不得不测试引导现在我突然得到Artifactno.company:frontend:1.0-SNAPSHOT@zip',而不是在当地的repo中删除这些工件。



是否不可能依赖当前版本生成 的工件?



编辑:



根据另一个想法(和Peter反应这个Maven逻辑的回复),这个版本看起来有前途的,没有遍历Maven(注意:它的工作原理!):

  // ##从frontend的build.gradle:
任务zipFrontend(dependsOn:'buildFrontend',type:Zip){
from('dist /')
}

// ##从后端的build.gradle:
应用插件:'war'

// ##从合并的build.gradle:
任务mergeFrontAndBack(dependsOn :[':backend:war',
':frontend:zipFrontend'],type:War){
from zipTree(project(':frontend')。tasks ['zipFrontend']。archivePath)
from zipTree(project(':backend')。tasks ['war']。archivePath)
destinationDir(buildDir)
}
pre>

编辑2:



根据彼得对未达成兄弟姐妹的评论'项目结构和具体建议,这里是结果(注意:它的作品!):

  // ##从前端build.gradle:
任务zipFrontend(dependsOn:'buildFrontend',键入:Zip){
from('dist /')
}
配置{zip}
工件{zip zipFrontend}

// ##从后端的build.gradle:
应用插件:'war
配置{jsonOnlyWar}
工件{jsonOnlyWar war}

// ##从合并的build.gradle:
配置{merge}
依赖关系{
合并项目(路径:':backend',配置:'jsonOnlyWar')
合并项目(路径:':frontend',配置:'zip')
}
任务mergeFrontAndBack (dependsOn:configured.merge,type:War){
from {configurations.merge.collect {zipTree(it)}}
destinationDir(buildDir)
}


解决方案

本地Maven存储库(和Gradle的安装 task)只能在与Maven构建交换工件时使用。这不意味着用于在Gradle构建项目之间交换工件,并且安装到本地的Maven存储库不会自动发生。



相反,合并需要在其他两个项目上声明项目依赖关系。例如:

 配置{
合并
}

依赖关系{
合并项目(:frontend),project(:backend)
}

任务合并(类型:Zip){
from {configurations.merge。收集{zipTree(it)}}
}

这假设前端后端正确声明其工件。 (这可能会自动发生,例如,如果使用 war 插件)。



你会发现更多在 Gradle用户指南中,特别是多项目构建章节。


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.

The third sibling project 'merger' depends on these two artifacts, to build a "merged" war by simply "zipping them together".

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.

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?

Edit:

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)
}

Edit 2:

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)
}

解决方案

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.

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) } }
}

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

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

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

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