What is the best way to resolve the physical location of a declared dependency?

Hi,

When running my tests I have to declare a Java agent (for EclipseLink). I would however prefer not to hard-code the JAR reference but rather resolve the physical location of an already declared dependency?

test {

jvmArgs ‘-javaagent:/path/to/spring-instrument-3.1.0.RELEASE.jar’

}

I see that you can parse the locations using:

configurations.compile.files.each { file ->

println “$file.name -> $file.path”

}

But I was wondering, is there something more elegant I should use?

Thanks!

Cheers,

Gunnar

You’ll want something like:

configurations {
  testAgent
  // or:
  testAgent.extendsFrom testRuntime
}
  dependencies {
  testAgent "org.springframework:spring-instrument:3.1.0"
}
  // defer resolution until execution time
test.doFirst {
  jvmArgs "-javaagent:${configurations.testAgent.singleFile}"
}

Thanks! What about when the dependency resolves to multiple files? This seems to resolve:

jvmArgs “-javaagent:${configurations.javaAgentOpenJpa.files.iterator().next()}”

For some strange reason I am running into classpath issues. Shouldn’t jvmArgs have access to all my transitive dependencies?

‘iterator().next()’ gives just the first file. You’ll want something like ‘configurations.javaAgentOpenJpa.asPath’, depending on the exact syntax expected by the ‘-javaagent’ switch.

Hi Thanks for all your help! While the javaagent is being invoked now I still seem to have a Gradle issue for which I have no explanation.

In Eclipse (Project imported using STS) I run all my tests successfully. I have tests that run using OpenJPA, EclipseLink and Hibernate. In Eclipse I provide the following VM arguments to allow for runtime intrumentation of my JPA classes.

-javaagent:/path/to/lib/spring-instrument-3.1.0.RELEASE.jar

-javaagent:/path/to/apache-openjpa-2.2.0/openjpa-all-2.2.0.jar

This works perfectly in Eclipse. However, I don’t seem to get the same to work with plain Gradle. I am getting:

<openjpa-2.2.0-r422266:1244990 fatal user error> org.apache.openjpa.util.MetaDataException: The type “class org.springframework.integration.jpa.test.entity.StudentReadStatus” has not been enhanced.

My gradle script:

test {

compileTestJava.options.compilerArgs = ["${xLintArg},-path"]

String argsString1 = “-javaagent:${configurations.javaAgentSpringInstrument.asPath}”

String argsString2 = “-javaagent:${configurations.javaAgentOpenJpa.files.iterator().next()}”

println(argsString1)

println(argsString2)

jvmArgs argsString1 , argsString2

}

The paths are resolved correctly. Is there anything else I need to consider in regards to running javaagents?

Please fix the formatting or repost. ‘compileTestJava’ shouldn’t be configured inside ‘test’. Instead of ‘files.iterator().next()’, you should use ‘singleFile’. You also forgot the ‘doFirst’, which will cause problems. Make sure you try with rc-1.

Hi,

Well, I better don’t write Markdown at 2am in the morning…

Anyway, upgrading to Gradle 1.0-rc-1 fixes my issues. I wonder what was going on, it looks like the classpath was not resolved correctly. Anyway, it is working now. Regarding your recommendation for using configurations.javaAgentOpenJpa.singleFile: That causes the following error:

Expected configuration ‘:spring-integration-jpa:javaAgentOpenJpa’ to contain exactly one file, however, it contains 10 files.

I only need the first file in the collection, which resolves to /path/to/openjpa-2.2.0.jar. Since you discourage using configurations.javaAgentOpenJpa.files.iterator().next(), what shall I use instead?

In in the interim, the following configuration using Gradle 1.0-rc-1 works now:

configurations {

javaAgentSpringInstrument

javaAgentOpenJpa

}

dependencies {

javaAgentSpringInstrument “org.springframework:spring-instrument:$springVersion”

javaAgentOpenJpa “org.apache.openjpa:openjpa:2.2.0”

}

test.doFirst {

String jvmArgsSpringIntrument = “-javaagent:${configurations.javaAgentSpringInstrument.asPath}”

String jvmArgsOpenJpa

= “-javaagent:${configurations.javaAgentOpenJpa.files.iterator().next()}”

jvmArgs jvmArgsSpringIntrument , jvmArgsOpenJpa

}

Thanks a lot!

Cheers,

Gunnar

rc-1 contains a fix regarding Java agents. If you only need the first file, a cleaner solution would be to make sure that the configuration resolves to just that file. Otherwise, you’ll have to stick to your approach.