如何使用<MacroDef>统计文件夹中不同扩展名的文件数量
<MacroDef> Got it! Let's figure out how to use Ant's <MacroDef> to batch count files by their extensions—this will save you from repeating the same <resourcecount> code over and over. Here's a clean, reusable solution tailored to your needs:
Step 1: Define a Reusable Macro
First, we'll create a macro that encapsulates the logic to count files of a specific extension. This macro will accept parameters for the extension and (optionally) a prefix for the property that stores the count.
<project name="FileExtensionCounter" default="count-extensions"> <!-- Macro to count files with a given extension --> <macrodef name="count-files-by-extension"> <!-- Required: The file extension to count (e.g., xml, txt) --> <attribute name="ext" /> <!-- Optional: Prefix for the property holding the count (defaults to "count") --> <attribute name="property-prefix" default="count" /> <sequential> <!-- Use resourcecount to tally files matching the extension --> <resourcecount property="@{property-prefix}.@{ext}"> <fileset dir="${basedir}"> <include name="*.@{ext}" /> </fileset> </resourcecount> <!-- Print the result to the console --> <echo message="Found ${@{property-prefix}.@{ext}} *.@{ext} files in ${basedir}" /> </sequential> </macrodef>
Step 2: Call the Macro for Multiple Extensions
Next, create a target that calls the macro for each extension you want to count. This keeps your code organized and easy to update.
<!-- Target to run batch counting --> <target name="count-extensions"> <!-- Call the macro for each extension --> <count-files-by-extension ext="xml" /> <count-files-by-extension ext="txt" /> <count-files-by-extension ext="java" /> <count-files-by-extension ext="properties" /> <!-- Optional: Reuse the counts later in your build --> <echo message="--- Summary ---" /> <echo message="XML files: ${count.xml} | TXT files: ${count.txt}" /> <echo message="Java files: ${count.java} | Properties files: ${count.properties}" /> </target> </project>
How It Works
- The
@{ext}placeholder in the macro gets replaced with the extension you pass when calling the macro (e.g.,xmlbecomes*.xmlin the fileset). - The count is stored in a property like
${count.xml}(using the default prefix) or a custom one if you specifyproperty-prefix. - You can easily add or remove extensions by adding/removing
<count-files-by-extension>calls.
Advanced: Loop Through a List of Extensions
If you have a long list of extensions, you can use the <foreach> task from ant-contrib to avoid repetitive macro calls (note: you'll need to add ant-contrib to your Ant classpath first):
<taskdef resource="net/sf/antcontrib/antcontrib.properties" /> <target name="count-many-extensions"> <!-- Define your list of extensions --> <property name="extensions" value="xml,txt,java,properties,csv" /> <!-- Loop through the list and call the macro for each extension --> <foreach list="${extensions}" delimiter="," target="count-single-extension" param="current.ext" /> </target> <target name="count-single-extension"> <count-files-by-extension ext="${current.ext}" /> </target>
内容的提问来源于stack exchange,提问作者Manish Joshi

