Is it possible to have `java-gradle-plugin` use a ...
# plugin-development
m
Is it possible to have
java-gradle-plugin
use a custom publication? The doc says
Copy code
This automatic configuration happens in a Project.afterEvaluate() block (so at the end of the build configuration phase), and only if these publications haven't already been defined, so it's possible to create and customise them during the earlier stages of build configuration
But no matter what, the "java" component is added and the build fails with
Copy code
Maven publication 'pluginMaven' cannot include multiple components
Can I tell
java-gradle-plugin
to use another component than the
java
one?
a
it's not in the docs, but there's an option to disable the automatic configuration
Copy code
gradlePlugin {
  isAutomatedPublishing = false
}
https://github.com/gradle/gradle/blob/ebfcf97806a2c04b870652374bcbee2c915dac93/platforms/extensibility/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/MavenPluginPublishPlugin.java#L66-L68
šŸ‘€ 1
m
Does it also remove the plugin marker? I want the plugin marker šŸ˜…
Yea, looks like it does
I'm borderline considering writing my own
java-gradle-plugin
a
yes please!
or maybe a
kotlin-gradle-plugin
m
Features I currently use in
java-gradle-plugin
: • generation of the resource file • creation of the plugin marker
What would
kotlin-gradle-plugin
do on top of that?
a
if it helps, you can try disabling the task for the automatic publication
Copy code
tasks.withType<PublishToMavenRepository>().configureEach {
  onlyIf { publication.name != "pluginMaven" }
}
m
Yea but I'd like the marker to use my own publication instead of the default one
I could probably modify the default one (remove artifacts or so) but mutability makes me anxious
a
kotlin-gradle-plugin
would do the same as
kotlin-dsl
, but without the precompiled script plugins. And strongly encourage all of the best practice https://github.com/gradle/gradle/pull/24286
m
Ah yes, I see.
I'd probably dump sam-with-receiver and the set-operator too, just use plain kotlin
a
yeah, I'd prefer not to modify the other tasks/publications too
yeah, I can understand that. Although I like adding sam-with-receiver as a sort of dog fooding. If my plugin's DSL is hard to configure in my plugin, then it's too hard in buildscripts!
m
Yea, especially for Groovy I guess. But I'm hoping declarative Gradle will fix all of that šŸ¤ž
šŸ¤ž 1
v
This might be an XY problem. What is your use-case? Why do you want to user a custom publication, what is wrong with the one created for you / what do you do differently in your custom one?
m
I want to publish the relocated jar by default
v
With
relocated
you probably mean you use the
shadow
plugin to create a fat jar and want to publish that instead?
Are you publishing to Gradle Plugin Portal or to somewhere custom?
m
Publishing to Maven Central
I use R8 to relocate
shadow
cannot relocate the kotlin stdlib
but the setup is relatively similar for publishing purposes
v
Sad, because the
plugin-publish
plugin has built-in support for the
shadow
plugin and will automatically publish that one. Ah, so you just obfuscate your code with R8? Or what is it? Just heard of it in relation to Android builds. If so, why do you do it? Maven Central only accepts open source libs anyway, doesn't it?
m
Ah, so you just obfuscate your code with R8?
Just relocate to avoid classloader issues, no obfuscation. R8 is designed for Android but it works with .class files too
Maven Central only accepts open source libs anyway, doesn't it?
Fun fact, Maven Central doesn't care. You can publish an empty source jar and it's fine
v
Yeah, but it is against the terms of service most probably afair šŸ™‚
Or at least those of OSSRH if not MC itself
m
They have been explicit about it, let me check
v
Oh, I see, thanks, maybe this changed at some point, or I just remember wrongly.
m
Yea, I think the explicitness is a recent addition but closed source has been allowed pretty much forever
v
So you do build a fat jar and use R8 for the relocation, right? Is it anyway a good idea to include the Kotlin stdlib in your plugin? Will that then not cause problems still as Gradle still controls the Kotlin execution environment / Kotlin version used?
m
Or maybe it was against TOS but they never enforced it, IDK
šŸ¤·ā€ā™‚ļø 1
Will that then not cause problems still as Gradle still controls the Kotlin execution environment / Kotlin version used?
That's the thing, I can use Kotlin 2 in my plugin without any fear šŸ™‚
Also I can use common dependencies like asm or what not without any risk of conflict with other plugins
Of course it's more work for the JVM but 🤷
v
For typically conflicting things like ASM, I fully understand, except that then a user can also not use a
constraint
to update the ASM version when you for example use a version that does not support some class file version that the user is needing. I for example did this for the dependency analysis Gradle plugin. That also uses a relocated asm, but at least packaged and depended on as separate artifact, so I could update it without updating the plugin which was not possible for other reasons. But I thought relocating Kotlin stdlib like that will not be sufficient as Gradle still controls which Kotlin version that is used to execute it. Well, as long as it is a Gradle version with Kotlin 1.9 it will probably be able to use the Kotlin 2.0 stdlib because of the one version forward compatibility guarantee, but I'm not sure whether that's really worth the effort and you can still not run then on Gradle version before 8.3.0. šŸ˜„ That's why I personally would never use Kotlin or Groovy to implement a public Gradle plugin, but always just Java and there lowest version supported by the lowest Gradle version I want to support to have best compatibility. šŸ™‚
Regarding
Yea but I'd like the marker to use my own publication instead of the default one
If your custom publication is published to the same coordinates the automatic publication would have, where is the problem? The marker depends on coordinates.
Or just reconfigure the
java
component to have what you want
m
> I fully understand, except that then a user can also no use a
constraint
to update the ASM version when you for example use a version that does not support some class file version that the user is needing. True > I thought relocating Kotlin stdlib like that will not be sufficient as Gradle still controls which Kotlin version that is used to execute it. Apollo has been using more recent kotlin version for several years now, it's working fine. > because of the one version forward compatibility That's at compile time. At runtime, if your plugin uses a K2 symbol, it'll crash (latest example)
FWIW, I'm not happy with this situation, I'd rather much use external packaging and share dependencies as usual but the way the classloaders works at the moment means it's too many headaches for users not overly familiars with this.
If your custom publication is published to the same coordinates the automatic publication would have, where is the problem?
It's mainly an aesthetics problem but not only. Now I have 2 publications overwriting each other. Someone calling
publishAllPublications...
will see a bunch of warnings, potentially use the wrong one, etc...
v
If you disable the tasks for the "wrong" publication, that should not happen, should it?
m
True, still doesn't feel great
v
Or, back to just modifying the
java
component if possible šŸ™‚
m
That works as well.
šŸ‘Œ 1