0

This is a basic gradle question but I could not find an answer: If I have multiple independent projects that share the same root directory, how should I use different build.gradle file for them?

for example the directory structure is like this:

src/main/java/package1/base1/project1package/

src/main/java/package1/base1/project2package/

It looks like the build.gradle file should be placed at root, but how to differ two projects which are totally independent and not related to each other? When I use gradle build command, how can I specify one project to be built? Thanks.

Opal
  • 81,889
  • 28
  • 189
  • 210
rdnnz
  • 1
  • These seems not to be a different projects but separate packages. Different project would have different paths and file tree in general. – Opal Sep 24 '14 at 05:57
  • You are right. What I am trying to do is to see how different projects co-exist and share the same root structure, and build with gradle separately. – rdnnz Sep 24 '14 at 14:45
  • So it seems that You should separate common codebase to a third project and then set dependency of the mentioned projects to the third one. – Opal Sep 24 '14 at 14:46

2 Answers2

0

To be honest I don't like the structure you are proposing. The standard one looks like this:

    <root>/build.gradle

    <root>/<subproject1>/build.gradle
    <root>/<subproject1>/src/main/java/<basepackage>/<subproject1>/

    <root>/<subproject2>/build.gradle
    <root>/<subproject2>/src/main/java/<basepackage>/<subproject2>/

The subproject1 and subproject2 are independent of each other. Moreover:

  • <root>/build.gradle can contain a build configuration, tasks and so on common to all sub-projects. subproject1-specific stuff is then in <root>/<subproject1>/build.gradle
  • If you execute gradle build in <root>/, all sub-projects will be built. If you do this in <root>/<subproject1>/, only the subproject1 will be built.

For more, see http://www.gradle.org/docs/current/userguide/multi_project_builds.html

Tomas
  • 71
  • 4
  • Thanks for the help. I gave some explanation why I organized the structure in this way. It is good practice to take the way you mentioned, and also it's good to know a flexible solution (-b to specify the file name). Thanks again. – rdnnz Sep 24 '14 at 03:40
0

Let me know if I am off base, but essentially what I think you want to do is simply produce two separate JARs from two sets of source that happen to live in the same root directory. This is a pretty funny way to structure your project, as @Tomas mentioned, but assuming you cannot change your project structure, you could simply create custom source sets to solve this problem.

Your build could look something like this.

apply plugin: 'java-base'

sourceSets {
    projA {
        java {
            srcDir 'src/main/java'
            include 'pkg1/**'
        }
    }
    projB {
        java {
            srcDir 'src/main/java'
            include 'pkg2/**'
        }
    }
}

task projAJar(type: Jar) {
    baseName 'projA'
    from sourceSets.projA.output
}

task projBJar(type: Jar) {
    baseName 'projB'
    from sourceSets.projB.output
}

Although this is technically one Gradle project, these two source sets each have their own compile tasks, jar tasks, and configurations. You would configure your dependencies like so.

dependencies {
    projACompile 'foo.org:lib:1.0'
    projBCompile 'bar.org:lib:2.0'
}
Mark Vieira
  • 13,198
  • 4
  • 46
  • 39
  • This is a very formal answer. Thanks a lot. My situation is a bit weird. Basically I want to create some example projects as my code snippets in a very close way under the same directory structure, so I can easily browse them. I take the trick to use -b option to choose different build config file for different projects. So these build config files can be placed under the same directory but to be used independently. I agree this is not the way to organize product code structure, but just a convenient way for code snippets combined together. – rdnnz Sep 24 '14 at 03:38