build-helper-maven-plugin add-test-resource error

troig picture troig · Oct 13, 2014 · Viewed 12.2k times · Source

I have this project structure:

/src
    /main
        /java
        /resources
    /test 
        /java
        /resources
    /it
        /java
        /resources

test for unit tests and it for integration tests. I'm using build-helper-maven-plugin to add additional test sources/resources to the classpath for later use maven-surfire-plugin for run unit tests and maven-failsafe-plugin for integration tests.

Plugin config as belows:

<plugin>                                                         
   <groupId>org.codehaus.mojo</groupId>                          
   <artifactId>build-helper-maven-plugin</artifactId>      
   <version>1.9.1</version>      
   <executions>                                                  
      <execution>                                                
         <id>add-integration-test-sources</id>                   
         <phase>generate-test-sources</phase>                    
         <goals>                                                 
            <goal>add-test-source</goal>                         
         </goals>                                                
         <configuration>                                         
            <sources>                                            
               <source>src/it/java</source>                      
            </sources>                                           
         </configuration>                                        
      </execution>                                               
      <execution>                                                
         <id>add-integration-test-resources</id>                 
         <phase>generate-test-resources</phase>                  
         <goals>                                                 
            <goal>add-test-resource</goal>                       
         </goals>                                                
         <configuration>                                         
            <resources>                                          
               <directory>/src/it/resources</directory>
            </resources>                                         
         </configuration>                                        
      </execution>                                               
   </executions>                                                 
</plugin>       

This works fine for the test-sources (they are coppied correctly to /target/test-classes) but doesn't copy test-resources. I've tried different combinations of <configuration>: use <resource> instead <directory>, use an specific file instead a directory...but neither works.

Stacktrace with the error:

Caused by: org.apache.maven.plugin.PluginConfigurationException: Unable to parse configuration of mojo org.codehaus.mojo:build-helper-maven-plugin:1.9.1:add-test-resource for parameter directory: Cannot configure instance of org.apache.maven.model.Resource from src/it/resources
        at org.apache.maven.plugin.internal.DefaultMavenPluginManager.populatePluginFields(DefaultMavenPluginManager.java:597)
        at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getConfiguredMojo(DefaultMavenPluginManager.java:529)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:92)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)

PROVISIONALLY, I've fixed it adding the integration tests resources to maven <build> configuration:

<build>
...
    <testResources>                               
       <testResource>                             
          <directory>src/it/resources</directory> 
       </testResource>                            
    </testResources>    
</build>

But I would prefer to have centralized all classpath modifications under build-helper-maven-plugin. Can anyone post example with a correct config?

Thanks in advance.

Answer

Ren&#233; Link picture René Link · Oct 13, 2014

According to the javadoc of the maven-build-helper-plugin:add-test-resources. The resources is an array of org.apache.maven.model.Resource. Thus you must configure it this way:

<configuration>
    <resources>  
         <resource>                                     
               <directory>/src/it/resources</directory>
         </resource>
    </resources>      
</configuration>

Take a look at how to configure plugin parameters.