I’m new into the Gradle world and I want developer my custom plugin for Gradle but I have a problem.
My plugin must communicate with the Database(create database, drop database), so I have need for a JDBC driver, this driver is must into the project that utilizes my plugin.
My question is: How getting a dependency jar for database into my plugin?
I look this post but I did not find this object dependencyNamesSet.
There’s a full README in the download. But I’ll give you the TL;DR here:
The download contains a bare-bones simulation of your gradle-database plugin. There’s an example of a build script that a user of your gradle-database plugin would run. Essentially, my plugin gives your plugin access to your users’ database driver Jar. Users of your plugin will need to do the following in their build scripts:
Configure a dependencies block that includes their required database driver artifact.
Using my plugin, fetch the user’s database driver artifact from Gradle’s dependency cache.
Set the fetched database driver artifact as the value of the appropriate property of your plugin’s task.
With the appropriate property set on your task, your plugin code will therefore have access to the database driver Jar file that my plugin fetched from Gradle’s dependency cache and passed to your task from the build script. In your plugin code, you would use the Jar file however you intend to.
The stripped-down database plugin and build script contained in the download, is a basic working example of the above use case.
Get in touch if I can answer any questions for you.
My project now work and I find the jar but I think that with Gradle is possible to increment the quality for the plugin.
For the moment I resolved the problem with this code
Some question on your plugin, if I understand the function of the your plugin finds the dependence, an example if I want to find the dependencies on some directory your plugin finde the jar, right?
I’m new in the Gradle world and I’m learning this technology for creating my plugins, an example if I want work on the Tomcat server with the version that downloads into tomcat site and not with the Ide version and I will developer my plugin for using a task ant by tomcat api with gradle and if I understood the function your plugin, I think that your plugin interesting.
The user is assumed to have explicitly declared an artifact in a dependency{} block. If they did, and if Gradle successfully resolves the artifact, then the artifact should pretty much always be in one of the above two places for my plugin to find.
Making it configurable to search in a user-specified location, might not be a bad idea though for a future version. Thanks for the tip
The your plugin si very util, if you add the dependence for load jar in another dir is fantastic, an example if I add the my other project and not have creste last versione because is only test with the personal directory your project is very util
Thanks again I intend to give your suggestion some serious consideration.
Meanwhile, download the demo project if you ever have any spare time. Try to think of ways to break the Jar plugin. It needs some real world usage to help it improve its utility.
Yes I already have dowload your project example and in the next weeks I update the my project with your plugin because it help me with improve the quality plugin. Thanks so much
Hey! That would be awesome. Feel free to get in touch if you eventually think of any questions you want to ask about the JarExec plugin.
I thought some more about the Tomcat/IDE/personal directory scenario you described earlier. I might not fully understand your use case. But even in the current version of the JarExec, you could do something like this:
…where 'non.existent:artifact-gav:0.0.0' are fake coordinates of a dummy artifact that you know for sure does not exist. The non-existent artifact will, of course, never be found.
In that case, the helper will instead set dbJar to be the user-specified file() in the orElse() call. In other words, if the helper fails to fetch your first option, it will return a file known to exist at a known location. The default option.
Probably not the precise use case you described earlier. But it might come in handy in other circumstances.
Oh this is a good news for me.
In the my precendent use case, tomcat has a the personal jars, an exampe jasper.jar and for precompile page jsp into my project I have need for the jar for jasper, now is possible two option
Into plugin adding dependence and non included it when i create the war
The plugin usinging a jar into tomcat_dir/lib for compile the jsp page, in this case I didn’t have need for exluded the dependence when I create a war
I not sure why you are messing around with looking up a jar in foreign configurations.
I’ve seen and used lots of usecases, where I was in need of a specific classpath (just imagine the database driver needs of a set of jars to work). Therefor you can easily create your own configuration and let the user define a dependency in it.
Then you won’t lookup a specifically named jar file, but simply resolve the configuration and use it as classpath. Your plugin may create a specific configuration by itself but it’s also totaly fine to let the user do smth like this:
This way a user has to define it’s own configuration, but also has the control on how to name it. The main advantage is to not mix up dependencies and task configuration. Anyone sees clearly what classpath your plugin will use and not forget to update the nameJar property if he updates the dependency. One may even declare a filesystem based repository to resolve only locally available drivers.
Hi,
Thanks for your answer and I have some question for you, with your method I find the dependencies databaseDriver into my task with this code right?
Yeah that would be it if you directly lookup the configuration. While that’s possible you might also wanne create exactly this configuration in your code too, otherwise you depend on a fixed configuration name that might not exist.
The other way shown in my example is to give the configuration (or maybe just the name) as a configureable property to your plugin. This way someone may use your task for different databases with different drivers in one build.gradle using specific configurations for every database.
I’m sorry, but I don’t get what you mean.
Sure the solution I suggested is a little different. After all you should not use/need the method findDependecyFileJarForDriver anyomore but put the whole collection of files in the Classloaders path.
Just some minor hint for your plugin. The Extension you wrote seems to be used very different from what the docs suggest. In https://guides.gradle.org/implementing-gradle-plugins/#capturing_user_input_to_configure_plugin_runtime_behavior the extension is registered and while registering the task a Action is also provided to the gradle API to apply the extensions values to the task. In your plugin the tasks request the projects extension while they are beeing processed.
While it seems to be a minor difference, it will prevent you from reusing your tasks.
Now I don’t understand but I going to read your reference and if have some questions I will write to you.
Thanks for your advise for improving my plugin