简体   繁体   中英

How I can deploy existing web applications (wars) in the Spring Boot embedded server (tomcat)

I have an application which allows to dynamically generate web applications (wars) and I would like to deploy these applications in a server to test them and I think of putting them in the same embedded server of spring, here is how I solved the problem with a simple main java.

public class Main {
    private final static Logger logger = LoggerFactory.getLogger(Main.class);
    private final static File catalinaHome = new File(
            "C:\\Users\\Dev\\Desktop\\demo\\userstory-2\\compiler\\patternHost");
    private static Tomcat tomcat = null;
    public static void main(String[] args) {
        tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.setBaseDir(catalinaHome.getAbsolutePath());
        tomcat.getHost().setAutoDeploy(true);
        tomcat.getHost().setDeployOnStartup(true);
        tomcat.getServer().addLifecycleListener(new VersionLoggerListener());
        tomcat.getHost().addLifecycleListener(new HostConfig());
        try {
            tomcat.start();
        } catch (LifecycleException e) {
            logger.error("Tomcat could not be started.");
            e.printStackTrace();
        }
        logger.info("Tomcat started on " + tomcat.getHost());
        tomcat.getServer().await();
    }
}

How can I do the same with spring boot. ?

I have converted a non-spring app to spring boot in this way and it worked for me. I was able to run it with spring boot embedded tomcat. Hope this helps.

Spring boot is all about speed, it comes with embedded-tomcat server(provided you use spring-boot-starter-web dependency) and now all you need is java to run your standalone spring boot application. It reduces the manual steps of copying war file to tomcat's webapp folder and then starting it.

Try the approach which suits your app.

If your old app is spring based:

  1. Create a new spring boot starter web project and copy your old source code to this new project. Modify application.properties, resources folder, add all required dependencies in pom.xml file and change package as war .
  2. Do a mvn clean install it will generate a war file (with embedded tomcat) in target folder of your project's root directory. Now to run it all you need to do is, in your target folder open terminal and run java -jar your_warFileName.war it will start the application.

If your old app is not a spring based:

  1. Again start with new spring boot starter web project and copy your source code but then to use your old code with spring-boot, first you have to do clean-up stuff like adding @RestController to controller classes, declaring beans by marking classes with @Service or @Component and autowiring beans in the appropriate places. Once your code compiles fine then to run it your can use step 2 as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM