Gradle: How to create a library from only part of a project? - java

I have a spring boot project with server functionality. In the same project I wrote a client that provides an abstract way of communicating with the server. The client makes it much easier to make the necessary HTTP requests without needing to be concerned with URLS, response codes, etc.
I would like for the client code to live in the server project, and export this as a library that I can add as a gradle dependency in other projects.
What is the best way to do this? Can I configure gradle to export a certain package as a separate library, e.g. com.example.client.* or do I need a second build.gradle and re-structure the project?

See the documentation for
Multi Project Builds
Publishing Artifacts

Related

How do I divide my code into two applications?

I have a few java classes, holding my application's logic. Now I want to create two applications, one will be a SpringBoot web application and another one will be a desktop application. Both of them will use logic, that I already wrote.
I see only one way to do it: compiling logic classes into a .jar file and then putting it as a library into two Idea projects. But if I update any of this code, I will have to recompile it into jar and then replace an old jar with a new one in both projects.
Is there another, more simple way two divide my code into two projects?
What should I do with Git/GitHub?
P.S.
My project is in java, I use Intellij IDEA, GitHub.
Multi-module project
As commented, you need a multi-module project. The content for each module is compiled separately, producing separate JAR files.
IntelliJ supports multi-module projects.
I recommend using a Java build management tool to assist. Apache Maven is one such tool that supports multiple modules.
The word “module” used here in this context should not be confused for the Java Platform Module System. You may or may not choose to use Java modules in your project modules’ code.
I suggest rest web service interface to export the logic data..
SpringBoot web application can consume the rest services
and the other a desktop application can consume the rest services
ONE REST SERVICE LAYER
--------------------------------------
| |
Desktop app, SpringBoot web application
this way to don't duplicate your code and bugs twice.

Android client and java server in the same git project

I am developing a simple Android application and it should communicate with server using REST api. Both client and server use kotlin and serialize classes to json with Jackson and API looks like:
class xRequest { .. }
class xResponse { .. }
Client and server have their own git repositories and I use Android Studio and Idea to work with them separately. This leads to class declaration duplication as they both need to know API.
What's the best way to get rid of duplication? I could move API to some third project and then build - publish - add dependency on it but that's a lot of work during development.
Is it a good idea to move them to the single project so it will look the way:
my project
api
android-client
server
Thanks
It's a classic duplication problem. What we usually do in this case?
Extract duplicated code to the separate entity and reuse it.
So, I would introduce a new pure java project called core and add it as a dependency to android-client and server.

Java - Spring Boot project used with many contextpaths

I have a Spring Boot Project called SuperProject and I want to use this project with differents contexts paths.
For example: paths 'baseUrl/client1', and 'baseUrl/client2', and 'baseUrl/clientX' will use the same logic and use cases defined on SuperProject.
All of this clients will use different database url connections, but the tables will be equals, so the client project will work.
So basically I should to only change the .properties/.yml file for each client.
However I can't have repeated code, because if I have to change any logic/use case, all of this clients will need to change too.
I think that i have to develop a .war library and share it by maven into all "client projects". But I don't know if the webservices url's, defined on SuperProject, will be mapped when i compile the projects.
How can I do that? Is this idea the best approach?
For example, you can use "assembly" plugin for maven with different environments. Eventually, maven will create packages for each client.

Clientside GWT and Maven curious Setup

I want to setup GWT in a special mode. I only use GWT for the client side as a replacement for having to deal with JavaScript directly.
The idea is to produce a single JS file.
Since this is also part of a bigger project with multiple project pages I got a problem where to put the output of the compiler and how to setup.
The ideal setup would be placing the GWT stuff into a single project and incooperate the ouput in a different project. The question is how to do it?
Update:
The current plan is to compile the JavaScript out of GWT using a simple Java application just issuing the GWT compile command and taking the Eclipse auto-build classes as necessary input. After the sources are compiled to java script the application copies the js files (one for every supported browser) to the related destination. This way the once created js files stay static and other developers do not have to deal with GWT related build tasks and we just avoid a necessary maven fight to get things working on build.
Also the GWT project can now depend on the web project making it possible to start the web application and alter its behaviour by adding support to host mode debugging.
Does anyone know a working example?
The easiest way, if you build a WAR in the end, is to put the output of GWT in a WAR too that you can use an an overlay in the final WAR module.
Other Maven plugins could do the trick too (dependency:unpack, maven-shade-plugin, etc.)
See https://github.com/tbroyer/gwt-maven-archetypes for examples.

GWT/Gradle project examples.

I need advice on how to structure a multi-tier GWT/Spring project so that Gradle can build the artifacts and deploy the correct jars..
Google hasn’t helped much – I can find a number of articles on building multi-projects and indeed building GWT project in Gradle however, all of these seem incomplete for my problem domain as I’m finding the following problems as I have encountered the following issues.
In the multi-project examples, the GWT dependencies are being included in the web-application from the war plug-in.
If I go down the single gradle build route then I’m losing decoupling with the projects..
Both the client & Server have dependencies on certain class files (for GWT-RPC); currently these are packaged in the client project so has resulted, again, in a server dependency on the client (for the GWT-RPC DTO objects).. This leads me to feel I need a third module exclusively for the shared class files with the source being also present in the gwt-client project (for the GWT compiler to pick these up)..
So; the question is has anyone came across a multi-tier GWT examples that uses Gradle as the build tool & deals with some/all of the above issues?
Thanks in advance,
Ian.
We're using a single build, but we address point #2 - "coupling of projects" using the Classcycle maven dependency plugin.
Ultimately, you want three genres of code: server, client and shared. The advantage of packaging those separately in separate jars (as you said in point #3) is that your server jar size will be decreased, and you could use more liberal source directories in your .gwt.xml file.
If you decide to use a single jar/war, then you will be including the extra, unused client classes on the server. This could lead to runtime exceptions from code leakage and (potentially?) worse performance on the server. We avoid the runtime exceptions by enforcing the layering separation at build time (using Classcycle), and the extra performance overhead from the extra client classes should be marginal. You can always strip out the client code from the jar after compile, using a post-build task.
Sorry, I don't know much about gradle, but I figured I would try to help anyways.

Categories