Arham
Arham

Reputation: 2102

Patching a new version of maven artifact jar

I have an ear that contains some jars,

e.g. 
Sample.ear\WEB-INF\lib\org.apache.sling.installer.core-3.3.5-R1242752.jar
Sample.ear\WEB-INF\lib\org.apache.sling.installer.factory.configuration-1.0.2.jar
Sample.ear\WEB-INF\lib\cq-jcrclustersupport-0.1.6.jar
Sample.ear\WEB-INF\lib\com.day.jcr.vault-2.3.24.jar

Now let say I get a new Hotfix.zip that contains the following jars,

Hotfix.zip\lib\org.apache.sling.installer.core-3.4.6-R12345.jar
Hotfiz.zip\lib\org.apache.sling.installer.factory.configuration-2.1.4.jar
Hotfiz.zip\lib\cq-jcrclustersupport-5.6.7.jar

Task - Patch the ear with the new jars.

I have tried the following till now,

  1. Unzip the Ear to a location X
  2. Unzip the Hotfix to a location Y.
  3. Copy the content of Hotfix to Ear.
  4. Zip the ear back.

Issue - I'm stuck at point 3. I want some way to find out if the jar already exists with an older version and then delete it and copy the new one.

Solution - I'm using Groovy because it provides me with the AntBuilder()'s unzip and copy tasks. But any help in terms of Java would also be helpful.

Thought - I'm trying hard to think in terms of regex, but can't figure out a solution.

Note - Please don't go into the details of the jars but just the names.

Upvotes: 0

Views: 161

Answers (1)

tim_yates
tim_yates

Reputation: 171104

Here's a quick and kinda dirty way of doing this...

Lets start with a class to define a library:

class LibVersion implements Comparable {
  String filename
  String library
  String version

  // Just compare the String versions.
  // This is naïve, and should probably be improved
  int compareTo( Object other ) {
    version <=> other.version
  }

  String toString() {
    "$library: $version"
  }

  static fromFilename( String s ) {
    def matcher = ( s =~ $/.+/(.+?)-([0-9\.]+(?:-.+?){0,1})\.jar/$ )[0]
    new LibVersion( filename: s, library: matcher[ 1 ], version: matcher[ 2 ] )
  }
}

Then, given some test data:

def originals = [
  'Sample.ear/WEB-INF/lib/org.apache.sling.installer.core-3.3.5-R1242752.jar',
  'Sample.ear/WEB-INF/lib/org.apache.sling.installer.factory.configuration-1.0.2.jar',
  'Sample.ear/WEB-INF/lib/cq-jcrclustersupport-0.1.6.jar',
  'Sample.ear/WEB-INF/lib/com.day.jcr.vault-2.3.24.jar',
]

def replacements = [
  'Hotfix.zip/lib/org.apache.sling.installer.core-3.2.6-R12345.jar',
  'Hotfiz.zip/lib/org.apache.sling.installer.factory.configuration-2.1.4.jar',
  'Hotfiz.zip/lib/cq-jcrclustersupport-5.6.7.jar',
]

We can generate a map of original libraries, and do the same for the replacements:

Map<String,LibVersion> originalVersions = originals.collectEntries {
  LibVersion.fromFilename( it ).with { v ->
    [ (v.library):v ]
  }
}

Map<String,LibVersion> replacementVersions = replacements.collectEntries {
  LibVersion.fromFilename( it ).with { v ->
    [ (v.library):v ]
  }
}

Then, we can loop through the replacements and print out if the file is new (not found in the original map) or the replacement version is greater than the original

replacementVersions.each { k, v ->
  def orig = originalVersions[ k ]
  if( !orig || orig < v ) {
    println "Should replace $orig with $v"
  }
}

This prints:

Should replace org.apache.sling.installer.factory.configuration: 1.0.2 with org.apache.sling.installer.factory.configuration: 2.1.4
Should replace cq-jcrclustersupport: 0.1.6 with cq-jcrclustersupport: 5.6.7

Upvotes: 1

Related Questions