如何通过gradle打包REST客户端代码以用于多项目 [英] How to package REST Client code via gradle for multi-project usage

查看:95
本文介绍了如何通过gradle打包REST客户端代码以用于多项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

在多项目(例如Alpha和Beta)中的Java/Spring4.x环境中,Project Alpha需要在Project Beta中使用REST端点. Alpha和Beta项目非常相关,并且将始终一起发布.但是,它们也需要分开,因为代码将被内置到不同的部分中,这些部分将在不同的物理机器上执行-因此首先实现REST实现!

In a multi-project (say Alpha and Beta) Java/Spring4.x environment where Project Alpha needs to use a REST endpoint in Project Beta. Projects Alpha and Beta ARE very related and will always be released together. However, they also need to be separate as the code will be built into different pieces that will execute on different physical machines - hence the REST implementation in the first place!

轻微的问题:UI代码在其他项目中,并且通常是各种REST端点的使用者,因此不需要使用REST Client的Java实现. REST客户端通常是src/main/test,因此它们甚至都没有打包,通常只为单元测试创​​建.

Slight side-point: The UI code is in other projects and is usually the consumer of the various REST endpoints and thus does not need to consume Java implementations of a REST Client. The REST Clients are usually src/main/test so they are not even packaged, generally they are only created for Unit Tests.

详细信息:

给定的API通过接口类在Project Beta中定义:

The given API is defined in Project Beta via an Interface class:

// located in /src/main/java/com/mycompany/myservice/api
public interface MyServiceApi {
    String getDataMethod();
}

Java REST客户端当前也处于Project Beta中(需要上面的API):

The Java REST Client is also currently in Project Beta (needs the API above):

// currently located in /src/main/client/com/mycompany/myservice/client
public class MyServiceRestClient implements MyServiceApi {
    . . . 
}

问题:

很抱歉,到这里已经花了很长时间!这两个项目都在相同的settings.gradle文件中定义-它们是一起构建的.每个项目也都有自己的build.gradle文件.问题是如何仅打包客户端REST工件,然后如何在Project Alpha中引用它们?理想情况下,答案既可以在gradle命令行中使用,也可以在Eclipse中使用.显然,在Project Alpha的build.gradle中使用它:

Sorry it has taken so long to get here! Both projects are defined in the same settings.gradle file - they are built together. Each project also has its own build.gradle file. The question is how to package only the client REST artifacts and then how to refer to them in Project Alpha? Ideally, the answer will work both from the gradle command line as well as within Eclipse. Clearly, using this in Project Alpha's build.gradle:

dependencies {
    compile project('beta')
}

不起作用,因为它会引入所有项目Beta.通过将客户端Java代码放入 src/main/client 中,我一直认为Project Beta将需要定义一个SourceSet来处理此代码.我该怎么做,将其包装到Jar中(我知道该如何做),最重要,同时引用Project Alpha中的Jar进行相同的单个多项目gradle构建?还是有一个更好的方法完全是另一种方法呢?

does not work as that brings in ALL of project Beta. By putting the client Java code into src/main/client, I have been assuming Project Beta will need to define a sourceSet to process this code. How do I take that, package it into a Jar (I know how to do this part), AND MOST IMPORTANTLY, reference that Jar from Project Alpha all in the same, single multi-project gradle build? Or is there a much better way to do that is a different approach entirely?

推荐答案

我想我刚刚找到了答案-我不知道在定义项目"依赖项时,可以通过包含特定配置来进行优化!

I think I just found the answer - I did not know that when defining a "project" dependency you can refine via inclusion of a specific configuration!

所以我想我可以在Alpha中做这样的事情(其中Beta定义了引用的配置):

So I think I can do something like this in Alpha (where Beta defines the referenced configuration):

dependencies {
    compile project('Beta') {
        configuration = 'RESTclient'
    }
}

我现在正在对此进行测试,如果可以正常运行,则会对其进行编辑.

I am testing this out now and will edit this answer if it works correctly.

更新

如果您需要的是上述方法,则可以正常工作.我需要一些不同的东西. Project Beta提供了Project Alpha所需的REST客户端.首先,我将其打包到Project beta中的客户端" jar文件中:

The approach described above works fine if that is what you need. I required something a bit different. Project Beta offered up a REST Client that was needed by Project Alpha. First, I packaged this into a 'client' jar file in Project beta:

clientJar {
    // ensure the client jar file is relatively self-contained, so add API class
    // Note: this is additive, so the client configuration class files are already in the jar
    from sourceSets.main.output.asFileTree.matching {
        include 'com/mycompany/package/path/api/**'
}

}

然后在Alpha项目中有两个选择.首先直接从Project Beta使用客户端类文件:

Then in Project Alpha there were two choices. First use the client class files directly from Project Beta:

jar {

    from project(':Beta').sourceSets.client.output.classesDir
    . . .
}

或者,Alpha中的另一个选项是引用创建的Jar文件:

Or, the other option in Alpha is to refer to the created Jar file:

dependencies {
    . . .

    compile "com.mycompany.project.path:beta:${version}:client"
}

YMMV,但通常我更喜欢后一种方法.

YMMV, but generally I prefer the latter approach.

这篇关于如何通过gradle打包REST客户端代码以用于多项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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