Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 908
ScreamingMage Avatar asked Aug 18 '11 17:08

ScreamingMage


1 Answers

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.

like image 121
ScreamingMage Avatar answered Sep 22 '22 21:09

ScreamingMage