【问题标题】:Maven: how to get a war package with resources copied in WEB-INF?Maven:如何获取在 WEB-INF 中复制的资源的战争包?
【发布时间】:2011-06-13 05:57:06
【问题描述】:

当我使用 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=true clean package resources: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,sn-p 基本上与文档相同 有什么想法吗?

【问题讨论】:

  • 不要运行 resources:copy-resources 目标,它会自动包含在阶段 copy-resources 中(在我的答案中)

标签: maven war


【解决方案1】:

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

编辑:

此配置对我有用:

  <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] 中挂钩的所有插件目标,因此无需显式调用它们。

【讨论】:

  • 也可以使用${project.build.finalName} 而不是${project.artifactId}-${project.version} 来获取工件的名称。
【解决方案2】:

web资源和java资源不一样,应该放在classpath下。 Web 资源是通过 war 插件处理的,应该放在src\main\webapp\WEB-INF\ 中。在这种情况下,它会自动工作,无需在 pom.xml 中进行任何额外配置

【讨论】:

  • 但是,如果java resource没有自动部署到需要的地方,怎么处理?
  • @Eddy 如果它没有部署到“需要的地方”,很可能意味着你把它放在了源代码的错误位置。提供更多细节。
  • 你是对的!在这种情况下:使用 maven 并运行 mvn clean。那么当将资源放在src/main/java而不是src/main/resources下时,maven不会为我重新部署它。我正在使用日食。
【解决方案3】:

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

【讨论】:

  • 使用 maven WAR Plug 复制任何网络资源是最糟糕的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 2012-06-05
  • 1970-01-01
相关资源
最近更新 更多