首页 文章

Maven:如何使用WEB-INF中复制的资源获取war包?

提问于
浏览
22

当我用maven创建war包时,目录“src / main / resources”下的文件和目录被复制到/ WEB-INF / classes而不是/ WEB-INF中 . 如何将它们复制到/ WEB-INF中?

谢谢,兰德

更新:在我的pom中我现在使用这个:

<plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->
                    <phase>war</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>myapp/target/WEB-INF</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我用以下命令启动mvn:

mvn -Dmaven.test.skip =真正的干净包资源:copy-resources

但我得到了:

[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'

[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:

<configuration>
  ...
  <outputDirectory>VALUE</outputDirectory>
</configuration>.

[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:

<configuration>
  ...
  <resources>VALUE</resources>
</configuration>.

我正在使用maven 2.2和片段基本上是相同的文档任何想法?

3 回答

  • 28

    要么配置 resources:resources 插件的 outputDirectory 参数,要么将文件放在 src/main/webapp/WEB-INF/ 目录下 . resource plugin

    EDIT:

    这个配置对我有用:

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

    您可以以 somePhase 或目标 somePlugin:someGoal 的形式运行阶段 . 阶段调用将按顺序调用间隔[validate,phase]中阶段上挂钩的所有插件目标,因此不需要显式调用它们 .

  • 25

    Web资源与java资源不同,后者应放在类路径中 . Web资源通过war插件处理,应该放在 src\main\webapp\WEB-INF\ 中 . 在这种情况下,它将自动工作,而无需在pom.xml中进行任何其他配置

  • 0

    这个配置正在工作添加插件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>
    

相关问题