groovygroovy-grape

Groovy's Ivy cache (@Grab) not caching?


I'm running this simply groovy script using groovyConsole (version 1.8.1):

println "Start " + new Date()
@Grab( 'log4j:log4j:1.2.16' )
import org.apache.log4j.Logger
println "End " + new Date()

Usually, it executes very quickly (~ 0 seconds).

However, sometimes (once every ~5 runs), it pauses for 3-5 seconds before completing the run. I'm sniffing with Wireshark (here is the capture), and see HTTP requests to repository.codehaus.org (I see some 404 responses in the Wireshark stream, but the script manages to run, so evidently the jars are found at some point)

My question is - once the script runs once, aren't the jars it download via @Grab cached forever? Why is the actual Ivy/Maven repository queried so often?


Solution

  • The files should be cached in ~/.groovy/grapes. If you rerun your script with -Divy.message.logger.level=4, you'll get some debug information from ivy that may be helpful.

    Additionally, if you have a grape that takes a long time to resolve, you can tell ivy to check less often. Copy the default ivy config from here to ~/.groovy/grapeConfig.xml and add the property ivy.cache.ttl.default to some length of time to wait before checking again. For example:

    <ivysettings>
      <property name="ivy.cache.ttl.default" value="24h"/>
      <settings defaultResolver="downloadGrapes"/>
      <resolvers>
        <chain name="downloadGrapes" returnFirst="true">
          <filesystem name="cachedGrapes">
            <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
            <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/>
          </filesystem>
          <ibiblio name="localm2" root="file:${user.home}/.m2/repository/" checkmodified="true" changingPattern=".*" changingMatcher="regexp" m2compatible="true"/>
          <!-- todo add 'endorsed groovy extensions' resolver here -->
          <ibiblio name="codehaus" root="http://repository.codehaus.org/" m2compatible="true"/>
          <ibiblio name="ibiblio" m2compatible="true"/>
          <ibiblio name="java.net2" root="http://download.java.net/maven/2/" m2compatible="true"/>
        </chain>
      </resolvers>
    </ivysettings>