maven项目中的webapp包下的文件没有打包进target

本文介绍了如何在Maven构建项目时,通过配置maven-war-plugin插件,将src/main/webapp目录下的文件打包到target目录,以确保web应用资源的正确集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当 Maven 打包时,默认情况下并不会将 src/main/webapp 目录下的文件直接打包到 target 目录中。这是因为在传统的 WAR 项目中,webapp 目录下的文件是属于 web 应用程序的资源,不属于 Java 代码及其编译结果。 Maven 会使用插件将 webapp 目录下的文件复制到生成的 WAR 文件中。

要确保 webapp 目录下的文件被正确打包到 target 目录中,你需要使用 maven-war-plugin 插件并在 pom.xml 中进行相应的配置。

<build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.*</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      //确保在项目的 pom.xml 文件中正确配置了 webapp 目录为 Maven 的资源目录。
      <resource>
        <directory>src/main/webapp</directory>
        <targetPath>META-INF/resources</targetPath>
      </resource>
    </resources>
    <plugins>
      <!-- 其他插件配置 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
          <webResources>
            <resource>
              <directory>src/main/webapp</directory>
              <targetPath>/</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

写完上面的代码后,更新maven资源,用右侧栏的maven工具clean已经生成的target,然后点击小锤子生产新的target即可。(这一步不要做错。要用maven去clean,而不是delete删除target。)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值