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?

0

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

0

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).

 

0

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?

 

0

You can check validity in com.intellij.execution.configurations.RunConfiguration#checkConfiguration

0

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.

 

0

Extend from and use com.intellij.execution.configurations.RunConfigurationBase#checkRunnerSettings

0

请先登录再写评论。