How to Package a spring rest API as JAR - java

I have devloped a spring rest-api. packaging it as a war and deploying the same works fine. what has to be done if need to package it as jar. First of all is that correct, if yes, then how to do so. Please help.

If you are using Maven, just create another project module and move these REST-API classes to that module. The only difference between these classes and "regular" java classes is that they are annotated.
You can still throw the JAR to the classpath and set the package to the "component-scan" directive as a base-package.
You need to use this module together with the main application anyway I suppose.

Related

How to include a jar without its subjar

I have a struts project. It has many jars in its APP-INF/lib directory and the packaged ear size is huge. I also have another spring boot project and it has one utility class.
My target is to use that utility from the struts project.
The problem here is, if I include this spring-boot jar in the pom.xml, it includes the spring boot jar with all its dependency which are already present in the struts project. This makes the ear more huge. The jars are copied twice in a way. Basically the project becomes like this:
struts-project
--APP-INF/libs/
----**.jar (many jars)
----spring-boot-project.jar
--------BOOT-CONF/libs/
-------------**.jar (again many jars, most of them already in one level above directory)
My end goal is to use the utility, for which I have 2 ways (what I have in mind, more ways possible)
Include the spring boot jar, but find a way to reduce packaged ear size.
Create a rest api in the spirng boot project just to use the jar. But if the service is down anyhow, it will have huge impact on main application - so don't want to use this way also.
Please suggest more ways to achieve my end goal. Or any improvement/suggestion in the above approaches.
In order to use utility / commons classes in many project you should create separate project for utility.
Utility project should not use any dependency or very little in other case you should redesign it.

Why does maven jar project not contain it's dependencies?

I have recently been working with Spring Boot and all of the jar dependencies are packaged within the Spring Boot 'uber' jar.
I have recently made a regular jar application and none of the jars are packaged within the jar, but I am still able to execute the code within the java by calling specific classes (with main methods)
Can anyone shed some light? Any similar questions I see regarding this always speak to the shade plugin
EDIT:
I understand I didn't specify the shade plugin to create an uber jar, but could anyone explain why it's not needed for the majority of projects, and how the code is able to run if the dependencies are not there?

How do I create a Java EE container deployable JAR?

I have a main project used as a standalone batch that I want to move to the container using EJB timer (using WildFly 8.2). I thought building a WAR file with the timer class and the dependencies withing WEB-INF/lib, but doesn't sound an elegant solution because it isn't a web app, it doesn't need to bound a context. It's just a JAR, with an EJB inside and the dependencies (I'm using a fat jar) but the container throws an error when I deploy it. Should I use an EAR? Or do you guys think a WAR file is fine?
PS: I'm using Maven, so possible suggestions can take it in account.
You don't really need it in a WAR file if it's not a web application, although it doesn't harms. I would also put it in a JAR file, i don't like to add something that doesn't have a meaning...it can be confusing for others. But as #Bruno César pointed out, you need to package first the EJB(s). Have a look at this; you'll see the ways to package them as JAR, WAR, EAR.
EJB package is good solution if your standalone application has no any third party libraries. But if it has, then best solution would be EAR package type.

Spring boot application can't see beans when run as a jar

My spring boot web app works when run it in eclipse but when I try to run as an executable jar it fails to register any of the beans.
The configuration is all annotated - there are no xml config files.
I used mvn clean compile and mvn package commands to generate the jar.
Has anyone had a similar problem or have any ideas?
Thanks
Executable jars can not include nested jars, so it is necessary to workaround that limitation. A common approach is to shade the jars (i.e. unpacking all jars, and then packing in to single jar).
Spring-boot takes a different approach that relies on a custom maven repackaging plugin and handling by the Spring Boot Loader module. It generally just works, but per the documentation:
The spring-boot-starter-parent POM includes configuration
to bind the repackage goal. If you are not using the parent POM you
will need to declare this configuration yourself. See the plugin
documentation for details.
More information:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-first-application-executable-jar
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#executable-jar
My guess is that you are are you either not using the spring-boot maven plugin, or are not using the spring-boot parent POM. If that's not the issue, then you will need to post more information.
I have now resolved the problem and the app is working as a standalone jar.
Turns out the project folder structure was wrong - such that most of the app was unreachable!

using Proguard in a Maven project with several modules

I can't make Proguard Maven plugin to obfuscate files in a web project. I tried some solutions on Stackoverflow - no luck.
My actual application is Vaadin-based packaged with Maven, but I created a sample demo project https://github.com/taskadapter/projectX for now to illustrate the problem.
My end goal is creating a web application WAR file with all maven modules (e.g. "util") and web app code (e.g. "SomeImportantHiddenClass") obfuscated.
the build fails with:
[proguard] Error: The input doesn't contain any classes. Did you
specify the proper '-injars' options?
I saw http://maksim.sorokin.dk/it/2010/08/23/obfuscating-several-jars-in-one-single-maven-build-with-proguard/, but this does not make sense to me.
if I run proguard plugin on every maven module in my project separately, then how will other modules in the same project find required classes if they are all "messed up"?
I assume I need to add proguard execution to either root pom.xml or my web module, which actually packages stuff into a WAR file (see the project on GitHub).
"injar" option allows adding Jars to the obfuscation process, but I expected the plugin to find my modules and obfuscate them without me hardcoding "injar" values like "../util/target/util.jar".
plus proguard documentation does not say anything about "injars" option, it only describes "injar" configuration parameter. neither of them works in this case.
I also saw Proguard is saying it can't find any classes , but I'm not sure moving all the classes from "web" maven module to a separate one is the right solution and not sure it'll work anyway. I'll keep trying...

Categories