0

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.

Matthew J
  • 49
  • 5
  • 1
    Stack Overflow isn't a code writing service. We can help figure out problems with code you've written, but don't wait for us to begin and complete your project for you. I suggest [this answer](http://stackoverflow.com/a/14204577) would be an excellent backbone for your script. – rojo Feb 08 '16 at 02:31
  • Actually, [this answer](http://stackoverflow.com/a/24670484/1683264) is the one I had intended to link. The previous one ain't bad, though. – rojo Feb 08 '16 at 04:04
  • I apologise for my inappropriate post but thank you for the links. – Matthew J Feb 08 '16 at 12:24

1 Answers1

0

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
}
Matthew J
  • 49
  • 5