skip to Main Content

I would like to ask about the experience of a production run of a Spring Boot web application, with embeded Tomcat, based on Maven or Gradle. So far I have been working with a vanilla Spring application, deployed on Tomcat, without dependencies manager, with all external jars collected in /WEB-INF/lib/.

I have a typical web application, which is primarily a multi-user web service, and which changes very often. I often change something in views (JSP files in my case), in Java classes, and in Javascript or CSS files.

I don’t want to generate a new .jar file with each update, upload it to the server, and restart the application. I would like to work on the unpacked files and replace only the ones that have changed.
I can run the application in "debug" mode so that simple changes to classes will automatically load. And with changes to the external libraries (dependencies) used in my app, of course I can "reload" the application, but I’d like this to happen as quickly as possible and with the least impact on users.

How do I organize the process of updating the application on the server?

I assume that on the server I can execute, for example, an application update from SVN or GIT repository, and I can execute a Maven or Gradle script there. I would like the update to be as fast as possible, for example:

  1. "git pull" of Java sources, views and static files
  2. update external libraries from dependencies, eventually download new jar files and remove unused ones.
  3. compile new classes on server
  4. only if necessary – reload the application

Or maybe for such needs, the model of a Spring Boot application deployed on Tomcat, rather than with Tomcat embedding, is better? What are your experiences?

And the second question: in the application deployed on Tomcat, I used the convenient function of reloading the application with Tomcat Manager. This is a very fast operation, much faster than restarting the whole application. In addition, it allows you to serializate and restore all sessions.
Is such a "reload" possible in Spring Boot with embeded Tomcat?

Update

It looks like, Spring Boot Actuator is the answer to questions related to app reloading.

As for Maven:
I have read many tutorials on how to use Maven with Spring Boot.
However, they all assume that the apllication is packaged to jar or war. I work in the Eclipse IDE, and of course the IDE doesn’t need to package the app and serves "unpacked" from the file system.
I have not been able to run the application through a Maven script in the same way.

Can you provide an example of a Maven or Gradle configuration file that:

  1. updates the dependecy list and dependecies jars
  2. compiles java classes
  3. starts or restarts the application

I can’t deal with this last point.
Eclipse on Windows fetches the dependecy to the folder "C:Users<user>.m2repository" and is able to pass the dependency list to javaw.exe via the "-classpath" parameter, for example:

javaw.exe --classpath C:Users<user>.m2repositoryorgspringframeworkbootspring-boot-starter-web3.2.5spring-boot-starter-web-3.2.5.jar;C:Users<user>.m2repositoryorgspringframeworkbootspring-boot-starter3.2.5spring-boot-starter-3.2.5.jar;C:Users<user>.m2repositoryorgspringframeworkbootspring-boot-autoconfigure3.2.5spring-boot-autoconfigure-3.2.5.jar;C:Users<user>.m2repositoryorgyamlsnakeyaml2.2snakeyaml-2.2.jar;C:Users<user>.m2repositoryorgspringframeworkbootspring-boot-starter-json3.2.5spring-boot-starter-json-3.2.5.jar;C:Users<user>.m2repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.15.4jackson-datatype-jdk8-2.15.4.jar;C:Users<user>.m2repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.15.4jackson-datatype-jsr310-2.15.4.jar;C:Users<user>.m2repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.15.4jackson-module-parameter-names-2.15.4.jar;C:Users<user>.m2repositoryorgspringframeworkspring-web6.1.6spring-web-6.1.6.jar;C:Users<user>.m2repositoryorgspringframeworkspring-beans6.1.6spring-beans-6.1.6.jar;C:Users<user>.m2repositoryiomicrometermicrometer-observation1.12.5micrometer-observation-1.12.5.jar;C:Users<user>.m2repositoryiomicrometermicrometer-commons1.12.5micrometer-commons-1.12.5.jar;C:Users<user>.m2repositoryorgspringframeworkspring-webmvc6.1.6spring-webmvc-6.1.6.jar;C:Users<user>.m2repositoryorgspringframeworkspring-expression6.1.6spring-expression-6.1.6.jar;C:Users<user>.m2repositoryorgspringframeworkbootspring-boot-starter-log4j23.2.5spring-boot-starter-log4j2-3.2.5.jar;C:Users<user>.m2repositoryorgapachelogginglog4jlog4j-slf4j2-impl2.21.1log4j-slf4j2-impl-2.21.1.jar;C:Users<user>.m2repositoryorgapachelogginglog4jlog4j-core2.21.1log4j-core-2.21.1.jar;C:Users<user>.m2repositoryorgapachelogginglog4jlog4j-jul2.21.1log4j-jul-2.21.1.jar;C:Users<user>.m2repositoryorgapachelogginglog4jlog4j-spring-boot2.21.1log4j-spring-boot-2.21.1.jar;C:Users<user>.m2repositoryorgapachelogginglog4jlog4j-api2.21.1log4j-api-2.21.1.jar;C:Users<user>.m2repositoryorgspringframeworkbootspring-boot3.2.5spring-boot-3.2.5.jar;C:Users<user>.m2repositoryorgspringframeworkspring-core6.1.6spring-core-6.1.6.jar;C:Users<user>.m2repositoryorgspringframeworkspring-context6.1.6spring-context-6.1.6.jar;C:Users<user>.m2repositoryorgspringframeworkbootspring-boot-configuration-processor3.2.5spring-boot-configuration-processor-3.2.5.jar;C:Users<user>.m2repositoryjakartaxmlbindjakarta.xml.bind-api4.0.2jakarta.xml.bind-api-4.0.2.jar;C:Users<user>.m2repositoryjakartaactivationjakarta.activation-api2.1.3jakarta.activation-api-2.1.3.jar;C:Users<user>.m2repositoryjakartaservletjspjstljakarta.servlet.jsp.jstl-api3.0.0jakarta.servlet.jsp.jstl-api-3.0.0.jar;C:Users<user>.m2repositoryjakartaservletjakarta.servlet-api6.0.0jakarta.servlet-api-6.0.0.jar;...

Where does Maven download files on a server that runs on Linux / Debian?
How to start the Spring Boot application through Maven?

Additional question: when deploying the application on Tomcat, the dependencies should be in "WEB-INF/lib/" – how then to download all the necessary jars to this folder with Maven?

2

Answers


  1. If you are updating your Spring Boot web application on an embedded Tomcat server Here is the best way to do it smoothly:

    Dependency Management: Use Maven or Gradle. Commands like mvn clean install or gradle build make updating external libraries easy.

    Version Control: Keep your code organized with SVN or Git. This makes pulling updates from the repository a breeze.

    Incremental Updates: Instead of replacing the entire app, focus on changing only what’s necessary: Java classes, JSP files, JS, and CSS.

    Compile New Classes: After pulling updates, compile new classes on the server to stay current.

    Application Reload: For smooth updates, consider a graceful reload strategy. Tools like Spring Boot Actuator can help.

    As for reloading applications, achieving Tomcat Manager’s level of control may need some exploration with Spring Boot and embedded Tomcat.

    By focusing on efficient dependency management, version control, and graceful reloads, your updates will be seamless.

    Login or Signup to reply.
  2. For new Questions

    Maven Configuration:
    example

    `<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>my-web-application</artifactId>
        <version>1.0.0</version>
        
        <!-- Dependencies -->
        <dependencies>
            <!-- Add your dependencies here -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.5.0</version>
            </dependency>
            <!-- Other dependencies -->
        </dependencies>
        
        <!-- Plugins -->
        <build>
            <plugins>
                <!-- Maven Compiler Plugin for Java compilation -->
                <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>
                <!-- Spring Boot Maven Plugin for running and packaging -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.5.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    `
    

    To dependency Update and Compilation: you can use Maven commands like mvn clean install or mvn clean package. This will fetch the latest dependencies and compile your code.

    Application Restart:For production deployment, you typically build a WAR file and deploy it to your server. To restart the application, you can use a script or automation tool to execute commands like java -jar your-application.jar. Alternatively, you can use tools like Jenkins or Ansible for automated deployments and restarts.

    For Additional Question: you just need to configure your project dependencies in the pom.xml file. Maven handles the rest automatically during the build process. Once you build your project using Maven (mvn clean package), the WAR file generated will contain all the necessary JARs in the WEB-INF/lib/ directory.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search