Execute main class from compileOnly dependency

I added a compileOnly dependency from mavenCentral. Given that I know the main class which I want to execute, how do I do it with the groovy dsl?

Also, when will the jar be downloaded and how to get its path?

Can you elaborate a bit more?
Why do you want to execute a main class from a compileOnly dependency?
What is your actual use-case?

In one subproject I want to write an xjc plugin, but by using the moxy included in eclipselink 4.0.x.

In another subproject I want to use the compiled plugin.

And at all times to use just eclipselink, not other things.

I know there is an xjc plugin, but is it up to date? Even if it is, I couldn’t figure out how to instruct it to use eclipselink.

I’m not sure I fully understood, so sorry if I got it wrong.
Are you saying you have a multi-project build with subprojects “xcj-gradle-plugin” and “project-that-wants-to-use-that-plugin”?
That would be a hen-and-egg situation.
You cannot build a Gradle plugin in a build and use it in the same build.
You could use a composite build though, so that you develop the plugin in one build, include that build in the other build and use it there, or develop the plugin in buildSrc which is quite similar to that.

But even then, assuming I got you right, it does not make much sense to have a compileOnly dependency on something you need at runtime. It would probably then be a normal implementation dependency and then you can just use the class you need.

Yes, that’s the concept I was missing: composite builds.

Yes, I was aware of chicken-egg, I was confused, but I hoped gradle would do some magic I’m not aware of.

Now it all makes sense.

1 Like

So that helped with half of the problem and I like the solution so far.

I do have the second half: I want to specify eclipselink as a dependency and execute the java main class of moxy from the downloaded jar.

The build naturally has to be reproducible and the moxy used has to be the one provided by eclipselink.

This is important because other tasks not mentioned yet will be generating JPA classes, which will also have to be done with the same eclipselink.

What I do have: executing manually jaxb-compiler (from the binary eclipselink distribution) with the right parameters and including my own plugin works.

I would need at least some pointers in the right direction. Thanks.

dependencies {
    implementation("org.eclipse.persistence:eclipselink:4.0.2")
}
val doMoxy by tasks.registering(JavaExec::class) {
    setClasspath(configurations.runtimeClasspath.get())
    mainClass = "org.eclipse.persistence.jaxb.xjc.MOXyXJC"
    args("-version")
}