简体   繁体   English

使用Gradle构建多Java项目

[英]Building multi java project using Gradle

I'm tearing my hair out over this. 我正在撕裂我的头发。 I've stripped my scripts right back to code provided in the Gradle tutorial pages as I think I'm either doing something fundamentally wrong or have misunderstood how a multi project application is supposed to be structured using gradle. 我已经将我的脚本剥离回Gradle教程页面中提供的代码,因为我认为我要么做了一些根本错误的事情,要么误解了如何使用gradle构建多项目应用程序。

I've got three java projects within eclipse, with all three containing a build.gradle script and only one containing a settings.gradle script. 我在eclipse中有三个java项目,所有三个都包含build.gradle脚本,只有一个包含settings.gradle脚本。 Structure is as follows: 结构如下:

Scripts
-- build.gradle
-- settings.gradle
Database
-- build.gradle
Services
-- build.gradle

I am attempting to build the 'Database' and 'Services' project using the build script in the 'Scripts' project. 我正在尝试使用“脚本”项目中的构建脚本构建“数据库”和“服务”项目。 To create the project tree, I have the following code in settings.gradle: 要创建项目树,我在settings.gradle中有以下代码:

include 'Services', 'Database'

project (':Services').projectDir = new File(settingsDir, "../Services")
project (':Database').projectDir = new File(settingsDir, "../Database")

Replicating the code in the multi-project tutorial (gradle docs), I'm trying to get each of the build scripts to print out some text, to ensure everything is set up correctly. 复制多项目教程中的代码(gradle docs),我试图让每个构建脚本打印出一些文本,以确保所有内容都正确设置。 My end goal is to have the dependencies correctly built when 'eclipseClasspath' is executed, so that all projects correctly compile within eclipse. 我的最终目标是在执行'eclipseClasspath'时正确构建依赖项,以便所有项目在eclipse中正确编译。 However, the text is not being printed out as I expected! 但是,正如我所料,文本没有打印出来!

Below are what is contained within the three build scripts: 以下是三个构建脚本中包含的内容:

Scripts build.gradle 脚本build.gradle

allprojects {
    task hello << { task -> println "I'm $task.project.name" }
}

subprojects {
    hello << {println "- I depend on Scripts"}
}

Database build.gradle 数据库build.gradle

hello.doLast { 
    println "- I'm inside database" 
}

Services build.gradle 服务build.gradle

hello.doLast { 
    println "- I'm inside services" 
}

Within the 'Scripts' project, when I run 'gradle -q hello', I get the following results: 在'Scripts'项目中,当我运行'gradle -q hello'时,我得到以下结果:

I'm Scripts
I'm AnprDatabase
- I depend on Scripts
I'm Database
- I depend on Scripts

Why is the text '- I'm inside database' and '-I'm inside services' not appearing? 为什么文本' - 我在数据库内'和'我在服务内'没有出现? I'm a bit baffled by it. 我有点困惑。 I can only surmise it has something to do with my project structure. 我只能猜测它与我的项目结构有关。 Can someone confirm this is the case? 有人可以证实这是事实吗? If not, what is the problem? 如果没有,问题是什么? As mentioned, I've stripped my scripts back to this simple example, as I could not get the dependencies to run within each projects build script using the same structure. 如上所述,我已经将我的脚本剥离回这个简单的示例,因为我无法使用相同的结构在每个项目构建脚本中运行依赖项。

Greatly appreciate any help offered. 非常感谢任何提供的帮助。

I've managed to figure out why it's not working. 我已经设法找出它为什么不起作用。 It was a silly error on my part. 这对我来说是一个愚蠢的错误。 The 'Database' and 'Services' project were not in the same directory as my 'Scripts' project. “数据库”和“服务”项目与我的“脚本”项目不在同一目录中。 I had checked these projects into a git repository, which copies the local directories out of my Eclipse workspace and over to the local git repository. 我已经将这些项目检查到git存储库中,该存储库将本地目录从Eclipse工作空间复制到本地git存储库。 I hadn't checked the 'Scripts' project into my git repository so it was still residing in my Eclipse workspace. 我没有将'Scripts'项目检查到我的git存储库中,因此它仍然存在于我的Eclipse工作区中。 Seems very obvious now as to why it didn't work! 现在似乎非常明显,为什么它不起作用! Checking my 'Scripts' project in and changing the path to the other projects in settings.gradle has fixed the problem. 检查我的'Scripts'项目并更改settings.gradle中其他项目的路径已修复此问题。

It's interesting that rather than throwing a FileNotFoundException or something similar, Gradle will create an empty project if it cannot find the one defined. 有趣的是,如果找不到定义的项目,Gradle将创建一个空项目,而不是抛出FileNotFoundException或类似的东西。 This was something I haven't appreciated. 这是我没有意识到的。 At first I thought this was strange behaviour, but I suppose it gives the user the power to create his projects on the fly at script runtime. 起初我认为这是一种奇怪的行为,但我认为它使用户能够在脚本运行时动态创建项目。 Not sure if I like that though! 不确定我是否喜欢它!

So, always make sure your navigating to the correct directory in your settings when including projects! 因此,在包含项目时,请始终确保导航到设置中的正确目录! Otherwise, you may fall foul as I did. 否则,你可能会像我一样犯规。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM