9

I have a server module which depends on a web modules:

<dependency>
    <groupId>org.ema.server</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>war</type>
</dependency>

which is getting re-packaged with the spring-boot-maven-plugin in order to get a standalone:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <fork>true</fork>
        <mainClass>org.ema.server.Server</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Everything is working so far. The web module is getting included into the final jar as BOOT-INF/lib/web-0.0.1-SNAPSHOT.war.

I know Spring allows one to load additional war files for Tomcat at runtime but it appears that such a war file must either be located somewhere in the file system or I am doing it wrong.

The question is how I can keep the standalone feeling but still be able to add war files into my final standalone jar and deploy them to the built-in Tomcat?

Below is the code that shows how one could potentially do this but I am not sure if it can be done the way I require it:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {

    LOGGER.info("Adding web app");

    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {

            // Ignore that since it's not working anyway.
            String resourcePath = getClass().getResource("/../lib/web-0.0.1-SNAPSHOT.war").getPath().toString();

            try {
                tomcat.addWebapp("/web", resourcePath);
            } catch (ServletException ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}
Community
  • 1
  • 1
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • You could take a look at [this](https://spring.io/guides/gs/consuming-rest-angularjs/) guide – geoand Feb 06 '17 at 06:40
  • @geoand This does not really have anything to do with my issue but thanks. – Stefan Falk Feb 06 '17 at 07:35
  • Could you explain in details, what do you mean by "depends on war" - it either uses them as web-services? so in this case you should better think about "depends on other web app", and here docker or any other container-based deployment comes for rescue, and don't reinvent the wheel. (docker compose, and a tiny network configuration - and you are on a safe side of microservice-like world, what was spring-boot intended to be used for) If use them as base library (i.e. classes or resources) - than you can use them as pure jar, but properly configure your resource resolver in main spring app. – Ilya Dyoshin Feb 08 '17 at 05:45
  • @IlyaDyoshin Well, with "depends on war" I basically meant it depends on yet another web application. Imagine I have a REST API there but more than one web clients (for whatever reason). I am looking for a simple standalone such that a user would only have to download my final jar file and run everything out of the box. The only solution I was able to do this was to *copy* the web application war file with a Maven plugin into the final jar and let the Java application (the standalone) extract that from the resources, copy it to the file system and *then* call addWebapp from Tomcat. – Stefan Falk Feb 08 '17 at 08:25
  • @IlyaDyoshin .. but this solution is not very appealing because now I cannot run the Java application in Eclipse just as it is it seems. – Stefan Falk Feb 08 '17 at 08:26
  • I don't get your question. You say you don't want the file to be located in the file system, yet in your example code you're doing just that. From where do you want the deployment to come? – eis Feb 14 '17 at 21:36
  • 1
    If from disk is ok, you should see the updated code at [this question](http://stackoverflow.com/q/31374726/365237) – eis Feb 15 '17 at 06:45

0 Answers0