0

I’m attempting to copy a global maven library to the project level, using the IntelliJ plug-in API.

I tried following the steps at “Creating a Library” at https://plugins.jetbrains.com/docs/intellij/library.html, but that creates a regular library, not a Maven library.

I'm able to create a "regular" library like the slf4j library shown below.

How can I create a "Maven" library (with the blue 'm' icon) in the screenshot below, using the IntelliJ plug-in API?

enter image description here

2
  • I'm not sure I understand your question. If you want to create a maven library, you can do it from intellij. But if you want a library to show as it comes from maven, it will need to be in the maven local cache (and maybe published on a maven repo). Commented Dec 13, 2024 at 16:54
  • I know I can do it using the GUI in IntelliJ. I'm trying to write a plug-in for IntelliJ that accomplishes the same thing. Commented Dec 13, 2024 at 16:55

1 Answer 1

0

Here's the solution I came up with. This is based on code from com.intellij.openapi.roots.ui.configuration.classpath.ChangeLibraryLevelActionBase.doCopy(LibraryEx library)

private void copyGlobalLibraryToProject( Project project, String libraryName ) {
    LibraryTable globalTable = LibraryTablesRegistrar.getInstance().getLibraryTable();
    final LibraryEx sourceLibrary = Objects.requireNonNull( (LibraryEx )globalTable.getLibraryByName( libraryName ) );
    LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
    final LibraryTable.ModifiableModel modifiableModel = libraryTable.getModifiableModel();

    final Set<File> fileToCopy = new LinkedHashSet<>();
    final Map<String, String> copiedFiles = new HashMap<>();
        for (OrderRootType type : OrderRootType.getAllTypes()) {
            for (VirtualFile root : sourceLibrary.getFiles(type)) {
                if (root.isInLocalFileSystem() || root.getFileSystem() instanceof ArchiveFileSystem ) {
                    fileToCopy.add( VfsUtilCore.virtualToIoFile( VfsUtil.getLocalFile(root)));
                }
            }
        }
        fileToCopy.forEach( from -> copiedFiles.put( FileUtil.toSystemIndependentName(from.getAbsolutePath()), FileUtil.toSystemIndependentName(from.getAbsolutePath()) ) );

    final Library copied = modifiableModel.createLibrary( libraryName, sourceLibrary.getKind());
    final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx)copied.getModifiableModel();
    LibraryEditingUtil.copyLibrary(sourceLibrary, copiedFiles, model);

    WriteAction.run(() -> {
        model.commit();
        modifiableModel.commit();
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.