Using custom Java project layouts?

(Chris) #1

Hi all,
I’m trying to port a project from ant to gradle with very little success. I’m completely new to gradle and I’m probably confusing all the concepts of how to work with this tool. I have some complete newbie questions as I haven’t managed to figure this out by reading the documentation or playing around with the syntax…
The project is a quite large java project that has been around for 15 years and uses a different layout than the layout that gradle expects and that’s where I’m going wrong.

I have 3 source paths “src”, “test” and “usercode” where the first two are self-explanatory. The 3rd is a place where developers put code to build into src but kept separate for project-internal reasons. src and usercode builds to build/classes and test to build/testclasses. “usercode” should not be compiled by default.

So I want to have compilation targets as follows (names don’t matter, but I’m using the names we have in ant):

  • runcompile: “internal” target that sets compiler settings etc and all other compile targets uses antcall to call this one after setting properties for which source set and output dir it should work with
  • compile-src: builds src by calling runcompile
  • compile-test: builds test by calling runcompile
  • compile: depends on compile-src and compile-test
  • compile-usercode: depends on compile-src and builds usercode by calling runcompile

And I have no idea how to accomplish this with gradle. I’m trying to use java-library plugin and do something like this:

sourceSets {
main {
java {
srcDirs ‘src’
}
}
test {
java {
srcDirs ‘test’
}
}
usercode {
java{
srcDirs ‘usercode’
}
}
}

and I’ve set the output directories using these lines:

sourceSets.main.output.classesDir = file(“build/classes”)
sourceSets.test.output.classesDir = file(“build/testclasses”)
sourceSets.usercode.output.classesDir = file(“build/classes”)

I’ve edited a compileJava target that sets the compiler options and it seems that gradle has implicitly created a compileUsercodeJava and there seems to be a compileTestJava, but I have no idea what I’m doing and even more what I should be doing.

So my questions:

  • What is the compileJava target supposed to work on? Is that supposed to build all the sourceSets or is that intended for src-only? If the former, then what target is to be used for building only src?
  • As I don’t want any target that builds “usercode” other than a specific one for that, how do I accomplish that the implicit targets exclude that from the build. That is, how do I avoid that there’s a “compile all in source sets” targets that users can accidentally call?
  • Now that I have the compiler options inside compileJava, will the other compilation-related targets also use that (as in the “antcall” in our current build). If not, how do I set the compilation options in only one place and still let them apply to all compilation targets?
  • Eclipse with buildship does not seem to accept my settings of the output directories even though the commandline build does. When I add the gradle nature to the project, it immediately starts putting all the output in bin/ and not build/, which is a large problem since we’re using bin/ for runnable scripts where users should add that to their $PATH. Am I setting the wrong output parameters or is buildship hardcoded for bin/ as compiler output root directory?

TIA,
Chris

0 Likes