How to bundle war file containing rest code with jax-rs dependencies so that it runs on Tomcat? - gradle

I'm using a Gradle based-setup to build a REST web service (with Jax-Rs and glassfish.jersey dependencies). I'm using the Gradle-tomcat plugin (bmuschko-gradle-tomcat-plugin) to deploy it on tomcat during runtime. I'm pretty new to this, and I've read that tomcat doesn't support Jax-Rs features on its own. So how do I make my REST code work on tomcat? Should I bundle Jax-rs/jersey jars along with my war? I won't be able to copy-paste those jars anywhere (few tutorials have the jars pasted under web-inf/libs module), I want it to be automated during the build with Gradle.
PS: I'm using #ApplicationPath (mandated by my company) in my code, which overrides servlet-mapping in the web.xml, so I won't be able to modify that.

Related

Spring Boot Maven plugin: What does it actually do?

Can someone give me an understanding of what the Spring Boot Maven plugin actually does? I have been Googling, but most of what I find doesn't give a clear picture.
The impression I have so far is that it can create a "fully executable" jar that does not need to be run via java -jar, and that it's also possible to make a more traditional jar that you would run via java -jar. I'm sure there are other variations of what it can produce as well.
I'm also under the impression that it can package up dependencies and resources. It's not at all clear to me how the resources are "accessed" by the application when it's run.
In either of the outcomes described above do I need just the jar and nothing else (i.e. no resource files, dependency jars, etc.)? In other words, is the jar self-contained? When I've opened the jar up, it does seem that everything it needs is there. Is that really the case?
Now, let's go a little further towards what I'm trying to do. I am writing a set of Spring services with REST APIs. Each service will be run in its own VM (or container - future). The services are packaged into a single jar and the service to be used is selected via Spring profile (i.e. spring.profiles.active=a-profile).
The way I've done things like this before has been to use the Maven assembly plugin to produce an archive (zip) for each separate service and inlcude all of the necessities (dependency jars, resource files, etc.). I'd place it where needed, unpack it, tweak some configuration and run it via an included script.
I'm getting the impression that's not "how it's done" when the Spring Boot Maven plugin is involved.
The Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”.
It builds the uber jar which bundles in Tomcat along with your app. If you inspect the contents of the jar with jar -tf <file_name> you will see that the format is a bit different. The Spring Boot classes look normal, but then your project's files are inside a BOOT-INF folder.

Spring Boot Migration Issue on Packaging JAR and WAR using Maven

We are trying to migrate our existing Spring MVC applications to Spring Boot application. Our existing applications are using 3.2.9, so tons of XML configurations. All the beans are defined in the XML files. What we have done is first we have upgraded our existing applications to Spring 4.2.5 version since Spring Boot will work only with Spring 4 versions.
Our requirement is to have both FAT JARs and WAR files from the build. Most of our existing customers would prefer Application Server deployment, so we have to create WAR file for them. Also for our internal testing and new deployments, we are planning to use FAT JARs.
How can we achieve them in the Maven file, we are able to provide separately as below. Is there any maven plug-in to generate both in single build?
<packaging>jar</packaging>
or
<packaging>war</packaging>
We are publishing our artifacts into Nexus repository. So, we want to have the separate repository location for JAR files and WAR files. Can we do that using the single pom.xml file?
Also another question, we have all the XML configurations under WEB-INF folder. When we are moving to the Spring Boot application, it has to be under the resources folder. How can we handle them easily. When we build FAT jars, the resources are not looked under WEB-INF because it simply ignores the webapp project.
I am looking forward for some guidance to complete the migration. Infact, we have already done that changes and it is working fine, but we are confused on this WAR / JAR generations.
Update:
I have got another question in mind, if we are converting our existing applications to spring boot, do we still have to maintain WEB-INF folder in the web-app or can move everything to the resources folder?. While building the WAR file, whether spring boot takes care of moving the resources to WEB-INF? How spring boot would manage to create the WAR file if you are putting all the resources under the resources folder.
Building WAR and FAT JAR is very easy with Gradle.
With Maven, I would try multi module setup, where one sub-module will build fat JAR and second will build WAR file.
Application logic can be as third sub-module and thus being standalone JAR with Spring configuration and beans. This application logic JAR would be as dependency for fat JAR and WAR module.
WAR specific configuration can be placed in Maven WAR sub-module.
I didn't have such requirement before, so don't know what challenges may occur. For sure I wouldn't try to integrate maven-assembly-plugin or other packaging plugins with spring-boot-maven-plugin.
Concerning location of config files, just place them into src/main/resources or it's sub-folders according Spring Boot conventions. Spring Boot is opinionated framework and will be most friendly to you if you don't try to resist defaults.
Maven does not handle this gracefully, but its far from difficult to solve. You can do this with 3 pom files. One parent pom that contains the majority of the configuration, and one each for the packaging portion of the work. This would neatly separate the concerns of the two different assembly patterns too.
To clarify -- I'm not recommending a multi-module configuration here, just multiple poms with names like war-pom.xml and fat-jar-pom.xml, along with parent-pom.xml.

Deploying a 'JAR' file instead of 'WAR' file for Spring-MVC and Maven

I am working on a Spring-MVC application which uses Maven. In the POM.xml I noticed that I can denote the file-type in which I can select if I want to deploy the project as a JAR or WAR.
Mostly I select a WAR file and then deploy it in Apache tomcat. My question is, If the application is Spring-MVC based, with Spring-Security, Hibernate and other libraries, can I package it as JAR by simply denoting it in POM.xml and deploy it in Apache webserver instead of using Apache tomcat? Or do I need to make some modifications somewhere for this to work. Kindly let me know. Thank you.
No, you can't. Apache httpd knows nothing about how to handle jar files.
What you can do though is to provided an embedded webserver (such as jetty) in your package and define in your MANIFEST.MF file a main class that will launch it and register your application to it.
That way you can package is a an auto-executable jar, or as a war that can be run on his own or deployed in a classical webserver.

