This guide covers SpringBoot packaging configurations and deployment procedures with solutions for common issues.
Maven-Based Packaging
JAR Package Configuration
In pom.xml, specify the packaging type:
<packaging>jar</packaging>
WAR Package Configuration
For WAR deployment in external Tomcat, configure in pom.xml:
<packaging>war</packaging>
Exclude the embedded Tomcat dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Using SpringBoot Maven Plugin
Configure the SpringBoot plugin in pom.xml:
<build>
<finalName>demo-app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.app.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Using Maven Assembly Plugin
Alternative configuration using assembly plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.app.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Building the Package
Execute the following command in the project root:
mvn clean package
To skip tests during build:
mvn clean package -Dmaven.test.skip=true
External Configuration Files
SpringBoot loads application.properties from these locations (in priority order):
{current_dir}/config/{current_dir}/classpath:/config/classpath:/
To use an external logback.xml, add to application.properties:
logging.config=classpath:logback.xml
Installing Local JAR Dependencies
To manually install a local JAR to your local repository:
mvn install:install-file -Dfile=lib/custom-utils.jar -DgroupId=com.example.utils -DartifactId=custom-utils -Dversion=1.0 -Dpackaging=jar
Then add the dependency:
<dependency>
<groupId>com.example.utils</groupId>
<artifactId>custom-utils</artifactId>
<version>1.0</version>
</dependency>
Eclipse/IDEA Direct Export
Exporting as Runnable JAR
- Run the main application in your IDE
- Right-click project → Export → Java → Runnable JAR file
- Select "Package required libraries into generated JAR"
- Specify the main class and destination path
Exporting as WAR
- Right-click project → Export → Web → WAR file
- Choose destination and filename
Ant Build Script
Create build.xml for integrated build:
<?xml version="1.0" encoding="UTF-8"?>
<project name="demo-app" default="buildAll" basedir=".">
<property name="output" value="output" />
<property name="target" value="target" />
<target name="clean">
<delete dir="${target}" />
<delete dir="${output}" />
</target>
<target name="init" depends="clean">
<mkdir dir="${output}" />
</target>
<target name="mvnPackage" depends="init">
<exec executable="cmd" failonerror="true">
<arg line="/c mvn clean package" />
</exec>
</target>
<target name="copyFiles" depends="mvnPackage">
<copy todir="${output}" file="${target}/demo-app.jar"></copy>
<copy todir="${output}" file="logback.xml"></copy>
<copy todir="${output}" file="application.properties"></copy>
<copy todir="${output}" file="start.bat"></copy>
</target>
</project>
Create build.bat containing:
ant
Run build.bat to automatically package and gather all required files.
Common Build Issues
Issue: Diamond Operator Not Supported in Source 1.5
Solution 1: Add compiler properties:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Solution 2: Configure compiler plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Issue: Missing Dependency in Packaged JAR
Solution: Add repository configuration:
<repositories>
<repository>
<id>spring-milestone</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
Issue: Maven Clean Failure
Cause: Target directory files are locked by another process.
Solution: Close any applications using the files and retry.
Deploying JAR Applications
Windows Deployement
Run the application:
java -jar demo-app.jar
Or create start.bat:
@echo off
title "demo-app"
java -jar demo-app.jar
Linux Deployment
Run with nohup:
nohup java -jar demo-app.jar &
Stop the application:
kill -9 <pid>
Register as System Service
Create symbolic link:
ln -s /app/demo-app.jar /etc/init.d/demo-app
chmod +x /etc/init.d/demo-app
Control the service:
service demo-app start|stop|restart
Shell Script Management
Create manage.sh:
#!/bin/bash
APPDIR=`pwd`
PIDFILE=$APPDIR/demo-app.pid
if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
echo "demo-app is already running..."
exit 1
fi
nohup java -jar $APPDIR/demo-app.jar > /dev/null 2>&1 &
echo $! > $PIDFILE
echo "demo-app started..."
Deploying WAR Applications
Deploy the WAR file to Tomcat/webapp/ directory and start Tomcat:
- Windows: Execute
startup.batinTomcat/bin/ - Linux: Execute
startup.shinTomcat/bin/