从单个源文件夹中 Gradle 多个 jars [英] Gradle multiple jars from single source folder

查看:23
本文介绍了从单个源文件夹中 Gradle 多个 jars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至于现在,我们有一个名为 src 的单个源文件夹的项目结构,其中包含三个模块的源代码.我想做的是:

As for now we have a project structure with single source folder named src, which contains source code for three modules. What I want to do is:

1) 编译源代码.这可以通过 sourceSets 定义轻松完成:

1) Compile source code. This is easily done with sourceSets definition:

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

2) 将编译结果放入三个jar中.我通过三个jar"类型的任务来做到这一点:

2) Put compilation results into three jars. I am doing this via three 'jar' type tasks:

我现在通过三个独立的任务来做到这一点:

I am doing this now via three separate tasks:

  • util.jar

  • util.jar

task utilJar(type: Jar) {
    from(sourceSets.main.output) {
        include "my/util/package/**"
    }
}

  • client.jar

  • client.jar

    task clientJar(type: Jar) {
        from(sourceSets.main.output) {
            include "my/client/package/**"
        }
    }
    

  • server.jar

  • server.jar

    task serverJar(type: Jar) {
        from(sourceSets.main.output) {
            include "**"
        }
        excludes.addAll(utilJar.includes)
        excludes.addAll(clientJar.includes)
    }
    

  • 问题是server.jar 应该包含所有未包含在client.jarutil.jar 中的类.在 ant 构建脚本中,我们通过使用 difference ant 任务来解决这个问题.这如何在 gradle 中完成(我目前的方法不起作用)?

    The thing is that server.jar should contain all classes that are not contained within client.jar and util.jar. In ant build script we solve this problem by using difference ant task. How this can be done in gradle (my current approach doesn't work)?

    也许我的方法是完全错误的.请指教.

    Maybe my approach is completely wrong. Please advice.

    附言至于现在我们不能改变项目源代码文件夹结构.

    P.S. as for now we CAN NOT change the project source code folder structure.

    推荐答案

    我将在这里发布我的工作解决方案作为答案(我在 gradle 的论坛上得到了提示).

    I will post my working solution here as an answer (I've got a hint on gradle's forum).

    gradle 中的作用域非常奇怪:) 我认为每个任务定义都会创建某个Task"类的对象,在这种特殊情况下类似于JarTask".然后我可以从我的 build.gradle 脚本中的任何地方访问该类的任何属性.但是,我找到了唯一可以看到模式的地方,这些模式包含在 jar 文件中 - 在任务的 from 块内.所以我现在的工作解决方案是:

    The scopes in gradle are very strange thing :) I thought that every task definition creates an object of some 'Task' class, which is something like 'JarTask' in this particular case. Then I can access any property of the class from anywhere in my build.gradle script. However, I found the only place where I can see the patterns, which are included in jar file - inside a from block of a task. So my working solution for now is to:

    1) 定义一个项目级集合以包含要从 server.jar

    1) Define a project-level collection to contain patterns to be excluded from server.jar

    2) 排除 serverJar 任务的 from 块中的所有模式.

    2) Exclude all patterns in from block of serverJar task.

    请看下面的最终版本

    sourceSets {  
        main {  
            java {  
                srcDir 'src'  
            }  
        }  
    } 
    
    // holds classes included into client.jar and util.jar, so they are to be excluded from server.jar
    ext.serverExcludes = []
    
    // util.jar
    task utilJar(type: Jar) {  
        from(sourceSets.main.output) {  
            include "my/util/package/**" 
            project.ext.serverExcludes.addAll(includes)
        }  
    }
    
    // client.jar
    task clientJar(type: Jar) {  
        from(sourceSets.main.output) {  
            include "my/client/package/**"
            project.ext.serverExcludes.addAll(includes)
        }  
    }
    
    // server.jar
    task serverJar(type: Jar) {  
        from(sourceSets.main.output) {  
            exclude project.ext.serverExcludes
        }  
    }
    

    这篇关于从单个源文件夹中 Gradle 多个 jars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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