2

Novice Gradle query.Consider following structure of a project

Project
   |
    -component_1

   |
    -component_2

I want to produce one jar from every component source, which resides under src directory. Please note that this is not a multi project scenario but sort of multicomponent. How can I do that in Gradle (1.6)

Update:

A single build file is required, instead of each component having separate build

anergy
  • 1,374
  • 2
  • 13
  • 29
  • the basic approach is to add things via {{project.artifact}} and potentially represent the components as named sourcesets but I'd say a good answer is going to be hard to write without some extra context. e.g. why don't you want to use multi project? what do you intend to do with the jars, publish them somewhere? are they related or independent? are they plain java source code or something else? – Matt Jun 17 '13 at 10:37
  • Thanks Q: Why not multi-project? A: existing code, cant change structure Q: What to do with jar? A: pack them into single archives, but possibly later independently publish them Q: Related? A: some of them are related. – anergy Jun 17 '13 at 10:48

1 Answers1

10

You mention that some of the components depend on each other, so I am also wondering why you would not want to implement this as a multi-project build. For now, let's assume all of your components are independent Gradle projects. Given that they produce a JAR file, it seems to be a given that they are Java projects. I'd create a build.gradle file for each project.

.
└── your-project
    └── component1
    │   ├── build.gradle
    │   └── src
    └── component2
        ├── build.gradle
        └── src

For each of these projects, we will apply the Java plugin. The only difference to the standard conventions introduced by this plugin is that your source files sit in the directory src instead of src/main/java. To configure this, all you need to put into your build.gradle files is the following content:

apply plugin: 'java'

sourceSets {
   main {
      java {
         srcDirs = ['src']
      }
   }
}

If you execute gradle build for any of your projects, Gradle will compile your production and test source code, run your unit tests and assemble the JAR file under the directory build/libs.

EDIT:

You can either implement this the way Matt explains in his comment or go for a multi-project build approach. I put together the following code examples to give you the idea. Create two files on the root level of your project: build.gradle and settings.gradle.

.
└── your-project
    ├── build.gradle
    ├── settings.gradle
    ├── component1
    │   └── src
    └── component2
        └── src

In the settings.gradle file include the project that are part of your build like this:

include 'component1', 'component2'

Then in your build.gradle file you only need to define your project definition once by putting it into a subprojects configuration block. This means it applies to all subprojects of your build. With this approach you can define common behavior without having to repeat the code.

subprojects {
   apply plugin: 'java'

   sourceSets {
      main {
         java {
            srcDirs = ['src']
         }
      }
   }
}
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
  • +1 for the answer, but I would prefer to keep single build file outside components. – anergy Jun 19 '13 at 09:00
  • 3
    you can still do that by including a build.gradle in the root dir that configures each component build in a `project(':component1') { }` block, i.e. take the build.gradle benjamin has described and paste it into a project block like I have written. If you don't describe them as separate projects then you will end up replacing the concept of a project with a sourceset and things will just get more complicated all round for no obvious benefit. – Matt Jun 19 '13 at 09:41