为什么我的目标没有被执行?

Why is my target not getting executed?

我正在尝试为部署做好准备,因此我想将正确的配置文件复制到 WEB-INF/classes/ ,然后 一切都打包到 WAR 部署开发.

的文件

最后我想在每次调用时执行 deployment-tasks

mvn glcoud:deploy

- 每当我的项目目录中执行其他内容时,我就需要部署配置文件和开发任务。

目前我还没有决定我将如何做,但首先我尝试执行这样一个 "dummy task"。不幸的是,它不起作用。

这是我在 pom.xml:

中配置的配置文件
<profile>       
    <id>default-profile</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <build>
        <pluginManagement> 
            <plugins>                   
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>                          
                    <executions>                        
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <configuration>
                                <target>
                                    <echo message="Hello World!"/>
                                    <copy file="src/main/resource/x.xml" todir="src/main" />
                                </target>
                            </configuration>                                    
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>                           
                </plugin>
            </plugins>
        </pluginManagement> 
    </build>
</profile>

应该echo"Hello World!"然后从A复制一个x.xml文件到B。我决定在compile相表示

mvn clean compile

实际上应该足以让 target 执行但是..如果它有效我就不会在这里了。

问题:有人知道为什么没有执行吗?


如评论中所述,我 could/should 从 build 中删除了 pluginManagement。但是,这会给我一个错误提示:

Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-antrun-plugin:1.8:run (execution: compile, phase: compile)

我根据问题“How to solve “Plugin execution not covered by lifecycle configuration” for Spring Data Maven Builds”的回答添加了pluginManagement


下面的解决方案给出了相同的“生命周期配置未涵盖的插件执行”错误

<profile>       
    <id>default-profile</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>   
    <build>
        <pluginManagement> 
            <plugins>                   
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>                          
                    <!-- -->
                    </executions>               
                </plugin>
            </plugins>
        </pluginManagement>         
        <plugins>           
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
            </plugin>
        </plugins>
    </build>    

</profile>

我也看到了同样的情况:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
        </plugin>   
    </plugins>
</build>

<profiles>
    <profile>       
        <id>default-profile</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>       
        <build>
            <pluginManagement> 
                <plugins>                   
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.8</version>                          
                        <!-- -->                
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>    
    </profile>      
<profiles>

为了让 m2e 开心又能满足您的要求,请尝试以下操作:

<build>
    <pluginManagement> 
        <plugins>                   
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>                          
                <executions>                        
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <configuration>
                            <target>
                                <echo message="Hello World!"/>
                                <copy file="src/main/resource/x.xml" todir="src/main" />
                            </target>
                        </configuration>                                    
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>                           
            </plugin>
        </plugins>
    </pluginManagement> 
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
        </plugin>
    </plugins>
</build>    

注意附加的 plugins 部分,它基本上只是重复插件的 artifactId

这里发生了什么:

  • 通过 pluginManagement 部分我们告诉 Maven:每当构建(通过 POM 配置或命令行执行)需要执行此插件时,默认应用此版本,此配置执行
  • Maven 和 Eclipse 之间的 m2e 不那么完美的集成将对这个插件配置感到满意,但是除非我们有效地声明它,否则不会执行任何插件
  • 通过 plugins 部分,我们最终真正定义了我们的构建,告诉 Maven 将这个插件添加到它的构建计划中。无需指定版本、配置或执行,因为我们已经将它们定义到pluginManagement(即管理插件)中,将默认应用configuration/behavior。

有关插件和 pluginManagement 之间差异的更多详细信息,请查看 reference post on SO: Maven: what is pluginManagement


关于此类执行的关联 phase 的进一步说明:prepare-package 阶段将是比 compile 更(语义正确且易于维护)的选择。查看 official Build Lifecycle phases list 了解更多详情。关于 prepare-package:

perform any operations necessary to prepare a package before the actual packaging.


更新
看来不仅如上所述 prepare-package 阶段是更好的选择,而且在这种情况下它也是使 m2e 插件完全满意的正确阶段。

参见POM Reference, Plugin Management

  • pluginManagement

    However, this only configures plugins that are actually referenced within the plugins element in the children.

这意味着在 <pluginManagement> 中声明一个插件只是故事的一半。您也必须在 <build>/<plugins> 部分中声明它才能真正执行其目标。

你的情况:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-antrun-plugin</artifactId>
    </plugin>
  </plugins>
</build>