Deploying a third party war in a Spring Boot embedded container

Pardon if this feels a bit of "necroposting". I looked and found only one similar question with no answers here (Spring-Boot Embedded Wars).
I have a service packaged into a spring boot (1.0) container. This service uses activiti (www.activiti.org) to manage some buisiness processes. I am trying to deploy inside the same spring boot container, the war for activiti-explorer. This war has its own web.inf, spring config, et cetera, so it may conflict with the existing spring config, but nonetheless, I'd like to try to deploy that war as it is.
I haven't found any way to do that, and suspect that spring boot doesn't support the deployment of pre-package wars into the embedded container, isn't it?
Just as a warning, I think I can't put the extracted war into the spring-boot jar as I feel it needs a fully functional web container. If spring-boot doesn't offer this functionality, no big deal, we're going to deploy that war on its own tomcat, but it would be handy if it could be.
Thanks
Update
Just to clear better, I have an already running Spring Application standalone server, with its own embedded Tomcat.
Inside the embedded Tomcat I plugged some #Controllers I developed.
Then I was also able to map a third-party servlet using a ServletRegistrationBean (mapped to /servlet-path).
Now I'd like to do something similar with another war that contains a full fledged web application (it's a vaadin/spring 3.2 application with its own libraries, jsps, static resources ...) and would like to map it to (say) /war-path.
I would like to drop the war in a well known location and deploy it into tomcar with a (say) WarRegistrationBean that would let Tomcat handle all the classloading hurdles (as I mentioned, the war is using spring 3.2 while I'm using 4.0 with spring boot, ...).
I suspect that this last feature is not supported by spring-boot or - possibly - even out of scope for the project itself.
You can manually enhance a war archive by adding the stuff that the boot plugin does (classes from the loader and some META-INF information). Easiest would be to simply enhance an "empty" war, and then merge it with the target one (by exploding them both and re-jarring). The only thing you'd need to add might be a main class.
It's still a gap in the Boot tooling. If you think it needs filling please raise an issue and/or send some code.

Example using Jersey (REST resource), OSGi (Apache Felix), and Maven (build and deploy)?

I seem to be really struggling here. What I want to do (in this order) is:
1) Build a RESTful resource using a Jersey application and resource w/annotations (this is not the issue).
2) Package, install, and start that bundle into the Felix OSGi container, as an HTTP service, including dependencies.
3) Package, install, and start a WAR in the OSGi container that may incorporate #2 as a dependency.
And I would like to be able to do all of this using Maven.
I cannot seem to find a working example of even the individual steps, especially involving Maven, that work let alone the combination of those steps. I have tried cobbling together various q&a from across the web with varying levels of success but not an end-to-end working example yet.
Any pointers would be appreciated...
I do not have an example for Jersey but I have a tutorial for CXF with Apache Karaf (which uses Felix). It shows how to create a Rest service and build it with maven. Using Apache Karaf you can then deploy the bundle directly from the maven repo. Moving this to Jersey probably just means to exchange the lib and use another blueprint config to initialize the rest service.
Apache Karaf also allows to deploy wars and wabs but I have not yet tested them.
Apache Stanbol does most or all of this (not sure if the war packaging is included out of the box) to implement its RESTful services.
You'll have to dig through its codebase but searching for Jax-RS annotations in there should point you to the right places.
I also am really struggling with exactly what you are attempting to do. So far, I seem to be really close but alas not quite there, here's what I've been doing:
creating a War with Maven that defines my Jersey Resource's
bundling it with maven bundle plugin ( see section Adding OSGi metadata to existing projects without changing the packaging type ). Which allows me to run the restlets in Tomcat and test.
define a target in Eclipse that includes resources from my locally defined p2 site which I create with the p2-maven-plugin plugin. In this way I can gather up any of the dependencies from the WAR project into a p2 site, which I can deploy to an Eclipse defined target
Where I am stuck is trying to register the Jersey Resource's as services, for which I've tried:
using the JAX-RS OSGI connector, for which I eventually gave up on because it uses glassfish jersey which seems to export a version 2 API of Jersey when Jersey hasn't even defined a version 2 API yet. This caused package resolution problems when I wanted to use version 1.17 of Jersey libs.
registering the Resources using a Whiteboard a la Apache Felix HTTP Service, my current approach which doesn't seem to work yet.
And, finally, if the preceding doesn't work I'll try Amdatu
Another route I might try is from the Jersey project OSGI chapter
I tried this combination and made it up and running -
1- Modularity Specification-->OSGi specification
2- OSGi implementation-->Apache Felix 4.4.0
3- OSGi Runtime-->Apache Karaf 3.0.3
4- Software Architecture Specification of REST – JAX-RS
5- JAX-RX implementation-->Apache CXF – 2.7.5
You can refer this nice tutorial - http://java.dzone.com/articles/building-cxf-rest-service-osgi
Now, I am also planning to move to Jersey from CXF as the Jersey is the light wait reference implementation of JAX-RS. Now I am planning to use Jersey with Apache Felix/Apache Karaf. You can install Jersey in Karaf and deploy your Jersey Rest Web Services as Bundle. You can refer this tutorial - https://vzurczak.wordpress.com/2014/09/30/web-applications-with-osgi-working-with-jersey/
Here's a good example on github : https://github.com/ddragosd/jax-rs-on-karaf

Resources