为什么我的资源没有被复制到"验证"相位φ

时间:2016-06-03 15:09:26

标签: maven maven-resources-plugin

我正在尝试确保将正确的资源移至WEB-INF/config/,无论我是否致电

mvn gcloud:deploy

mvn tomcat7:run 

这就是我要强制设置-Dpackage-mode=<value>的原因。我正在使用maven-resource-plugin复制部件,如下所示:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>validate</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>src/main/webapp/WEB-INF/config</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/assembly/${package-mode}/config</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

但是,如果我跑

mvn validate tomcat7:run -Dpackage-mode=dev 

启动服务器时不会复制文件。

E:\java\mz\mz-server\mz-web-server>mvn validate tomcat7:run -Denv=dev -Dpackage-mode=dev
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mz-web-server 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-maven) @ mz-web-server ---
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-property) @ mz-web-server ---
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ mz-web-server >>>
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-maven) @ mz-web-server ---
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-property) @ mz-web-server ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mz-web-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mz-web-server ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ mz-web-server <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ mz-web-server ---
[INFO] Running war on http://localhost:8080/
[INFO] Using existing Tomcat server configuration at E:\java\mz\mz-server\mz-web-server\target\tomcat
[INFO] setting SystemProperties:
[INFO]  gwt.codeserver.port=9876
[INFO] create webapp with contextPath:
Jun 03, 2016 5:24:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 03, 2016 5:24:57 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Jun 03, 2016 5:24:57 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
...

我在这里做错了什么?我实际上并不知道validate在这里是不是一个好选择而且我并不是真的受某个阶段的约束。我想要的是确保根据package-mode来复制配置文件。

可是:

如果我只是跑

mvn validate -Dpackage-mode=dev 

然后一切都按预期工作 - 文件被复制。

1 个答案:

答案 0 :(得分:2)

您不应使用maven-resources-plugin复制webapp资源,此插件仅适用于应用程序资源。此外,您应该从不生成或复制src下的文件,就像您使用<outputDirectory>src/main/webapp/WEB-INF/config</outputDirectory>一样。生成的文件应始终放在target文件夹下。

相反,请使用maven-war-plugin。您可以将其配置为filter a specific directory并将其输出到WAR中,如下所示:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <webResources>
      <resource>
        <directory>src/main/assembly/${package-mode}/config</directory>
        <filtering>true</filtering>
        <targetPath>WEB-INF/config</targetPath>
      </resource>
    </webResources>
  </configuration>
</plugin>

这将过滤目录src/main/assembly/${package-mode}/config并将其下的文件放在WAR中的目标文件夹WEB-INF/config中。

通过此更改,您将能够使用mvn clean tomcat7:run-war(而不是tomcat7:run运行您的webapp,因为这不会打包WAR。)