Why is my target not getting executed?

风格不统一 提交于 2019-12-13 01:44:49

问题


I am trying to get ready for deployment and therefore I want to copy the correct configuration files to WEB-INF/classes/ before everything gets packed into the WAR file for either deployment or development.

In the end I want to execute deployment-tasks whenever I call

mvn glcoud:deploy

- which is when I need deployment configuration files - and development-tasks whenever something else gets executed in my project directory.

At the moment I have not decided how exactly I'm going to do it but first of all I try to execute such a "dummy task". Unfortunately it is not working.

This is the profile I configured in the 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>

It is supposed to echo "Hello World!" and copy a x.xml file from A to B. I decided to do this in the compile phase which means

mvn clean compile

should actually be enough to get the target executed but .. I wouldn't be here if it worked.

Question: Does somebody know why this is not getting executed?


As mentioned in a comment, I could/should remove pluginManagement from build. However, this would give me an error saying:

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

I've added pluginManagement according to an answer of the question "How to solve “Plugin execution not covered by lifecycle configuration” for Spring Data Maven Builds".


The solution below is giving the same “Plugin execution not covered by lifecycle configuration” error

<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>

and I am seeing the same for:

<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>

回答1:


In order to make m2e happy and yet being able to meet your requirements, try the following:

<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>    

Note the additional plugins section which is basically just repeating the artifactId of the plugin.

What's happening here:

  • Via the pluginManagement section we are telling Maven: whenever the build (via POM configuration or command line execution) needs to execute this plugin, then apply this version by default and this configuration an executions
  • The m2e not-so-perfect integration between Maven and Eclipse will then be happy about this plugin configuration, however no plugin execution will ever happen unless we effectively declare it
  • Via the plugins section we are eventually really defining our build, telling Maven to add this plugin to its build plan. No need to specify version, configuration or executions, since we already defined them into the pluginManagement (that is, management of plugins), which will be applied as default configuration/behavior.

For further details concerning the difference between plugins and pluginManagement, check the reference post on SO: Maven: what is pluginManagement.


Further note on the associated phase for such an execution: the prepare-package phase would be a more (semantically correct and maintenability-friendly) choice than compile. Check the official Build Lifecycle phases list for more details. Concerning prepare-package:

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


Update
It appears that not only as described above the prepare-package phase would be a better choice, but it also the right phase to make the m2e plugin perfectly happy in this case.




回答2:


See POM Reference, Plugin Management:

  • pluginManagement

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

That means declaring a plugin in <pluginManagement> is just half of the story. You have to declare it in a <build>/<plugins> section, too, to actually execute its goal.

In your case:

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


来源:https://stackoverflow.com/questions/37601777/why-is-my-target-not-getting-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!