在 Maven 中过滤文件名

发布于 2024-10-26 18:04:29 字数 889 浏览 0 评论 0原文

我希望能够在 Maven 中过滤文件名和文件内容。

下面的代码片段可以过滤文件内容,但我也需要能够重命名文件。

使用场景是我希望对我的web应用程序中的所有静态资源进行编号,以便亚马逊的CloudFront将它们视为不同的版本。当然,手动管理数字是不切实际的,所以我希望构建过程能够做到这一点。

例如,一个名为 的文件

logo_VERSION.jpg

最终会被称为

logo_254.jpg

如果不编写自定义插件就可以实现这一点,有什么想法吗?


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>/src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
                ...

I'd like to be able to filter filenames as well as file contents in Maven.

The below snippet enables filtering on file contents, but I need to be able to rename files too.

The usage scenario is that I'd like all static resources in my webapp to be numbered, so they can be treated as different versions by Amazon's CloudFront. Naturally managing the numbers manually would be impractical, so I'd like the build process to do this.

For instance a file called

logo_VERSION.jpg

would end up being called

logo_254.jpg

Any ideas if this is possible without writing a custom plugin?


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>/src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
                ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2024-11-02 18:04:29

我已经使用 antrun 插件做了类似的事情 - 有时你只需要回到 ant 即可。

pom 片段

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>build</id>
                    <phase>prepare-package</phase>
                    <configuration>
                        <tasks>
                            <property name="project.version" value="${project.version}"/>
                            <property name="all.environs" value="DEV1,DEV2,DEV3,DEV4,UAT,PROD"/>
                            <property name="application.environments" 
                                        value="${application.environments}" />

                            <ant antfile="${basedir}/build.xml" target="setup" />
                            <ant antfile="${basedir}/build.xml" target="build"/>

                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

build.xml

<property name="ant-contrib-jar" value="${user.home}/.ant/lib/ant-contrib-1.0b3.jar"/>

<target name="setup" unless="ant-contrib.present">
    <echo>Getting ant-contrib</echo>
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${ant-contrib-jar}"
         src="http://nexus.inhouse.com:8081/nexus/content/repositories/central/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>  
</target>

<target name="taskdefs">
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${ant-contrib-jar}"/>
        </classpath>
    </taskdef>
</target>

<target name="build" depends="taskdefs">

    <echo message="basedir: ${basedir}"/>
    <echo message="project.version: ${project.version}"/>

    <foreach list="${application.environments}" target="jar-resources" param="app.env" trim="true">

        <param name="basedir" value="${basedir}" />
        <param name="project.version" value="${project.version}" />

    </foreach>

</target>

<target name="jar-resources">

    <mkdir dir="${basedir}/target/${app.env}"/>

    <copy todir="${basedir}/target/${app.env}">
        <fileset dir="${basedir}/src/main/resources">
            <include name="mail_config.properties"/>
            <include name="service.properties"/>
        </fileset>
    </copy>

    <filterset id="applicationFilterSet">
        <filtersfile file="${basedir}/src/main/filters/filter-${app.env}.properties"/>
        <filter token="PROJECT.VERSION" value="${project.version}"/>
    </filterset>

    <copy file="${basedir}/src/main/resources/coresystem.properties"  
        tofile="${basedir}/target/${app.env}/coresystem.properties.${app.env}">
        <filterset refid="applicationFilterSet"/>
    </copy>

    <copy file="${basedir}/src/main/resources/extraProps.properties"  
        tofile="${basedir}/target/${app.env}/extraProps_${app.env}.properties">
        <filterset refid="applicationFilterSet"/>
    </copy>

    <jar destfile="${basedir}/target/MyApp-env-${project.version}-${app.env}.jar"
        basedir="${basedir}/target/${app.env}" />

</target>

I've done something similar using the antrun plugin - sometimes you just have to drop back into ant.

pom snippet

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>build</id>
                    <phase>prepare-package</phase>
                    <configuration>
                        <tasks>
                            <property name="project.version" value="${project.version}"/>
                            <property name="all.environs" value="DEV1,DEV2,DEV3,DEV4,UAT,PROD"/>
                            <property name="application.environments" 
                                        value="${application.environments}" />

                            <ant antfile="${basedir}/build.xml" target="setup" />
                            <ant antfile="${basedir}/build.xml" target="build"/>

                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

build.xml

<property name="ant-contrib-jar" value="${user.home}/.ant/lib/ant-contrib-1.0b3.jar"/>

<target name="setup" unless="ant-contrib.present">
    <echo>Getting ant-contrib</echo>
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${ant-contrib-jar}"
         src="http://nexus.inhouse.com:8081/nexus/content/repositories/central/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>  
</target>

<target name="taskdefs">
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${ant-contrib-jar}"/>
        </classpath>
    </taskdef>
</target>

<target name="build" depends="taskdefs">

    <echo message="basedir: ${basedir}"/>
    <echo message="project.version: ${project.version}"/>

    <foreach list="${application.environments}" target="jar-resources" param="app.env" trim="true">

        <param name="basedir" value="${basedir}" />
        <param name="project.version" value="${project.version}" />

    </foreach>

</target>

<target name="jar-resources">

    <mkdir dir="${basedir}/target/${app.env}"/>

    <copy todir="${basedir}/target/${app.env}">
        <fileset dir="${basedir}/src/main/resources">
            <include name="mail_config.properties"/>
            <include name="service.properties"/>
        </fileset>
    </copy>

    <filterset id="applicationFilterSet">
        <filtersfile file="${basedir}/src/main/filters/filter-${app.env}.properties"/>
        <filter token="PROJECT.VERSION" value="${project.version}"/>
    </filterset>

    <copy file="${basedir}/src/main/resources/coresystem.properties"  
        tofile="${basedir}/target/${app.env}/coresystem.properties.${app.env}">
        <filterset refid="applicationFilterSet"/>
    </copy>

    <copy file="${basedir}/src/main/resources/extraProps.properties"  
        tofile="${basedir}/target/${app.env}/extraProps_${app.env}.properties">
        <filterset refid="applicationFilterSet"/>
    </copy>

    <jar destfile="${basedir}/target/MyApp-env-${project.version}-${app.env}.jar"
        basedir="${basedir}/target/${app.env}" />

</target>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文