简体   繁体   English

Maven 在编译阶段连接文本文件

[英]Maven concatenate text files at compile phase

I have a folder containing different txt files in my Maven project.我的 Maven 项目中有一个包含不同 txt 文件的文件夹。

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?每次执行编译阶段时,我如何告诉 Maven 连接所有这些 txt 文件并将它们全部附加到该文件夹​​外的单个文本文件中?

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.如果你能帮我指明一些方向,在哪里查看使用什么,因为目前唯一似乎有点接近我的目标的是 Swagger,但我认为它是专门用于构建 API 的。

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.我认为没有直接的 Maven 方法来实现它,但是您可以使用 Maven 和 Ant 来实现它。 First of all you have to use maven-antrun-plugin and inside <configuration> ,you have to use ant specific to merge the file.首先你必须使用maven-antrun-plugin并且在<configuration> ,你必须使用 ant 特定的来合并文件。 Since you want in compile phase, you have to mention goal as compile .由于您想要在 compile 阶段,您必须将目标称为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 .在文件操作方面,我通常更喜欢使用maven-antrun-plugin

For your case concat ant task is a perfect match.对于您的情况, concat ant 任务是一个完美的匹配。

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.之后,执行mvn compile ,将在您定义的目录中创建连接的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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