Home > Mobile >  wrong configuration of maven war plugin
wrong configuration of maven war plugin

Time:07-26

I'm a basic maven user and I'm trying to copy the resources folder into the war file created by maven. I need to do this in order to be able to import css files into my jsp files. These are the relevant parts of my configuration files:

servlet.xml

<mvc:resources location="/resources" mapping="/resources/**"></mvc:resources>

pom.xml

<plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
        <web-resources>
        <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.css</include>
        </includes>
        </resource>
        </web-resources>
        <outputDirectory>../../tomcat/webapps</outputDirectory> 
        </configuration>
      </plugin>
    </plugins>

jsp file:

<header>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css">
</header>

resources structure:

  • resources
    -- css
    style.css

I've spotted that the css folder is actually copied into the classes folder, but even changing the import path into the jsp file I'm not able to correctly load the css file. To launch the install I simply issue: mvn clean install

CodePudding user response:

The default configuration of the war plugin will copy your resources into the root of your WAR. I recommend just leaving the plugin without any additional configuration, and adjusting your mapping.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.3.2</version>
    <configuration>
      <webResources>
        <resource>
          <targetPath>/css/</targetPath>
          <filtering>true</filtering>
          <directory>src/main/resources/css</directory>
        </resource>
      </webResources>
      <outputDirectory>${project.build.directory}/deployments</outputDirectory>
    </configuration>
  </plugin>

This will put your css folder into /css in your war, then you just configure your mapping as such.

<mvc:resources location="/css" mapping="/resources/css"></mvc:resources>
  • Related