You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Ant生成带文件名的SHA-256校验和?求Maven替代方案

Ant 解决方案

Ant 的 checksum 原生任务默认仅生成单个校验码文件,没有直接生成带文件名汇总文件的选项,但可以通过组合任务实现:

方案1:调用系统 sha256sum 命令

直接用 <exec> 任务调用 Linux 原生命令,一步生成符合要求的汇总文件:

<target name="generate_checksum">
    <exec executable="sha256sum" output="/home/myusername/temp/checksum/sha256sums.txt">
        <arg line="/home/myusername/temp/checksum/*.jar"/>
    </exec>
</target>

方案2:组合 checksum 与 concat 任务

如果需要跨平台兼容(不依赖系统命令),可以先生成单个校验文件,再拼接校验码和文件名到汇总文件:

<target name="generate_checksum">
    <!-- 生成单个校验文件 -->
    <checksum algorithm="SHA-256" fileext=".sha256">
        <fileset dir="/home/myusername/temp/checksum">
            <include name="*.jar"/>
        </fileset>
    </checksum>
    
    <!-- 拼接汇总文件 -->
    <concat destfile="/home/myusername/temp/checksum/sha256sums.txt">
        <fileset dir="/home/myusername/temp/checksum" includes="*.jar">
            <mapper type="merge" to="@{file}.sha256"/>
        </fileset>
        <!-- 为每个校验码追加文件名 -->
        <filterchain>
            <tokenfilter>
                <filetokenizer/>
                <replaceregex pattern="^(.+)$" replace="$1  ${filename}" flags="g"/>
            </tokenfilter>
        </filterchain>
    </concat>
</target>
Maven 替代方案

方案1:使用 exec-maven-plugin 调用系统命令

类似 Ant 的 exec 方式,直接调用 sha256sum 生成汇总文件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>generate-sha256sum</id>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>sha256sum</executable>
                <arguments>
                    <argument>${project.build.directory}/*.jar</argument>
                </arguments>
                <outputFile>${project.build.directory}/sha256sums.txt</outputFile>
            </configuration>
        </execution>
    </executions>
</plugin>

方案2:使用 maven-checksum-plugin 生成带文件名的校验文件

该插件支持生成符合 sha256sum 格式的汇总文件:

<plugin>
    <groupId>net.sf.maven-checksum</groupId>
    <artifactId>maven-checksum-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <id>generate-sha256</id>
            <phase>package</phase>
            <goals>
                <goal>files</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileSet>
                        <directory>${project.build.directory}</directory>
                        <includes>
                            <include>*.jar</include>
                        </includes>
                    </fileSet>
                </fileSets>
                <algorithms>
                    <algorithm>SHA-256</algorithm>
                </algorithms>
                <!-- 生成汇总文件,格式与 sha256sum 一致 -->
                <aggregate>true</aggregate>
                <aggregateFilename>sha256sums.txt</aggregateFilename>
            </configuration>
        </execution>
    </executions>
</plugin>

内容的提问来源于stack exchange,提问作者R D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 18:35:25