简体   繁体   中英

Gradle flat project with multiple Jars in one project

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

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']
         }
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM