使用 Maven Replacer 插件列出文件夹中的文件

问题描述 投票:0回答:2

我想使用 maven-replacer-plugin 将文件中的 $file-list$ 替换为项目文件夹中逗号分隔的文件列表。这可能吗?

谢谢

java maven plugins pom.xml
2个回答
15
投票

这就是我所做的:

使用 antrun 插件创建一个临时文件,其中包含列表:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <target>
                    <fileset id="my-fileset" dir="src/main/resources" />
                    <pathconvert targetos="unix" pathsep=","
                        property="my-file-list" refid="my-fileset">
                        <map from="${basedir}\src\main\resources\" to="" />
                    </pathconvert>
                    <echo file="${basedir}\target\file-list.txt">${my-file-list}</echo>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

然后使用替换插件替换我想要的文件中的列表:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <executions>
        <execution>
            <id>replace-file-list</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <ignoreMissingFile>false</ignoreMissingFile>
                <file>target/MY_FILE.TXT</file>
                <regex>false</regex>
                <token>$file-list$</token>
                <valueFile>target/file-list.txt</valueFile>
            </configuration>
        </execution>
    </executions>
</plugin>

我确信一定有更好的方法,但希望这对某人有帮助。


0
投票

如果您使用

maven-antrun-plugin
生成输入,那么您也可以使用它来更新您的文件 - 这将是同一插件中的另一个命令,因此稍微简单和干净。

我做过这样的事情:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <target>
                    <fileset id="file-list" dir="src/main/resources"/>
                    <pathconvert targetos="unix" pathsep=","
                            property="file-list" refid="anot-list">
                        <map from="${basedir}/src/main/resources/" to=""/>
                    </pathconvert>
                    <replace file="${basedir}/target/MY_FILE.TXT">
                        <replacefilter token="$file-list$" value="${file-list}"/>
                    </replace>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
© www.soinside.com 2019 - 2024. All rights reserved.