Search code examples
windowsmavenbatch-filejarregistry

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


I'm looking for a simple way to read the 2nd line of the pom.properties file that is placed within the META-INF folder of a compiled .jar. (see here: http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR). I often need to know the date in that file and it's just a pain to have to open the jar every time and dig down into it. I want a Windows batch script that I can run via right-clicking on a .jar (so I'll need help with the Windows registry command as well). The result of the batch command can just be displayed in a cmd window (a nice bonus would be the value being copied to the clipboard, too).

In short: I want to be able to right-click on a .jar file in Windows Explorer > select 'Get Maven Generated Date' (or whatever) > and have the 2nd line of the pom.properties file printed to the console (and copied to the clipboard).

I know this can't be too hard, I just don't know quite what to look for :).

Thanks in advance for any help.


Solution

  • Note that .NETv4.5 is required to use the 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
    }