29

I am trying to use Maven to move all the *.xsd files contained in a given folder to another one, but without the source subdirectory structure.

This is what I have so far:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <id>move-schemas</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/schemas-target</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

...

<resources>
    <resource>
        <directory>${basedir}/schemas-source</directory>
        <includes>
            <include>**/*.xsd</include>
        </includes>
    </resource>
</resources>

And it is (almost) working. The only problem is that it keeps the source subdirectory structure, while I need to remove that hierarchy and put all the xsd files in the target folder. Example:

This is what I have in the schemas-source folder:

schemas-source
 │- current
 │    │- 0.3
 │        │- myfile.xsd
 │- old
      │- 0.2
          │- myfile-0.2.xsd

and this is what I'd need in the schemas-target folder:

schemas-target
 │- myfile.xsd
 │- myfile-0.2.xsd
Guido
  • 46,642
  • 28
  • 120
  • 174
  • I think the answer I posted here: https://stackoverflow.com/a/72187373/2026010 might help. And it's done only with Maven. – Felipe Mosso May 10 '22 at 13:41

2 Answers2

25

I banged my head against that restriction myself, again and again.

Basically: I don't think there's a maven only solution. You will have to resort to using something dynamic like

  • The Maven Antrun Plugin
    Embed ant tasks in maven, in this case an ant copy task, something like this:

    <copy todir="${project.basedir}/schemas-target" flatten="true">
        <fileset dir="${project.basedir}/schemas-source">
            <include name="**/*.xsd"/>
        </fileset>
    </copy>
    
  • The GMaven plugin Lets you execute Groovy code from your pom, something like this:

    new File(pom.basedir, 'schemas-source').eachFileRecurse(FileType.FILES){
        if(it.name.endsWith('.xsd')){
            new File(pom.basedir, 'schemas-target/${it.name}').text = it.text;
        }
    }
    
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Thank you, I tried the Maven Antrun Plugin, but I was not aware of that flattern="true" attribute that does the trick. – Guido Nov 23 '10 at 16:43
  • Unfortunately the antrun plugin is the only way I've been able to perform similar tasks...seems like "flatten" should have been included as a setting in the copy-resources goal of maven-resources plugin. – JCD Nov 23 '10 at 16:47
  • flatten is a start, but often I want to set a "start path" like when I expand a WAR archive, I may want to lose web-inf/classes but keep the package structure. But I guess that can be achieved using mappers: http://ant.apache.org/manual/Types/mapper.html – Sean Patrick Floyd Nov 23 '10 at 17:18
  • You may also use `move` instead of `copy` which should remove the source dir/files as well. – Kenston Choi Jan 11 '19 at 06:35
13

There is a way.. and its really monotonous and cumbersome. Implement it only if you want to be completely mavenized. Sean's answer is the easiest solution.

The problem is you have to specify each and every directory and then use wild cards for the files inside.

<execution>
<id>copy-jars</id>
<phase>process-sources</phase>
<goals>
    <goal>copy-resources</goal>
</goals>
<configuration>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <resources>
        <resource>
            <directory>${basedir}/resources</directory>
            <includes>
                <include>xyz.jar</include>
            </includes>
        </resource>     
        <resource>
            <directory>${basedir}/as-u-like-it</directory>
            <includes>
                <include>abc.jar</include>
            </includes>
        </resource>     
</configuration>