【问题标题】:Maven copy resources executed twice once for profile once defaultMaven 复制资源一次执行一次配置文件一次默认
【发布时间】:2018-10-07 16:22:16
【问题描述】:

我有一个 maven 项目,我在其中定义了一个基于 profile 的构建,包括自定义 maven-resource-plugin 配置。

...
<profiles>
    <profile>
        <id>docker</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <executions>
                        <execution>
                            <id>resources</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/target/classes</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/src/main/resources</directory>
                                        <filtering>false</filtering>
                                        <excludes>
                                            <exclude>log4j2*.xml</exclude>
                                            <exclude>docker/*</exclude>
                                        </excludes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
...

现在,当我执行 mvn compile -P docker 时,我可以看到资源插件执行了两次。

INFO] --- maven-resources-plugin:3.1.0:copy-resources (resources) @ mma-access-management-auth-server ---

[INFO] 使用 'UTF-8' 编码复制过滤的资源。

[INFO] 复制 2 个资源

[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ mma-access-management-auth-server ---

[INFO] 使用 'UTF-8' 编码复制过滤的资源。

[INFO] 复制 5 个资源

这是对的吗?我真的必须排除默认构建的所有资源以避免它们通过默认资源再次复制吗?

【问题讨论】:

  • 有点像。插件可以在生命周期中运行多次,甚至可以在同一阶段运行。提示将是您提供的执行 ID:resources - 如果要替换默认值,则需要使用相同的执行 ID - 如果要将配置放在执行块中。如果有不同的插件,则每个配置的执行都会运行。

标签: maven maven-profiles maven-resources-plugin


【解决方案1】:

不要再次将插件绑定到 generate-resources 阶段。 仅使用配置标签时,可以更改默认执行的配置。

...
<profiles>
    <profile>
        <id>docker</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/resources</directory>
                                <filtering>false</filtering>
                                <excludes>
                                    <exclude>log4j2*.xml</exclude>
                                    <exclude>docker/*</exclude>
                                </excludes>
                            </resource>
                        </resources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
...

【讨论】:

  • 当我不将它绑定到阶段/目标时,插件不会被执行?
  • 是的,当你不在配置文件中绑定它时,插件只执行一次,配置文件中指定的配置。
  • 谢谢,它有效。我忽略了将配置放在执行部分之外。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
相关资源
最近更新 更多