1

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>
Community
  • 1
  • 1
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • remove the `` element, that is, put it into `build/plugins` and not into `build/pluginManagement` section. – A_Di-Matteo Jun 02 '16 at 20:45
  • @A.DiMatteo I had to add it because otherwise m2e identifies an error as described in another [question](http://stackoverflow.com/questions/6352208/how-to-solve-plugin-execution-not-covered-by-lifecycle-configuration-for-sprin). – Stefan Falk Jun 02 '16 at 20:46
  • 1
    argh, it's too long to describe in a comment, I need to write an answer then.. ok – A_Di-Matteo Jun 02 '16 at 20:49
  • @A.DiMatteo Looking forward to it :D – Stefan Falk Jun 02 '16 at 20:50
  • Apart from the above you should never copy (it does not matter how) into `src/` cause it's under version control. Furthermore what you are doing is copying from `src/main/resources/` which is by default handled by the resources copying...so no need to add `maven-antrun-plugin`... – khmarbaise Jun 04 '16 at 15:55

2 Answers2

2

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.

Community
  • 1
  • 1
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • Nit-pick but I would have `prepare-package` since apparently this should be invoked to set-up the correct config in the packaging. – Tunaki Jun 02 '16 at 20:55
  • @Tunaki definitely good point, my focus was on the build configuration, but this is definitely worth to add and clarify, I'll update accordingly! – A_Di-Matteo Jun 02 '16 at 20:56
  • If I do it *exactly* like you did it like placing it *right below* `pluginManagement` then I get that "*Plugin execution not covered by lifecycle configuration*" error .. – Stefan Falk Jun 02 '16 at 21:01
  • 1
    @displayname what is not working? I just tried your code and got HelloWorld! in the console. Concerning Eclipse, the phase is the issue in its default mappings, chaging it to prepare-package solved the issue. Can you give it a try? – A_Di-Matteo Jun 02 '16 at 21:10
  • @A.DiMatteo That was the problem! Using `prepare-package` does work for me as well. Sorry, I didn't see that you updated your answer already! – Stefan Falk Jun 02 '16 at 21:11
  • @displayname no problem, but good I also gave it a try hands-on, always better ;-) – A_Di-Matteo Jun 02 '16 at 21:12
  • @A.DiMatteo Thanks for helping me! :) – Stefan Falk Jun 02 '16 at 21:15
1

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>
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107