简体   繁体   中英

Maven concatenate text files at compile phase

I have a folder containing different txt files in my Maven project.

How can i tell maven, each time the compile phase is performed, to concatenate all those txt files and append all of them inside a single text file immediately outside that folder?

If you could help me out with some directions, where to look what to use because at the moment the only thing that seems a bit close to my goal is Swagger but i see is specific for building APIs.

I have no clue where to start

Thanks Davide

I think there is no direct maven way to achieve it, but you can Maven with Ant to achieve it. First of all you have to use maven-antrun-plugin and inside <configuration> ,you have to use ant specific to merge the file. Since you want in compile phase, you have to mention goal as compile . The code will look something like this. This is the hint you can try.

... other code ...
<goals>
   <goal>compile</goal>
</goals>
<configuration>
  <target>
   <merge tofile="allconcatenated.txt">
        <sourcefiles>
            <fileset dir="file1"></fileset>
            <fileset dir="file2"></fileset>
        </sourcefiles>
   </merge>
  </target>
</configuration>

When comes to file operations, I usually prefer using maven-antrun-plugin .

For your case concat ant task is a perfect match.

Please find below an example configuration of the aforementioned plugin.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>default-cli</id>
            <phase>compile</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <!--Provide the destination file-->
                    <concat destfile="${project.basedir}/files.txt"
                            force="yes">
                    <!-- Provide directory containing txt files that you need to concat-->
                        <fileset dir="${project.basedir}/src/main/resources/text_files">
                            <include name="*.txt"></include>
                        </fileset>
                    </concat>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>            

Afterwards, execute mvn compile and the concatenated file will be created in the directory you have defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM