Get path relative to $MODULE_DIR$

In the IvyIDEA plugin I now add jars to a module library. I used absolute paths because that was the easiest way to do it, but I would really like to add the dependencies as paths relative to the module dir, much like IntelliJ does it internally when adding jars.

So I would like to add dependencies in the form of: jar://$MODULE_DIR$/../../lib/junit-4.8.2.jar!/
Rather than jar://D:/MyDevProjects/SomeNameThatIsSpecificToMyMachine/Whatever/lib/junit-4.8.2.jar!/

The big problem is that module files can't be shared between users now.

Any idea on the best way to do this?

Thanks!

1 comment
Comment actions Permalink

To answer my own question:

I used to add the dependencies using the URL like:
        libraryModel.addRoot(externalDependency.getUrlForLibrary(intellijModule.getModule()), externalDependency.getType());

Bun now I learned that it's better to create a virtual file and use that when adding the root.
        final VirtualFile vf = VirtualFileManager.getInstance().findFileByUrl("file://" + externalDependency.getLocalFile().getAbsolutePath());        
        libraryModel.addRoot(vf, externalDependency.getType());

This will automatically cause the added dependency to be added relative to the module.

0

Please sign in to leave a comment.