ppawel
ppawel

Reputation: 1056

Spring Boot application with part of JSP files from other Maven module

Let's assume that we have Spring Boot based web application using JSP templates. It can be even as simple as in the following example (from official Spring Projects repository)

Project structure:

|-src/main/java/
|    |-sample.tomcat.jsp
|        |-SampleTomcatJspApplication.java
|        |-WelcomeController.java
|-src/main/resources/
|    |-application.properties
|-src/test/java/
|    |-...
|-src/main/webapp/
|    |-WEB-INF
|        |-jsp
|            |-welcome.jsp
|-pom.xml

Properties file contains view prefix /WEB-INF/jsp/ and suffix .jsp and when requesting / we see properly rendered content of welcome.jsp.

WelcomeController.java

application.properties

Changes

Now let's make the following changes

  1. Duplicate WelcomeController.java as WelcomeController2.java and change a bit request mapping, model attributes and returned view name, e.g.:

    @RequestMapping("/2")
    public String welcome2(Map<String, Object> model) {
        model.put("message", "Hi from Welcome2");
        return "welcome2";
    }
    
  2. Duplicate welcome.jsp as welcome2.jsp so that src/main/webapp will be like this:

    |-src/main/java/
    |    |-sample.tomcat.jsp
    |        |-SampleTomcatJspApplication.java
    |        |-WelcomeController.java
    |        |-WelcomeController2.java
    ...
    |-src/main/webapp/
    |    |-WEB-INF
    |        |-jsp
    |            |-welcome.jsp
    |            |-welcome2.jsp
    

Then when requesting /2 we can see properly rendered content of welcome2.jsp.

The question

What is the way of splitting such project into two maven projects, so that both WelcomeController2.java and welcome2.jsp could be moved to other project (maven dependency) and still be successfully resolved when /2 URL is requested?

Note that with Spring Boot web-fragment.xml (that could be placed in META-INF directory of dependency) is ignored.

Upvotes: 3

Views: 1017

Answers (1)

jonathan.cone
jonathan.cone

Reputation: 6706

Unfortunately, I don't know of an easy way to do this but one approach I've used is to create a Maven artifact just like normal for the main project, in your case probably a WAR artifact. This project will need to have a dependency upon your second project. Then your second project would consist of two components:

  1. A standard Maven JAR artifact containing the compiled class files.
  2. A Maven assembly ZIP consisting of the JSP files that need to be included in the WAR archive as well. This will be generated from the second project during the package phase, but will need to be included as a separate dependency on the main project using a zip classifier.

When the first project is built, you'll need to unpack the assembly dependency as part of the packaging process for the WAR archive. If you want this to work in an IDE, you'll probably need to unpack it in a fairly early phase, such as process-resources or generate-sources.

Upvotes: 2

Related Questions