Add maven dependency on the fly?
已回答
I'm probably going 100% wrong direction but I thought I'd ask anyway. I created a custom run configuration for my plugin, which is based on the Maven run configuration. What I want is when the custom run configuration is executed, check the maven dependencies of the project (which I believe I know how to do) and if a certain dependency is missing, add it on the fly, without modifying the original POM, just for the scope of this execution. Is it even possible?
I suppose I could hack the command line builder, but I don't think there's a way in Maven to pass additional dependencies at the command line.
Any ideas?
请先登录再写评论。
Adding additional library to module dependency is quite risky, maven plugin does not expect that there will be dependencies other than declared in pom, it could lead to unexpected behaviour.
I'd recommend not to add additional implicit libraries to classpath, this is usually not expected, looks like a magic, and behaviour differs from command line. Usually it is contr-intuitive.
Instead, I'd recommend to create quick fix (popup baloon or build tool window quick fix) or create your own run configuration type
Yes, I did create my own run configuration type, but since the projects I intend to run are maven-based, I extended maven run configuration classes. That's why I'm asking how to do this. The reason I don't want to modify the existing POM is because the dependencies I want to add are only required for debugging purposes, they normally don't have to be in the project (unless a developer needs them there for other purposes).
Still trying to find a good solution. At the very least, how can I check before the debugger execution that the current project/module dependencies contain a certain dependency and if not, display a warning? What would be the right place to implement this code? Any example?
You can check validity in com.intellij.execution.configurations.RunConfiguration#checkConfiguration
Thanks, Yann, this should work. One last question is, can I limit the `checkConfiguration` calls to only when the run configuration is executed in the debugger (i.e. when a user clicks on the Debug icon)? Or at least in the checkConfiguration method code is there any way to know that this invocation was the result of a user clicking the Debug button? Because I don't want to run this check every time the run configuration is modified in any other way or even executed with a different runner.
Extend from and use com.intellij.execution.configurations.RunConfigurationBase#checkRunnerSettings
Hi,
@... , I wonder if you could finally manage for adding those maven dependencies on the fly
I am thinking to add plugin options to one of my applications (via Groovy scripts), but it would be fancy to be able to add the plugin own dependencies on the fly
Check the plugin for Apache Camel: https://github.com/camel-tooling/camel-idea-plugin
Basically the idea is to generate a custom POM on the fly and include the dependencies there. Here's the code: https://github.com/camel-tooling/camel-idea-plugin/blob/main/camel-idea-plugin/src/main/java/com/github/cameltooling/idea/runner/debugger/CamelDebuggerPatcher.java#L338
Thank you very much
I will have a look