Creating a library jar using Spring boot
Asked Answered
X

3

6

I created a spring boot application having REST web services and jpa dependencies. The application runs on its own as a standalone application. I'm trying to add UI layer using vaadin as a separate project that uses the services from the sring boot project. Is there an easy way to make the spring boot application as a library jar that can be included in other projects.

I searched the forum and found some threads that advised not using spring boot but instead using the spring framework to create the library. Just wanted to check if there are any examples how this can be done in Spring boot.

Xanthin answered 23/2, 2016 at 22:22 Comment(1)
This depends on how you plan on packaging & deploying them, as well as using the library from the Vaadin UI, plain old Java method calls or REST calls. I guess they suggest using just Spring because when using Boot the resulting artifact will include all of its dependencies. Either way, I think the simplest way would be to use maven, gradle or any other such build system which allows you to package your artifact in multiple ways and has support for distributed and versioned dependencies. Eg: maven project with 2 modules, 1 for the library and 1 for the UI which references the library.Tracheostomy
R
1

If your intention was to let Vaadin call the REST API you've created from the browser (as is usually the case with client-side frameworks like AngularJS), then you're misunderstanding Vaadin. A Vaadin application runs server-side.

So what you can do is run two servers, one running the Vaadin application and which calls the second one running your REST API. But if there's no need for this split, you can use the classes that form the REST API as a regular Java API called directly from the Vaadin application code.

Rattlebrain answered 27/2, 2016 at 21:37 Comment(0)
L
1

This project of mine may be of some interest to you. I have used Spring-Boot to make a library to be used in other projects.

The main thing to note here is to have:

@SpringBootApplication(scanBasePackages = {"me.ramswaroop.jbot", "example.jbot"})

in the main class where you start the spring-boot application. See this main class to learn more. But to be honest, using Spring-Boot to make a library to be included in other projects isn't a good choice according to me. If I were to rewrite JBot then I wouldn't have used Spring-Boot this way surely.

Spring-Boot is really good to create a stand-alone application that you can "just run" but to create a library, hmm, not sure. I think a library should have fewer dependencies as possible.

Lum answered 17/1, 2017 at 16:43 Comment(0)
P
0

Like Morphic said, you need to decide whether you're going to call the library methods natively using Java or whether you're setting up a web service. It sounds like you're building a REST API, in which case I wouldn't bother including it as a library. I would simply run your spring boot application (java -jar myservice.jar, or mvn spring:boot run), then just connect to it using HTTP REST on whatever port Spring Boot opens for your service.

If you do decide to load the Spring Boot app as a library jar then you probably don't need Spring boot at all. All you need is your service methods and Spring config packaged together as a jar, mvn install to your local repo, then just reference your jar in the vaadin project's pom file (all this assuming you're creating it as a maven project).

Prophecy answered 24/2, 2016 at 4:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.