Does IntelliJ Read Dependencies Added Via Maven Extensions?
已回答
I have a Maven extension (not plugin) that programmatically adds dependency information to certain Maven projects at the start of the lifecycle. I have made a short dummy example to demonstrate this behavior (this is not the actual code):
public class AddDependency extends AbstractMavenLifecycleParticipant {
@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
List<MavenProject> projects = session.getProjects();
for (MavenProject project : projects) {
Dependency dependency = new Dependency();
dependency.setGroupId("com.example");
dependency.setArtifactId("example-artifact");
dependency.setVersion("1.0.0");
dependency.setScope("compile");
project.getDependencies().add(dependency);
}
}
}
This extension works as expected and the Maven build passes with the expected dependencies but when I import the Maven project into IntelliJ it does not add these dependencies into the Intellij project and therefore some code doesn't work inside the the IDE. Is this because IntelliJ doesn't execute the Maven lifecycle when syncing a Maven project? Is this a bug I should report? Does IntelliJ recognize dependencies that are programmatically added via an extension?
请先登录再写评论。
IDE Maven import considers extensions.xml. But for such kind of configuration it does not work. For such support please file a feature request at https://youtrack.jetbrains.com/issues/IDEA with a sample project. Thank you.
Interesting. When I move it to extensions.xml IntelliJ does pick up the dependencies I define programmatically. It can be a bit flaky but it works most of the time. When it does flake, I have to delete the Intellij project and start a fresh import for it to be fixed. I wonder if its just a coincidence that it works.
I've created the issue as requested
https://youtrack.jetbrains.com/issue/IDEA-288322
Andrey Dernov: I am running into something similar, would you be able to point me to documentation/code that can help understand the configurations that IntelliJ maven importer supports (or doesnt support)?
For context my usecase is to be able to resolve artifacts from CodeArtifact. Which requires engineers to export an environment var every 12 hours. Harder to set with IDEA in general and adds friction.
Maven Extension : http://github.com/indyaah/codeartifact-maven-extension
Anuj Patel how do you configure this in your project and how do you use it? Also what exactly is not working in IDE that works in the command line? Thanks.
Thanks for responding Andrey Dernov, this extension is configured in maven project via .mvn/extensions.xml.
The extension listens for maven execution event and injects codeartifact token into maven reactor (by changing password for servers that are configured with AWS codeartifact URL)
When invoked from CLI the extension does it's thing (inject password); which allows maven to get artifacts from codeartifact but when importing projects into IntelliJ the event listener doesnt get invoked.
There is a single class in the extension that does everything: https://github.com/indyaah/codeartifact-maven-extension/blob/main/src/main/java/com/github/indyaah/coreartifact/maven/CodeArtifactTokenInjectingSpy.java
Could be that this is not supported. Is it truggered only on Maven build goal? It would be nice if you could provide a sample project with an opened YouTrack issue to check this (if possible).
As a workaround I guess it should work to assign any Maven goal (which would trigger this extension) as a Maven trigger before IDE build.
I did some more experiments and found out it works perfectly fine if I enable Delegate IDE build/run actions to Maven under Maven settings.
Is there any place I can read about how IntelliJ maven integration actually works? May be the intellij maven importer is different than standard maven CLI worklflow?
With Delegate IDE build/run actions to Maven enabled, IDE uses Maven goal (clean install) to build the project. So this should indeed trigger the extension. Without this option - IDE uses own builder which is most likely will not consider this extension to execute.