18.2 Building executable JAR files
Building a Spring application into an executable JAR file is rather straightforward. Assuming that you chose JAR packaging when initializing your project, then you should be able to produce an executable JAR file with the following Maven command:
$ mvnw packageAfter a successful build, the resulting JAR file will be placed into the target directory with a name and version based on the <artifactId> and <version> entries in the project’s pom.xml file (e.g., tacocloud-0.0.19-SNAPSHOT.jar).
Or, if you’re using Gradle, then this will do the trick:
$ gradlew buildFor Gradle builds, the resulting JAR will be found in the build/libs directory. The name of the JAR file will be based on the rootProject.name property in the settings .gradle file along with the version property in build.gradle.
Once you have the executable JAR file, you can run it with java -jar like this:
$ java -jar tacocloud-0.0.19-SNAPSHOT.jarThe application will run and, assuming it is a web application, start up an embedded server (Netty or Tomcat, depending on whether or not the project is a reactive web project) and start listening for requests on the configured server.port (8080 by default).
That’s great for running the application locally. But how can you deploy an executable JAR file?
That really depends on where you’ll be deploying the application. But if you are deploying to a Cloud Foundry foundation, you can push the JAR file using the cf command-line tool as follows:
$ cf push tacocloud -p target/tacocloud-0.0.19-SNAPSHOT.jarThe first argument to cf push is the name given to the application in Cloud Foundry. This name is used to reference the application in Cloud Foundry and the cf CLI, as well as used as a subdomain at which the application is hosted. For example, if the application domain for your Cloud Foundry foundation is cf.myorg.com, then the Taco Cloud application will be available at https://tacocloud.cf.myorg.com.
Another way to deploy executable JAR files is to package them in a Docker container and run them in Docker or Kubernetes. Let’s see how to do that next.
