Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

resources - Maven : copy files without subdirectory structure

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...