You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Appengine Maven Plugin排除node_modules不复制到target目录?

解决App Engine Maven插件复制node_modules到target导致启动缓慢的问题

我完全懂你的痛点——node_modules里动辄上万的文件被全盘复制到target目录,直接把本地启动时间拖到了10分钟。你已经在appengine-web.xml里排除了它作为静态/资源文件,但这还不够,因为Maven的资源复制流程会默认把src/main/webapp下的所有内容同步到target目录,这个过程不受appengine-web.xml的控制。

下面是具体的解决步骤:

1. 配置Maven资源插件排除node_modules

你需要在pom.xml中添加maven-resources-plugin的配置,明确告诉Maven不要复制node_modules目录:

<build>
  <plugins>
    <!-- 先配置资源插件排除node_modules -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.3.1</version> <!-- 可使用最新稳定版 -->
      <configuration>
        <!-- 排除主资源中的node_modules -->
        <excludes>
          <exclude>node_modules/**</exclude>
        </excludes>
      </configuration>
      <executions>
        <!-- 确保处理测试资源时也排除 -->
        <execution>
          <id>default-testResources</id>
          <phase>process-test-resources</phase>
          <goals>
            <goal>testResources</goal>
          </goals>
          <configuration>
            <excludes>
              <exclude>node_modules/**</exclude>
            </excludes>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <!-- 保留你原有的App Engine插件配置 -->
    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>appengine-maven-plugin</artifactId>
      <version>${appengine.maven.plugin.version}</version>
      <configuration>
        <deploy.project>XXX PROJECT XXX</deploy.project>
        <deploy.version>XXX VERSION XXX</deploy.version>
        <deploy.stopPreviousVersion>false</deploy.stopPreviousVersion>
        <deploy.promote>false</deploy.promote>
      </configuration>
    </plugin>
  </plugins>
</build>

2. 可选:仅在开发环境生效(推荐)

既然你只在开发阶段需要node_modules,可以把上述配置放到Maven Profile中,避免影响生产部署:

<profiles>
  <profile>
    <id>development</id>
    <activation>
      <activeByDefault>true</activeByDefault> <!-- 默认激活开发环境 -->
    </activation>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.3.1</version>
          <configuration>
            <excludes>
              <exclude>node_modules/**</exclude>
            </excludes>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

部署生产环境时,只需执行mvn appengine:deploy -P!development即可禁用这个排除规则(如果部署不需要node_modules,也可以直接忽略)。

为什么之前的配置没用?

appengine-web.xml里的<exclude>只是告诉App Engine运行时不要把node_modules当作静态文件或资源文件加载,但Maven在构建阶段的资源复制是独立的流程,它会默认同步src/main/webapp下的所有内容到target目录,所以必须通过Maven插件来拦截这个过程。

配置完成后,执行mvn clean appengine:run,你会发现target目录里不再有node_modules,启动速度会大幅提升!

内容的提问来源于stack exchange,提问作者Jokus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:46:18