使用 Gradle 构建多 java 项目

发布于 2024-11-30 04:50:07 字数 1481 浏览 1 评论 0原文

我为此抓狂。我已经将我的脚本直接剥离回 Gradle 教程页面中提供的代码,因为我认为我要么做了一些根本性错误的事情,要么误解了如何使用 gradle 构建多项目应用程序。

我在 eclipse 中有三个 java 项目,所有三个项目都包含 build.gradle 脚本,只有一个包含 settings.gradle 脚本。结构如下:

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

我尝试使用“脚本”项目中的构建脚本来构建“数据库”和“服务”项目。要创建项目树,我在 settings.gradle 中有以下代码:

include 'Services', 'Database'

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

复制多项目教程(gradle 文档)中的代码,我试图让每个构建脚本打印出一些文本,以确保一切设置正确。我的最终目标是在执行“eclipseClasspath”时正确构建依赖项,以便所有项目在 eclipse 中正确编译。然而,文本并没有按照我的预期打印出来!

以下是三个构建脚本中包含的内容:

脚本 build.gradle

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

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

数据库 build.gradle

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

服务 build.gradle

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

在“脚本”项目中,当我运行“gradle -q hello”时,我得到以下结果:

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

为什么文本是“-我在数据库内部”和“-我在服务内部”没有出现?我对此有点困惑。我只能推测这与我的项目结构有关。有人可以确认情况是这样吗?如果没有,问题出在哪里?如前所述,我已将脚本剥离回这个简单的示例,因为我无法使用相同的结构在每个项目构建脚本中运行依赖项。

非常感谢提供的任何帮助。

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.

I've got three java projects within eclipse, with all three containing a build.gradle script and only one containing a settings.gradle script. 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:

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. My end goal is to have the dependencies correctly built when 'eclipseClasspath' is executed, so that all projects correctly compile within 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

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

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

Database build.gradle

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

Services build.gradle

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

Within the 'Scripts' project, when I run 'gradle -q hello', I get the following results:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-12-07 04:50:07

我已经设法弄清楚为什么它不起作用。对我来说这是一个愚蠢的错误。 “数据库”和“服务”项目与我的“脚本”项目不在同一目录中。我已将这些项目签入 git 存储库,该存储库将本地目录从我的 Eclipse 工作区复制到本地 git 存储库。我没有将“Scripts”项目签入我的 git 存储库,因此它仍然驻留在我的 Eclipse 工作区中。现在看起来很明显为什么它不起作用!检查我的“脚本”项目并更改 settings.gradle 中其他项目的路径已经解决了问题。

有趣的是,如果 Gradle 找不到定义的项目,它不会抛出 FileNotFoundException 或类似的东西,而是会创建一个空项目。这是我不曾欣赏的。起初我认为这是奇怪的行为,但我认为它使用户能够在脚本运行时动态创建他的项目。但不确定我是否喜欢这样!

因此,在包含项目时,请务必确保导航到设置中的正确目录!不然你可能会像我一样犯规。

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. I hadn't checked the 'Scripts' project into my git repository so it was still residing in my Eclipse workspace. 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.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文