How to package REST client code via gradle for multi-project use

Background:

In a multi-project (such as Alpha and Beta) Java / Spring4.x environment where Project Alpha needs to use the REST endpoint in the Project beta. The Alpha and Beta ARE projects are very related and will always be released together. However, they also need to be separate as the code will be embedded in different parts that will be executed on different physical machines - hence the REST implementation in the first place!

Little side point: UI code resides in other projects and usually consumes various REST endpoints and therefore does not require Java implementations of the REST client. REST clients are usually src / main / test, so they are not even packaged, generally they are only created for Unit tests.

Features:

This API is defined in Project Beta through an interface class:

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

      

The Java REST client is also in the beta project (requires API above):

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

      

Question:

, ! settings.gradle - . build.gradle. , REST , Project Alpha? gradle, Eclipse. , Project Alpha build.gradle:

dependencies {
    compile project('beta')
}

      

doesn't work as it results in the whole Beta version of the project. By putting the Java client code in src / main / client, I assumed the Project Beta would need to define a sourceSet to handle this code. How to do this, package it in a Jar (I know how to do this part), AND MOST IMPORTANT , reference the Jar from Project Alpha all in the same multi-gradle build? Or is there a much better way to do this in a completely different approach?

0


source to share


1 answer


I think I just found the answer - I didn't know that when defining a project dependency, you can refine it through including a specific config!

So, I think I can do something like this in Alpha (where Beta defines the config it refers to):

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

      

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

UPDATE

The above approach works great if that's what you want. I needed something different. Project Beta offered the REST client that Project Alpha needed. First, I packed 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/**'
}

      



}

Then there were two options in Project Alpha. First, use the client class files directly from the beta project:

jar {

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

      

Or another option in Alpha is to link to the generated Jar file:

dependencies {
    . . .

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

      

YMMV, but I usually prefer the latter approach.

0


source







All Articles