0

QUESTION

In a Spring Boot Java application built with Maven using the spring-boot-maven-plugin with WAR packaging through Eclipse IDE with M2 with the goal as clean package, I would like to know if there is a command which would allow me to copy the resource folders (css, images, etc) which currently end up in mywar.war/WEB-INF/classes/static/ into the root directory mywar.war, so that I have mywar.war/images/, etc?

I see that with other plugins, there is something like a targetPath, but I can't find anything like that in the documentation for spring-boot-maven-plugin.

EXAMPLE POM

<?xml version="1.0" encoding="UTF-8"?>
<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>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.21.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <groupId>com.myapp</groupId>
    <artifactId>MyApp</artifactId>
    <version>1.0.0</version>
    <name>${project.artifactId}</name>
    <description>My Application</description>

    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.test.skip>true</maven.test.skip>
        <tomcat.version>7.0.78</tomcat.version>
        <servlet-api.version>3.0.1</servlet-api.version>
        <thymeleaf.version>3.0.3.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>

        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provided</scope>
        </dependency>

         <dependency>
            <groupId>test</groupId>
            <artifactId>x</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/MyJar.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Miss Kitty
  • 162
  • 1
  • 3
  • 16
  • 1
    Why do you want to do that? – Simon Martinelli May 28 '19 at 14:39
  • @SimonMartinelli Because those running the server I am going to deploy it to generally don't want to change their settings if ever possible, and their existing settings have the http server set up to allow proxypass for the /images/, /css/, etc folders in the root directory instead of in /WEB-INF/classes/static/images/, etc. Business as usual. :) – Miss Kitty May 28 '19 at 14:41
  • 1
    But what web framework are you using? – Simon Martinelli May 28 '19 at 14:42
  • 1
    And in which directory do you have these files? – Simon Martinelli May 28 '19 at 14:44
  • @SimonMartinelli This is a ``Spring Boot`` application in ``Java`` using ``Thymeleaf`` and running as a ``WAR`` on an ``Apache Tomcat`` server behind an ``Apache HTTP Server``. I have all of my resources in the project under ``/src/main/resources/static``, and they include ``/src/main/resources/static/images``, ``/src/main/resources/static/css``, etc. – Miss Kitty May 28 '19 at 14:50
  • 1
    But then everything will work as expected. The url will be ://css etc. – Simon Martinelli May 28 '19 at 14:56
  • 1
    The static directory is automatically mapped by Spring Boot as a subdirectory of the war, or if it's a standalone Spring Boot App, as the root. – Compass May 28 '19 at 14:59
  • 1
    Apart from that I strongly recommend to upgrade as soon as possible to Spring Boot 2.X cause Spring Boot 1.X will be EOL at August... – khmarbaise May 28 '19 at 15:23
  • @khmarbaise Yeah, I agree - but unfortunately this version was another requirement. I actually first built the app with Spring Boot 2.x, only to find out that it could not run because the ``Tomcat Apache Server`` was too old to support it, and I had to regress the application to this back-level. – Miss Kitty May 28 '19 at 15:40

2 Answers2

1

So, I ended up getting around this by moving all of my resources from the /src/main/resources/static directory into a /src/main/webapp directory. Then everything appeared right under the root of the resulting WAR file, and the Apache HTTP Server picked them up and allowed them through.

Thanks for your insight, everyone!

Miss Kitty
  • 162
  • 1
  • 3
  • 16
0

Everything will work as expected. Spring Boot will server your static content.

The urls will be

<host>:<port>/<warname>/css 
<host>:<port>/<warname>/images

etc.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • Hello - This is true, however, this does not resolve that the ``Apache HTTP Server`` needs to allow access to the files at ``mywar.war/WEB-INF/classes/static/``, etc for this to work without a 404 denied error on the client-side. My issue is that I need the files to be located at ``mywar.war/images`` to fulfill requirements. – Miss Kitty May 28 '19 at 15:39
  • No why do you think so? Do you use mod _jk or mod_proxy – Simon Martinelli May 28 '19 at 19:57