Spring Boot war or jar for rest api project

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Spring Boot war or jar for rest api project



I want to develop sample REST API project using Spring Boot. I am confused what should be the proper approach as we have multiple options for packaging like war, jar etc.


war


jar



I have requirement where I have external library folder which have multiple jar and resource files which will be used in REST API and front end (using React).


jar



I want to keep jars and resource as external dependencies due to their dynamic changes and I do not want to include them in project. I have tried sample project using loader.path using jar which works fine but same approach doesn't working with war file. I am using Maven as build tool.


loader.path


jar


war


war


jar


lib




4 Answers
4



Whether to choose jar or war depends upon whether you want a standalone executable application or you want to deploy your project on servers like Weblogic. Suppose if my application is a middle layer or an adaptor(helper application) of a complex project I would deploy it on WebLogic as war.



In your case My suggestion for you is to use a JAR instead of WAR. To build jar use mvn clean install command.



In order to load external properties file all you need to do is pass folder names and property names as part of command line arguments as shown below:


java -jar myapp.jar --spring.config.name=application,myapp
-- spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config



In order to externally import Resources, you can use


Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");



code snippet


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class CustomResourceLoader implements ResourceLoaderAware


private ResourceLoader resourceLoader;

public void setResourceLoader(ResourceLoader resourceLoader)
this.resourceLoader = resourceLoader;


public void showResourceData() throws IOException

//This line will be changed for all versions of other examples
Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");

InputStream in = banner.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

while (true)
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);

reader.close();




And applicationContext.xml file entry for this file is as below:


<bean id="customResourceLoader" class="com.howtodoinjava.demo.CustomResourceLoader"></bean>



appendix-





Thanks Shubham for your answer, this seems to be working for me
– Karim Narsindani
Aug 13 at 14:04



You should make it an executable Spring Boot JAR.



You only need a WAR if you have to deploy it on a Java EE server.



It's good that you're using Maven. Have it manage your dependencies and build the package.



You want to find the Maven plug-in that creates the executable JAR with dependencies included inside.



Update:



Here are my responses to your four questions:


mvn install



I'd recommend that you consider deploying the REST service separately and let the React front end call it. De-couple the two. Let the REST service be a microservice that stands on its own, without a UI.





Thanks for your answer, I will try link for working example and update you
– Karim Narsindani
Aug 9 at 14:21



You could create an executable JAR file with dependencies using Apache Maven Assembly Plugin.


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>$mainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>



Adding it to your pom.xml within <build></build> elements allows you to build both jar and jar-with-dependencies packages.


pom.xml


<build></build>


jar


jar-with-dependencies



To build package use mvn clean package command.


mvn clean package





I will try and update you. Thanks
– Karim Narsindani
Aug 8 at 22:18





I have tried your sample code but it creates jar with dependencies and I am not looking for this, I want to create Spring-Boot jar without dependency and all the dependency should be copied in lib folder. I found option in maven which allow to create jar without dependency and copies all the required jar in lib folder
– Karim Narsindani
Aug 9 at 14:13






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard