Gradle多模块项目设置 [英] Gradle Multi-Module Project Setup

查看:628
本文介绍了Gradle多模块项目设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Gradle并替换现有的基于Maven的项目。过去,我在使用Maven处理多模块构建时遇到了很多问题。 Gradle在处理多模块组件时一直呼吸新鲜空气,但它还不完美。



我的项目有以下文件夹布局:

   - 项目
---- EnterpriseApp1
------ EarProject
--- ----- build.gradle
------ EjbProject
-------- build.gradle
------ WarProject
- ------- build.gradle
------ properties.gradle
------ build.gradle
---- CommonLib
- ----- build.gradle
---- ClientApplication
------ build.gradle

我遇到的问题是EnterpriseApp1和ClientApplication都依赖于CommonLib项目。我不知道如何配置我的EnterpriseApp1构建文件以使用CommonLib项目作为EjbProject的依赖项。我已经接近完成这项工作,但尚未完成工作。我已经成功复制了EnterpriseApp1中的CommonLib文件夹,但这不是一个长期的解决方案。



这是我在EnterpriseApp1中的当前properties.gradle文件: / b>

 包括EarProject,EjbProject,WarProject
includeFlatCommonLib

根据Gradle文档,settings.gradle文件中的includeFlat命令将包括与文件夹位于同一杠杆的项目其中settings.gradle文件是(期望的行为)。

EnterpriseApp1 / build.gradle文件:

 子项目{
apply plugin:'java'

sourceCompatibility = 1.6
group ='org.example'
version ='1.0-SNAPSHOT'

仓库{
mavenCentral()
...
}

依赖项{

}
}
code>

EnterpriseApp1 / EjbProject / build.gradle:

  apply plugin:'java'

sourceCompatibility = 1.6

存储库{
mavenCentral()
...
}

依赖关系{
编译项目(':CommonLib')

编译组:'org.restlet.jee',名称:'org.restlet',版本:'2.0.11'
编译组:'ma.glasnost.orika',名称:'orika-core',版本:'1.0'
...
编译组:' javax.jmdns',name:'jmdns',version:'3.4.1'
}

当我从EnterpriseApp1文件夹执行gradle clean build时,所有依赖项都按预期下载并且项目开始编译(包括CommonLib项目),但EjbProject项目在构建过程中失败,因为它缺少CommonLib jar参考。 Gradle不够聪明(或者我完全不知道;))将我的EjbProject配置为使用从CommonLib项目构建阶段生成的Jar。



我为漫长而复杂的设置。我一直在试图解决这个问题一段时间,但几乎没有想法。我真的很感谢任何对社区的帮助。



谢谢!

解决方案

您选择的目录布局已经暗示了一个很好的解决方案。我建议有三个单独的构建: EnterpriseApp1 CommonLib ClientApplication 。我会将 CommonLib 发布到Maven或Ivy存储库,以便其他两个版本可以从此处使用它。对于本地开发,您可以将 CommonLib 发布到本地Maven回购(最简单)或基于文件的常春藤回购。


I've recently started using Gradle and replacing my existing Maven-based projects. I have had many issues in the past with handling multi-module builds with Maven. Gradle has been a breath of fresh air when handling multi-module buils, but it's not perfect yet.

I have the following folder layout for my projects:

-- Projects
---- EnterpriseApp1
------ EarProject
-------- build.gradle
------ EjbProject
-------- build.gradle
------ WarProject
-------- build.gradle
------ properties.gradle
------ build.gradle
---- CommonLib
------ build.gradle
---- ClientApplication
------ build.gradle

The problem I am having is that the "EnterpriseApp1" and "ClientApplication" both depend on the CommonLib project. I don't know how to configure my "EnterpriseApp1" build file to use the CommonLib project as dependency for the "EjbProject". I have come very close to getting this to work, but not quite working yet. I have had success by copying the CommonLib folder inside "EnterpriseApp1", but that's not a long term solution.

Here's my current properties.gradle file in "EnterpriseApp1":

include "EarProject", "EjbProject", "WarProject"
includeFlat "CommonLib"

According to the Gradle documentation the "includeFlat" command in the "settings.gradle" file will include projects in the same lever as the folder where the "settings.gradle" file is (desired behavior).

EnterpriseApp1/build.gradle file:

subprojects {
   apply plugin: 'java'

   sourceCompatibility  = 1.6
   group = 'org.example'
   version = '1.0-SNAPSHOT'

   repositories {
      mavenCentral()
      ...
   }

   dependencies {

   }    
}

EnterpriseApp1/EjbProject/build.gradle:

apply plugin: 'java'

sourceCompatibility  = 1.6

repositories {
   mavenCentral()
   ...
}

dependencies {
   compile project(':CommonLib')

   compile group: 'org.restlet.jee', name: 'org.restlet', version: '2.0.11'
   compile group: 'ma.glasnost.orika', name: 'orika-core', version: '1.0'
   ...
   compile group: 'javax.jmdns', name: 'jmdns', version: '3.4.1'
}

When I execute "gradle clean build" from the EnterpriseApp1 folder all the dependencies are downloaded as expected and the projects begin to compile (including the CommonLib project), but the EjbProject project fails during the build due to the fact it's missing the CommonLib jar reference. Gradle is not smart enough (or I'm completely clueless ;)) to configure my EjbProject to use the Jar generated from the CommonLib project build stage.

I apologize for the long and complicated setup. I have been working on trying to figure this out for some time now, but have nearly ran out of ideas. I would really appreciate any help for the community.

Thanks!

解决方案

The directory layout that you've chosen already hints at a good solution. I suggest to have three separate builds: EnterpriseApp1, CommonLib, and ClientApplication. I'd publish CommonLib to a Maven or Ivy repository so that the other two builds can consume it from there. For local development you can publish CommonLib to the local Maven repo (easiest) or a file-based Ivy repo.

这篇关于Gradle多模块项目设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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