Programmatically add directory based dependency/library to module

已回答

The framework I'm writing a plugin for has a specific directory defined for "user" libraries, which can contain binary, source and javadoc JARs. I want my plugin to automatically add this directory as a directory based dependency/library to a module (or as a project library and then that library as a module dependency).

I've tried to find some example of this in the IDEA source code, but everything I'm finding it is heavily intertwined with GUI code and I can't find the point where you just say "create a dependency/library from this directory and add it to the module".

Can you provide some information on how to do such? Ultimately it will run from either AnAction, upon project open, or upon adding my Facet. So I have a handle on the module in all cases.

 

1
Avatar
Permanently deleted user

I was able to determine how to do this. For the reference of others, here is the code I came up with.

Creating and Attaching a Directory Based Library

private void attachDirBasedLibrary(@NotNull final Module module, 
@NotNull final String libName,
@NotNull final String dir)
{
ApplicationManager.getApplication().runWriteAction(() ->
{
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
final String urlString = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, dir);
final VirtualFile dirVirtualFile = VirtualFileManager.getInstance().findFileByUrl(urlString);
if (dirVirtualFile != null)
{
final ModifiableRootModel modifiableModel = rootManager.getModifiableModel();
final Library newLib = createDirLib(libName, dirVirtualFile, modifiableModel.getModuleLibraryTable());
modifiableModel.commit();
}
});
}


private Library createDirLib(@NotNull final String libName,
@NotNull final VirtualFile dirVirtualFile,
@NotNull final LibraryTable table)
{
Library library = table.getLibraryByName(libName);
if (library == null)
{
library = table.createLibrary(libName);

Library.ModifiableModel libraryModel = library.getModifiableModel();
libraryModel.addJarDirectory(dirVirtualFile, true, OrderRootType.CLASSES);
libraryModel.addJarDirectory(dirVirtualFile, true, OrderRootType.SOURCES);
libraryModel.addJarDirectory(dirVirtualFile, true, JavadocOrderRootType.getInstance());
libraryModel.commit();

}
return library;
}
 

Creating and Attaching a JAR based Library

private void attachJarLibrary(@NotNull final Module module, 
@NotNull final String libName,
@NotNull final String jarFilePath,
@NotNull final String srcFilePath,
@NotNull final String docFilePath)
{
ApplicationManager.getApplication().runWriteAction(() ->
{
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);

final String clzUrlString = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, jarFilePath) + JarFileSystem.JAR_SEPARATOR;
final String srcUrlString = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, srcFilePath) + JarFileSystem.JAR_SEPARATOR;
final String docUrlString = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, docFilePath) + JarFileSystem.JAR_SEPARATOR;

final VirtualFile jarVirtualFile = VirtualFileManager.getInstance().findFileByUrl(clzUrlString);
final VirtualFile srcVirtualFile = VirtualFileManager.getInstance().findFileByUrl(srcUrlString);
final VirtualFile docVirtualFile = VirtualFileManager.getInstance().findFileByUrl(docUrlString);

final ModifiableRootModel modifiableModel = rootManager.getModifiableModel();
final Library newLib = createJarLib(libName, modifiableModel.getModuleLibraryTable(), jarVirtualFile, srcVirtualFile, docVirtualFile);
modifiableModel.commit();
});
}


private Library createJarLib(@NotNull final String libName,
@NotNull final LibraryTable table,
@Nullable final VirtualFile jarVirtualFile,
@Nullable final VirtualFile srcVirtualFile,
@Nullable final VirtualFile docVirtualFile)
{
Library library = table.getLibraryByName(libName);
if (library == null)
{
library = table.createLibrary(libName);
Library.ModifiableModel libraryModel = library.getModifiableModel();

if (jarVirtualFile != null)
{
libraryModel.addRoot(jarVirtualFile, OrderRootType.CLASSES);
}

if (srcVirtualFile != null)
{
libraryModel.addRoot(srcVirtualFile, OrderRootType.SOURCES);
}

if (docVirtualFile != null)
{
libraryModel.addRoot(docVirtualFile, JavadocOrderRootType.getInstance());
}

libraryModel.commit();
}
return library;
}
 

0

Hi Mark Vedder,

I am developing the same plugin where i am first downloading jar library to local project folder and then i am attaching that jar to modules.
I am able to see the jar file in project root folder and also able to see the jar in projectStructure ->modules->dependencies tab 
But the packages/classes which are in jar are not reflecting to java code.

Pleasee help me in resolving this issue. 

If i remove the jar from modules dependencies tab and again if i add the jar manually i am able to see the changes in java class.

Please help me in solving thins issue.

Thanks,
Sai Nikhil

0

请先登录再写评论。