Hi there. I have developed a binary plugin and pub...
# community-support
j
Hi there. I have developed a binary plugin and published it on my
mavelLocal()
repo. I now intend to call this plugin for projects, ideally without modifying their
build.gradle
file. Apparently the most reasonable way of doing this is placing the plugin's
apply
in an init file (there was some talk in github about implementing something else to be able to call plugins from the CLI, but as far as I can see it didn't get anywhere) Now, calling it from the target project's
build.gradle
file seems to work fine, but when I try to do
apply plugin: 'org.konveyor.java.plugin:konveyor-download-sources:1.0-SNAPSHOT'
from the init file, it fails. My init file looks like this:
Copy code
buildscript {
    repositories {
        mavenLocal() // Repository where your plugin is hosted
    }
    dependencies {
        classpath 'org.konveyor.java.plugin:konveyor-download-sources:1.0-SNAPSHOT'
    }
}

apply plugin: 'org.konveyor.java.plugin:konveyor-download-sources:1.0-SNAPSHOT'
Any help would be appreciated, thanks!
By the way, the way it fails is
Plugin with id 'org.konveyor.java.plugin:konveyor-download-sources:1.0-SNAPSHOT' not found.
e
apply plugin:
takes a plugin id, not an artifact coordinate
also you're applying the plugin to the init script itself; do you intend for it to be applied to the project, or does your plugin handle that?
j
@ephemient thanks for the info re. the plugin id
I am not sure about your question though, my intention is to be able to apply the plugin I have developed against whatever project might come
What is the plugin supposed to handle? It works so far if I "hardcode it" into whatever project I want to use it in (using it from the build.gradle file)
Do I need to change stuff in case I want to run it from the init file?
It still for some reason doesn't find the plugin...
d
I cannot speak to what your goal is, nor can my light searching find this plug in available, so this is based on assumption of what the plugin itself is
org.konveyor.java.plugin
. But I did get this concept working with an init script before with other plug ins.
Copy code
if (gradle.getParent() == null) {
  beforeSettings { settings ->
    settings.buildscript.repositories { mavenLocal() }
    settings.buildscript.dependencies.add("classpath", "org.konveyor.java.plugin:konveyor-download-sources:1.0-SNAPSHOT")
  }
  settingsEvaluated { settings ->
    if (!settings.pluginManager.hasPlugin("org.konveyor.java.plugin")) {
      settings.pluginManager.apply("org.konveyor.java.plugin")
    }
  }
}
j
Hey @Daniel B Duval, thanks for your reply, yeah it's a plugin I'm developing to be able to fetch the source JARs of gradle projects. I will try that out, thank you!
e
don't the project and settings buildscript classpaths extend the init buildscript classpath?
j
I'm quite ignorant in terms of Gradle, I have barely used it
@ephemient I have no idea
Should that be the case?
I just want to be able to do this without having to modify the project's build.gradle file
If I need to I will, but I'd rather avoid it...
d
@ephemient - the way I understand this is that there’s a desire to have all gradle projects on a machine to this plug in without having it in their build scripts. If I read that right, the above was a solution I put in place for spinning gradle based projects in our cloud dev environment (machines are ephemeral and can be destroyed all the time). This solution brought in the init script to enforce certain behaviors such as apply a plug in like gradle enterprise / devlocity by just using the cloud based prodcut.
e
sure, I just mean it could possibly be
Copy code
buildscript {
    repositories {
        mavenLocal()
    }
    dependencies {
        classpath "..."
    }
}
settingsEvaluated { // or rootProject or allprojects, depending on the desired target(s)
    pluginManager.apply("...")
}
instead of mucking with the child buildscripts, if the classpath is inherited
d
That’s a good way, I guess it’s a question of the goal: Are you trying to enforce that anyone using any machine on any build does this or is it just with the project. As I mentioned, my solution was a way of injecting the gradle enterprise/devlocity plug-in to any gradle project using our cloud engineering environment if they have not added it to their project. It does need to be part of the set process up to where the init script gets copied over to/placed into the needed place for it to work out of the box. Otherwise it’s a repeat each time manually, which may lose any value add in that screnario.
j
@Daniel B Duval thank you man, it looks like its working with your solution 🙂
I'm getting another problem now that I need to investigate
Copy code
Caused by: java.lang.ClassCastException: class org.gradle.initialization.DefaultSettings_Decorated cannot be cast to class org.gradle.api.Project (org.gradle.initialization.DefaultSettings_Decorated and org.gradle.api.Project are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @55f3ddb1)
Not sure if it's caused by the init script
e
well that means you built a project plugin and you're trying to apply it to settings (because that's what Daniel's snippet does)
just change that part to apply to the right target, like what I wrote
d
I should also point out that my example is based on putting it in
$GRADLE_USER_HOME/init.d/
path. Shouldn’t make a difference, but I was trying to just build an environment for anyone and have a set of plug-ins applied by using the set up I had defined.
Now that I look at the docs, perhaps what is in the init script docs is what you want instead - https://docs.gradle.org/current/userguide/init_scripts.html#sec:init_script_plugins
e
that seems outdated; the simpler solution now is
Copy code
dependencyResolutionManagement {
    repositories {
        maven {
            ...
        }
    }
    repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
}
in
settings
j
Should be any difference between placing it in
init.d
or anywhere else and doing
---init-script
?
Still can't find my plugin for some reason... This shouldn't be that complicated
@Daniel B Duval so basically embedding the plugin code in the init script?
I will try that tomorrow, thank you guys for your help 🙏