Custom Gradle Plugin ID not found

前端 未结 6 560
余生分开走
余生分开走 2020-12-28 13:30

I\'m writing a Gradle plugin and I\'m failing to get the apply plugin: command to work in the Gradle script that uses the plugin. I\'m using Gradle 1.1.

相关标签:
6条回答
  • 2020-12-28 13:45

    If you want to implement a plugin into your buildscript, then you have two options.

    Option 1
    apply plugin: YourCustomPluginClassName

    Option 2

    plugins {
        id 'your.custom.plugin.id'
    }
    

    apply plugin: is used when specifying your plugin by its class name (ex. apply plugin: JavaPlugin)
    plugins { } is used when specifying your plugin by its id (ex. plugins { id 'java' })
    See Gradle Plugins by tutorialspoint for reference

    If you choose Option 1, the your custom plugin will need to be brought into your build script by 1 of 3 ways.
    1. You can code it directly within your Gradle build script.
    2. You can put it under buildSrc (ex. buildSrc/src/main/groovy/MyCustomPlugin).
    3. You can import your custom plugin as a jar in your buildscript method.
    See Gradle Goodness by Mr. Haki for information about the buildscript method.

    If you choose Option 2, then you need to create a plugin id. Create the following file buildSrc/src/main/resources/META-INF/gradle-plugins/[desired.plugin.id].properties. Copy and paste implementation-class=desired.plugin.id into your newly created .properties file. Replace desired.plugin.id with your desired plugin id.
    See Custom Plugins by Gradle for more info.

    0 讨论(0)
  • 2020-12-28 13:46

    The plugin Jar has to be added as a build script dependency:

    buildscript {
        repositories { flatDir name: 'libs', dirs: "../build/libs" }
        dependencies { classpath 'test:test-plugin:0.1' }
    }
    
    apply plugin: "test-plugin"
    
    0 讨论(0)
  • 2020-12-28 13:47

    Adding to what @Bonifacio2 wrote this is a path META-INF/gradle-plugins and shows in IntelliJ as META-INF.gradle-plugins. At all costs don't make the stupid mistake I did creating this directly as a directory META-INF.gradle-plugins because you are based on another sample, and never works. Another tip is copying also from another intelliJ project as this is what is added: gradle-plugins.

    0 讨论(0)
  • 2020-12-28 13:51

    I also had the same problem with a custom plugin id not being found. In my case, I simply forgot to add the 'src/main/resources/META-INF/gradle-plugins' properties file. The name of the properties file must match the name of the plugin id with a '.properties' extension.

    The file must contain a the line:

    implementation-class=(your fully qualified plugin classpath)
    

    That's the complete mechanism on how plugin id's get resolved to class names.

    In addition the plugin needs to be added as a dependency as pointed out in the previous answer. The android documentation states that you should use a name associated with your unique domain name. I.e.: the name 'test-plugin' is not really in good form, but an id like 'com.foo.gradle.test-plugin' would be better.

    0 讨论(0)
  • 2020-12-28 13:52

    Ensure that your top-level build.gradle uses the correct classpath to refer to the path to the built *.jar file.

    Some plugins, like maven-publish, will build and save the jar to a specific location in mavenLocal, but the path may not be clear to see.

    You should look at the file path of the jar file, and ensure it matches your classpath, but the mapping is not immediately obvious:

    buildscript {
      dependencies {
        // This *MUST* match the local file path of the jar file in mavenLocal, which is:
        // ~/.m2/repository/com/company/product/plugin/product-gradle-plugin/1.0/product-gradle-plugin-1.0.jar
        classpath 'com.company.product.plugin:product-gradle-plugin:1.0'
      }
    }
    

    Be careful not to use the wrong classpath, which can refer to a directory instead of the actual jar file; like this:

    buildscript {
      dependencies {
        // This is wrong, because it refers to the mavenLocal FOLDER "product-gradle-plugin",
        // instead of the jar FILE "product-gradle-plugin-1.0.jar"
        // However, a gradle sync will still resolve it as a valid classpath!!
        classpath 'com.company.product:product-gradle-plugin:1.0'
      }
    }
    

    More info:

    • https://discuss.gradle.org/t/what-is-the-preferred-gradle-approach-to-locally-install-an-artifact-equivalent-to-mavens-install/5592
    • https://docs.gradle.org/current/userguide/publishing_maven.html
    • https://blog.codefx.org/tools/snapshots-gradle-maven-publish-plugin/
    • https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project
    0 讨论(0)
  • 2020-12-28 13:56

    hmm perhaps try;

        configure <org.jsonschema2pojo.gradle.JsonSchemaExtension> {
         this.sourceFiles = files("${project.rootDir}/schemas")
        }
    
    0 讨论(0)
提交回复
热议问题