一个项目中带有多个罐子的Gradle平面项目 [英] Gradle flat project with multiple Jars in one project

查看:125
本文介绍了一个项目中带有多个罐子的Gradle平面项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 项目
|
-component_1

|
-component_2

我想从每个组件源产生一个jar,它驻留在src目录。请注意,这不是一个多项目场景,而是多分量场景。我如何在Gradle(1.6)中做到这一点

更新:



单个构建文件是必需的,而不是每个组件都有单独的构建

解决方案

你提到一些组件相互依赖,所以我也想知道为什么你不想把它作为一个多项目构建来实现。现在,让我们假设你的所有组件都是独立的Gradle项目。鉴于他们产生了一个JAR文件,它似乎是一个给定的Java项目。我会为每个项目创建一个 build.gradle 文件。

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

对于这些项目中的每一个,我们将应用Java插件。与这个插件引入的标准约定唯一的区别在于你的源文件位于目录 src 而不是 src / main / java 。要配置此功能,只需将 build.gradle 文件放入以下内容即可:

  apply plugin:'java'

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






如果你执行 gradle为您的任何项目构建,Gradle将编译您的生产和测试源代码,运行您的单元测试并将JAR文件组装到目录 build / libs

您可以按照马特的方式实现这一点在他的评论中解释或寻求多项目构建方法。我把下面的代码示例放在一起给你看。在项目的根级别创建两个文件: build.gradle settings.gradle

 
└──你的项目
├──build.gradle
├──settings.gradle
├──component1
│└──src
└──component2
└──src

settings.gradle 文件包含这样的构建项目:

  include'component1 ','component2'

然后在 build.gradle 文件,只需将其放入子项目配置块中即可定义项目定义。这意味着它适用于您构建的所有子项目。使用这种方法,您可以定义常见行为而不必重复代码。

 子项目{
apply plugin:'java '

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


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

这篇关于一个项目中带有多个罐子的Gradle平面项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