Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

kotlin - Apply local jar-plugin without using maven

I'd like to load my custom plugin from a local jar. The jar file compiles fine and when I check it, the manifest and the plugin class are there.

gradlePlugin {
    plugins {
        create("asdf") { // <-- I really call it "asdf" in the kts script
            id = "asdf"
            implementationClass = "pluginTest.TestPlugin"
            version = "1.4.0"
        }
    }
}

The plugin doesn't do anything useful yet as it should be a proof-of-concept to make sure it actually works at all:

class TestPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        println("Hallo TestPlugin!")
    }
}

I then try to use it like this in another project:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath(files("..\..\path\to\pluginTest.jar"))
    }
}

plugins {
    id("asdf") version "1.4.0"
}

but it keeps telling me that:

Plugin [id: 'asdf', version: '1.4.0'] was not found in any of the following sources:

What am I missing here? I use Gradle v6.5.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you have the plugin jar on the classpath, you can't have a version number in the plugin application. I guess this is because you can't have multiple jars with different versions on the classpath in the first place, so specifying a version here doesn't make any sense (except perhaps to validate that you are using the correct one). This won't fix the problem, but it is a start.

To be honest, I don't know why your approach still won't work. The buildscript block is supposed to set up dependencies for that particular script, and that should make the plugin visible to it. It doesn't for some reason.

Perhaps this is a bug or perhaps this is just an undocumented limitation on the use of the plugin {} block. Maybe you could ask over at the Gradle forums or create an issue for it. However, there are workarounds that don't involve publishing to a (local) Maven repository, which I agree can be a bit annoying.

If you use "apply from" instead of "plugins {}", it works. For some reason, the former can see the buildscript classpath whereas the latter can't:

// build.gradle (Groovy DSL)
buildscript {
    dependencies {
        classpath(files("..\..\path\to\pluginTest.jar"))
    }
}

apply from: "asdf"

Alternatively, move the buildscript plugin from the build.gradle file to the settings.gradle file. This makes is available to the entire build classpath and will make it work with the plugin block:

// settings.gradle (Groovy DSL):
buildscript {
    dependencies {
        classpath(files("..\..\path\to\pluginTest.jar"))
    }
}

// build.gradle (Groovy DSL)
plugins {
    id("asdf")
}

Lastly, just in case you haven't considered it already, you may be able to add the plugin as a composite build. This will create a source dependency to the plugin and has the advantage that transitive dependencies will be carried over (the ones you put in the plugin's own dependency block) and that it will be built automatically if not up-to-date. I use this approach for integration testing my plugins and also sometimes to apply them to my other real projects to test them in a bigger setting before publishing new versions.

Do that with either:

// settings.gradle (Groovy DSL):
includeBuild("..\..\path\to\plugin")

// build.gradle (Groovy DSL):
plugins {
    id("asdf")
}

Or without hard-coding it in the build (so you can dynamically switch between local and published versions):

// build.gradle (Groovy DSL):
plugins {
    id("asdf") version "1.4.0" // Version is optional (will be ignored when the command line switch below)
}

// Run with:
./gradlew --include-build "..\..\path\to\plugin" build

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...