简体   繁体   中英

wrong configuration of maven war plugin

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

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>
  </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>

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