Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using gradle plugin from flatDir source

How to amend Gradle plugins {} management repository for custom plugins? is not duplicated in this post, because it does not cover use of flatDir.


Question

How do I use a Gradle plugin defined in a local JAR, using the new plugin {} semantics, instead of the deprecated apply() semantics?

Current Status

Not having any resolution, after posting the question and searching at considerable length, I filed an issue, wondering whether this use, which ought to be common and straightforward, is unsupported, either by design or oversight, within Gradle's revised plugin semantics.

Unfortunately, my report was closed, with no useful information provided.

I requested clarification in a new issue, but am still waiting.

I am frustrated, having expected that the community would be interested in at least discussing this problem.

If you can contribute information, please do so.

First Update

Following the clarification about the new style for configuring plugin sources, I updated my settings.gradle file to open with the following block. However, I regret that I see no improvement by this change alone. (For the plugin id field referenced in the build.gradle file, I have tried both the global ID published in the JAR metadata, and the basename of the JAR fie. Both fail equally.)

pluginManagement {
    repositories {
        gradlePluginPortal()
        jcenter()
        flatDir {
            dirs 'lib`'
        }
    }
}

The documentation explains how to use custom repositories, but appears to overlook the case of a trivial flat directory.

Second Update

I get some improvement if I add a version number to the JAR file and to the corresponding statement in the plugins {} block. In this case, the message becomes:

Plugin [id: 'plugin-id', version: '1.0.0'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'plugin-id:plugin-id.gradle.plugin:1.0.0')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    BintrayJCenter
    flatDir(/absolute/path/to/lib)

In this case, the directory is added to the list of sources searched.

It is strange that the .gradle.plugin suffix is being appended to my ID in the printed artifact. It is also strange that adding the version number to what is being searched for affects the list of places being searched.

So my project still cannot build. I appreciate any further help.

Original Background

I placed a JAR file containing a custom plugin definition in the lib directory of a project. With the build.gradle build file as below, the build runs successfully.

buildscript {
  repositories {
      flatDir {
          dirs 'lib'
      }
  }
}

apply plugin: 'plugin-id'

However, the apply() semantics are deprecated, favoring a plugins {} block, so I tried updating the build file as below.

plugins {
    id 'plugin-id'
}

repositories {
    flatDir {
        dirs 'lib'
    }
}

I understand that the plugins {} contents can draw from the repositories {} definitions.

However, the change creates a failure:

* What went wrong:
Plugin [id: 'plugin-id'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)

Keeping the plugin {} block but moving the repositories {} block back into a leading buildscript {} block does not resolve the error. That is, the error persists even if I revert to the earlier version only replacing the apply() statement with the plugin {} block.

Finally, it has no effect to add to the repositories {} block a dependencies { classpath: ':jarname' } block, which some sources suggest is necessary, though I don't know why it would be.

like image 466
epl Avatar asked Feb 28 '19 04:02

epl


1 Answers

This works. Tested with gradle 6.3.

build.gradle:

plugins {
    id 'plugin-id'
}

settings.gradle:

pluginManagement {
    buildscript {
        repositories {
            flatDir {
                dirs '/plugin-folder/build/libs'
            }
        }
        dependencies {
            classpath ':plugin-jar:0.0.1'
        }
    }
}

Update: I just found out today that it is possible to have your plugin jar resolved without using the dependencies block above. In that case you should name your plugin jar as [plugin-id].gradle.plugin[-version].jar. Note that the [-version] part is optional and plugin-id.gradle.plugin.jar will also work.

NB: Flat dir repositories are discouraged and local maven repo folder should be used instead. Especially in the case when you want to override locally an artifact which exists on a remote repo. See https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver. It seems impossible to fully move away from maven in favour of gradle, considering that installing local artifacts in a maven repo folder is not supported by gradle itself.

like image 147
Alex Avatar answered Oct 24 '22 23:10

Alex