How to add a Jar as a dependency via Gradle Plugin

I am writing a Gradle Plugin where I need to set 1 or more Jar files as dependencies for the project that has my plugin activated.

I know is can do it in a build.gradle file like

implementation files('/path/to/my/lib.jar')

But if I add the following in my plugin

Dependency dep = project.getDependencies().create("files(\"" + jarLocation + "\")");
project.getDependencies().add("compile", dep);

I get the following error message when build the project, so I am guessing there must be another way to add jar files as dependencies though plugins

* What went wrong:
A problem occurred evaluating root project 'GradleJavaPackage'.
> Failed to apply plugin 'archipelago'.
   > Supplied String module notation 'files("/mnt/c/Users/accou/workspace/private/Archipelago/Archipelago.Gradle/maui/build/libs/maui-1.0.jar")' is invalid. Example notations: 'org.gradle:gradle-core:2.2', 'org.mockito:mockito-core:1.9.5:javadoc'.

files('/path/to/my/lib.jar') as a String is not a valid dependency notation. When using implementation files('...') you are adding a FileCollection to the configuration (the Project.files(...) method returns a FileCollection).

You do not need to create a Dependency object, just create a FileCollection and pass it to dependencies.add. Something like:

project.getDependencies().add( "implementation", project.files( jarLocation ) )

*Note: I didn’t test this.