[ExternalSystem|Sync] new: add maven-like coordinate contribution for Gradle Java libraries

### Issues
  * IDEA-134885 Support substitution of library dependency with module dependency when the module is a part of another Maven or Gradle project

GitOrigin-RevId: de1f852b05bf80b15e9682dfd7338ffbb66a352a
This commit is contained in:
Sergei Vorobyov
2025-02-13 18:49:34 +01:00
committed by intellij-monorepo-bot
parent 4be8b382f4
commit dae918df11
5 changed files with 44 additions and 19 deletions

View File

@@ -1014,6 +1014,7 @@ com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallba
- onSuccess(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId,com.intellij.openapi.externalSystem.model.DataNode):V
com.intellij.openapi.externalSystem.service.project.ExternalSystemCoordinateContributor
- sf:Companion:com.intellij.openapi.externalSystem.service.project.ExternalSystemCoordinateContributor$Companion
- findLibraryCoordinate(com.intellij.openapi.roots.libraries.Library):com.intellij.openapi.externalSystem.model.project.ProjectCoordinate
- findModuleCoordinate(com.intellij.openapi.module.Module):com.intellij.openapi.externalSystem.model.project.ProjectCoordinate
f:com.intellij.openapi.externalSystem.service.project.ExternalSystemCoordinateContributor$Companion
com.intellij.openapi.externalSystem.service.project.ExternalSystemProjectResolver

View File

@@ -3,6 +3,7 @@ package com.intellij.openapi.externalSystem.service.project
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.externalSystem.model.project.ProjectCoordinate
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.libraries.Library
import org.jetbrains.annotations.ApiStatus
/**
@@ -22,6 +23,16 @@ interface ExternalSystemCoordinateContributor {
return null
}
/**
* Finds maven-like artifact coordinates for external library that corresponding to the given library.
*
* @param library is a library to find artifact coordinates of a corresponding external library.
* @return found external artifact coordinates.
*/
fun findLibraryCoordinate(library: Library): ProjectCoordinate? {
return null
}
companion object {
@ApiStatus.Internal

View File

@@ -13,9 +13,7 @@ import org.jetbrains.annotations.ApiStatus
@ApiStatus.Internal
@Service(Service.Level.PROJECT)
@State(name = "ExternalProjectsWorkspace", storages = [Storage(CACHE_FILE)])
class ExternalProjectsWorkspace(
private val project: Project,
) : SimplePersistentStateComponent<ExternalProjectsWorkspace.State>(State()) {
class ExternalProjectsWorkspace : SimplePersistentStateComponent<ExternalProjectsWorkspace.State>(State()) {
class State : BaseState() {
@@ -31,7 +29,7 @@ class ExternalProjectsWorkspace(
if (!Registry.`is`("external.system.substitute.library.dependencies")) {
return ModifiableWorkspaceModel.NOP
}
return ModifiableWorkspaceModelImpl(project, state, modelProvider)
return ModifiableWorkspaceModelImpl(state, modelProvider)
}
companion object {

View File

@@ -1,11 +1,7 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.externalSystem.service.project
import com.intellij.openapi.externalSystem.ExternalSystemManager
import com.intellij.openapi.externalSystem.model.ProjectKeys
import com.intellij.openapi.externalSystem.model.project.ProjectCoordinate
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.LibraryOrderEntry
import com.intellij.openapi.roots.ModifiableRootModel
import com.intellij.openapi.roots.ModuleOrderEntry
@@ -19,7 +15,6 @@ import java.util.*
@ApiStatus.Internal
class ModifiableWorkspaceModelImpl internal constructor(
private val project: Project,
private val state: ExternalProjectsWorkspace.State,
private val modelsProvider: IdeModifiableModelsProvider,
) : ModifiableWorkspaceModel {
@@ -61,16 +56,11 @@ class ModifiableWorkspaceModelImpl internal constructor(
private fun buildLibraryToCoordinateMap(): Map<String, ProjectCoordinate> {
val result = HashMap<String, ProjectCoordinate>()
val projectDataManager = ProjectDataManager.getInstance()
ExternalSystemManager.EP_NAME.forEachExtensionSafe { manager ->
val projectsData = projectDataManager.getExternalProjectsData(project, manager.systemId)
for (projectInfo in projectsData) {
val projectStructure = projectInfo.externalProjectStructure ?: continue
val libraryNodes = ExternalSystemApiUtil.findAll(projectStructure, ProjectKeys.LIBRARY)
for (libraryNode in libraryNodes) {
val libraryData = libraryNode.getData()
result.put(libraryData.internalName, libraryData)
}
ExternalSystemCoordinateContributor.EP_NAME.forEachExtensionSafe { contributor ->
for (library in modelsProvider.allLibraries) {
val libraryName = library.name ?: continue
val libraryCoordinate = contributor.findLibraryCoordinate(library) ?: continue
result.put(libraryName, libraryCoordinate)
}
}
return result

View File

@@ -0,0 +1,25 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.gradle.service.project
import com.intellij.java.library.MavenCoordinates
import com.intellij.java.library.getMavenCoordinates
import com.intellij.openapi.externalSystem.model.project.ProjectCoordinate
import com.intellij.openapi.externalSystem.model.project.ProjectId
import com.intellij.openapi.externalSystem.service.project.ExternalSystemCoordinateContributor
import com.intellij.openapi.roots.libraries.Library
import org.jetbrains.plugins.gradle.util.GradleConstants
class JavaGradleCoordinateContributor: ExternalSystemCoordinateContributor {
override fun findLibraryCoordinate(library: Library): ProjectCoordinate? {
val librarySource = library.externalSource ?: return null
if (librarySource.id != GradleConstants.SYSTEM_ID.id) {
return null
}
return library.getMavenCoordinates()?.toProjectCoordinate()
}
private fun MavenCoordinates.toProjectCoordinate(): ProjectCoordinate {
return ProjectId(groupId, artifactId, version)
}
}