Maven: how to get a war package with resources copied in WEB-INF?

前端 未结 3 962
春和景丽
春和景丽 2020-12-08 20:44

when I create a war package with maven, files and directories under the directory \"src/main/resources\" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get th

相关标签:
3条回答
  • 2020-12-08 21:26

    This configuration is working add plugin pom.xml

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>${jdk.version}</source>
            <target>${jdk.version}</target>
        </configuration>
    </plugin>
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
    
        <configuration>
    
            <webResources>
                  <!--copy resource file location-->
                <resource>
                    <directory>${project.build.directory}/classes</directory>
                </resource>
            </webResources>
            <!--location for add file-->
            <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
        </configuration>
    </plugin>
    
    0 讨论(0)
  • 2020-12-08 21:33

    Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml

    0 讨论(0)
  • 2020-12-08 21:38

    either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory. resource plugin

    EDIT:

    This configuration is working for me:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.2</version>
        <executions>
          <execution>
            <id>default-copy-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <overwrite>true</overwrite>
              <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
              <resources>
                <resource>
                  <directory>${project.basedir}/src/main/resources</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    

    you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.

    0 讨论(0)
提交回复
热议问题