There are multiple ways a spring boot application can be packaged for deployment, let’s look at some common options. We will use a sample project for the exercise in the following steps. You can refer to the article how to initialize a spring boot web project? for information on how to create a basic spring boot project.
One of the options is to create a JAR or WAR file, which can be deployed on middleware applications like weblogic or tomcat. Right click on the pom xml file and select run as maven build option, then use the “clean install” goals to build and create the jar file.
You should see the JAR file generated in the target folder of our demo project named as demo-0.0.1-SNAPSHOT.jar. By default spring boot maven plugin will create a JAR file, in the next step we will add the “packaging” property in pom xml to generate a WAR file.
Running the same maven goal “clean install” again will create a WAR file this time as demo-0.0.1-SNAPSHOT.war
The generated JAR or WAR file can now be used to deploy on middleware applications.
Another option to deploy our application is to create an executable JAR file which can be used to run a java application in a command line environment.
We will add a property in the pom xml file to specify the main class as entry point for our application.
Run maven build again and once the JAR is created, then use the “java -jar *.jar” command to run our demo app, example shown below.
Another popular option to package a java application is to build a container image, there are multiple ways we can achieve this, let’s look at few examples here.
Before we can build a docker image, we need to install docker on our system. Please refer to the getting started page https://www.docker.com/get-started and follow on screen instructions to install docker. Once installed you should see the docker process running using “docker info” command.
First option is through the pom xml file, right click on the pom file and select run as -> maven build… option and then use “spring-boot:build-image” maven goal to trigger the docker image build process. Once complete we should see the image name built and pushed to our local registry.
We can verify the newly created image using “docker images” command as shown below.
Another way to build a container image is using Dockerfile, let’s create one and use it to create a docker image.
Now use the docker CLI to build the image, go to the project folder and run “docker build” command. Make sure you are in the project folder where we created the Dockerfile.
We can again verify the image using “docker images” command as shown below.
Now that we saw two ways to build a docker image, now let’s try to run a container from the docker image and test our app. For this we can use “docker run” command, the application port used inside the container should be exposed outside the container using -p option.
We can also use -d option to run containers in background or in detached mode. Once successfully started, we can use docker ps -a command to check running containers and status. A quick curl command confirms that our app is running as expected.
The container images created in the last few steps now exist in our local repository, and can be pushed to a remote public or private container repository for further deployment to application servers or cloud based solutions.
We will use a docker hub registry account for this example, using the docker push command we can push the images to the docker hub repository. You can sign up on the docker hub site and create a private repository, then use the “docker login” command on your local system to connect to the remote docker repository and then use the “docker push” command to push an image from local to remote repository.
Through this post, we understood how to package a spring boot based java application. If you have any questions or feedback, please share in the comments section.