Windows 用于读取 .jar 中 pom.properties 文件的批处理脚本

Windows Batch script to read pom.properties file within .jar

我正在寻找一种简单的方法来读取位于已编译 .jar 的 META-INF 文件夹中的 pom.properties 文件的第二行。 (参见此处:http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR)。我经常需要知道该文件中的日期,每次都必须打开罐子并深入研究,这真是一件痛苦的事。我想要一个 Windows 批处理脚本,我可以通过右键单击 .jar 来 运行(因此我还需要有关 Windows 注册表命令的帮助)。批处理命令的结果可以只显示在 cmd window 中(一个不错的好处是值也被复制到剪贴板)。

简而言之:我希望能够在 Windows Explorer > select 'Get Maven Generated Date'(或其他)中右键单击 .jar 文件 > 并显示第二行pom.properties 文件打印到控制台(并复制到剪贴板)。

我知道这不会太难,我只是不知道要寻找什么 :)。

在此先感谢您的帮助。

请注意,需要 .NETv4.5 才能使用 System.IO.Compression.FileSystem class。

Add-Type -As System.IO.Compression.FileSystem;

$sourceJar = <source-jar-here>;
$jarArchive = [IO.Compression.ZipFile]::OpenRead($sourceJar).Entries
try
{
    foreach($archiveEntry in $jarArchive)
    {
        if($archiveEntry.Name -like "*pom.properties")
        {
            $tempFile = [System.IO.Path]::GetTempFileName()
            try
            {
                [System.IO.Compression.ZipFileExtensions]::ExtractToFile($archiveEntry, $tempFile, $true)
                $mavenDate = Get-Content $tempFile -First 2
                Write-Host $mavenDate
            }
            finally
            {
                Remove-Item $tempFile
            }
        }
    }
}
finally
{
    $jarArchive.Dispose
}