Replace Ivy resolver with Maven resolver in all places

This commit is contained in:
Ilya Muradyan
2021-06-29 14:06:14 +03:00
committed by teamcityserver
parent 014765a302
commit 46bbe5b1cb
6 changed files with 35 additions and 202 deletions
@@ -26,18 +26,17 @@ val relocatedJarContents by configurations.creating
val embedded by configurations
dependencies {
compileOnly("org.apache.ivy:ivy:2.5.0")
compileOnly(project(":compiler:cli-common"))
compileOnly(project(":kotlin-scripting-jvm-host-unshaded"))
compileOnly(project(":kotlin-scripting-dependencies"))
compileOnly(project(":kotlin-scripting-dependencies-maven"))
runtimeOnly(project(":kotlin-scripting-compiler-embeddable"))
runtimeOnly(kotlinStdlib())
runtimeOnly(project(":kotlin-reflect"))
embedded(project(":kotlin-scripting-common")) { isTransitive = false }
embedded(project(":kotlin-scripting-jvm")) { isTransitive = false }
embedded(project(":kotlin-scripting-jvm-host-unshaded")) { isTransitive = false }
embedded(project(":kotlin-scripting-dependencies")) { isTransitive = false }
embedded("org.apache.ivy:ivy:2.5.0")
embedded(project(":kotlin-scripting-dependencies-maven-all"))
embedded("org.slf4j:slf4j-nop:1.7.30")
embedded(commonDep("org.jetbrains.kotlinx:kotlinx-collections-immutable-jvm")) {
isTransitive = false
attributes {
+26 -6
View File
@@ -6,14 +6,14 @@
-keepdirectories META-INF/**
-dontnote **
-dontwarn org.sonatype.aether.**
-dontwarn org.jetbrains.kotlin.**
-dontwarn org.apache.commons.**
-dontwarn com.jcraft.**
-dontwarn org.apache.tools.ant.**
-dontwarn org.apache.oro.text.**
-dontwarn org.bouncycastle.**
-dontwarn org.apache.ivy.ant.**
-dontwarn org.eclipse.sisu.**
-dontwarn org.checkerframework.**
-dontwarn afu.org.checkerframework.**
-dontwarn org.sonatype.plexus.components.**
-dontwarn org.codehaus.plexus.PlexusTestCase
-dontwarn javax.enterprise.inject.**
-dontwarn kotlin.annotations.jvm.**
# hopefully temporarily, for coroutines
-dontwarn kotlin.time.**
@@ -23,3 +23,23 @@
-keep class org.jetbrains.kotlin.script.util.impl.PathUtilKt { *; }
-keep class org.apache.ivy.plugins.** { *; }
-keep class org.eclipse.sisu.** { *; }
-keep class org.jetbrains.kotlin.org.eclipse.sisu.** { *; }
-keep class com.google.inject.** { *; }
-keep class org.jetbrains.kotlin.com.google.inject.** { *; }
-keep class org.jetbrains.kotlin.script.util.impl.PathUtilKt { *; }
-keep class com.google.common.** { *; }
-keep class org.jetbrains.kotlin.com.google.common.** { *; }
-keep class org.apache.maven.wagon.providers.** { *; }
-keep class org.jetbrains.kotlin.org.apache.maven.wagon.providers.** { *; }
-keep class org.slf4j.** { *; }
-keepclassmembers class * extends java.lang.Enum {
<fields>;
**[] values();
}
@@ -1,184 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.mainKts.impl
import org.apache.ivy.Ivy
import org.apache.ivy.core.LogOptions
import org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor
import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor
import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor
import org.apache.ivy.core.module.id.ModuleRevisionId
import org.apache.ivy.core.resolve.ResolveOptions
import org.apache.ivy.core.settings.IvySettings
import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter
import org.apache.ivy.plugins.resolver.ChainResolver
import org.apache.ivy.plugins.resolver.IBiblioResolver
import org.apache.ivy.util.DefaultMessageLogger
import org.apache.ivy.util.Message
import java.io.File
import java.nio.file.Files
import kotlin.script.experimental.api.*
import kotlin.script.experimental.dependencies.ExternalDependenciesResolver
import kotlin.script.experimental.dependencies.RepositoryCoordinates
import kotlin.script.experimental.dependencies.impl.dependencyScopes
import kotlin.script.experimental.dependencies.impl.toRepositoryUrlOrNull
import kotlin.script.experimental.dependencies.impl.transitive
class IvyResolver : ExternalDependenciesResolver {
private fun String?.isValidParam() = this?.isNotBlank() ?: false
override fun acceptsArtifact(artifactCoordinates: String): Boolean = with(artifactCoordinates) {
isValidParam() && count { it == ':' }.let { it == 2 || it == 3 }
}
override fun acceptsRepository(repositoryCoordinates: RepositoryCoordinates): Boolean =
repositoryCoordinates.toRepositoryUrlOrNull() != null
override suspend fun resolve(
artifactCoordinates: String,
options: ExternalDependenciesResolver.Options,
sourceCodeLocation: SourceCode.LocationWithId?
): ResultWithDiagnostics<List<File>> {
val artifactType = artifactCoordinates.substringAfterLast('@', "").trim()
val stringCoordinates = if (artifactType.isNotEmpty()) artifactCoordinates.removeSuffix("@$artifactType") else artifactCoordinates
return if (acceptsArtifact(stringCoordinates)) {
val artifactId = stringCoordinates.split(':')
try {
resolveArtifact(
artifactId[0], artifactId[1], artifactId[2],
if (artifactId.size > 3) artifactId[3] else null,
if (artifactType.isNotEmpty()) artifactType else null,
options,
sourceCodeLocation
)
} catch (e: Exception) {
makeFailureResult(e.asDiagnostics(locationWithId = sourceCodeLocation))
}
} else {
makeFailureResult("Unrecognized set of arguments to ivy resolver: $stringCoordinates", sourceCodeLocation)
}
}
private val ivyResolvers = arrayListOf<IBiblioResolver>()
private fun resolveArtifact(
groupId: String,
artifactName: String,
revision: String,
conf: String? = null,
type: String? = null,
options: ExternalDependenciesResolver.Options,
sourceCodeLocation: SourceCode.LocationWithId? = null
): ResultWithDiagnostics<List<File>> {
if (ivyResolvers.isEmpty() || ivyResolvers.none { it.name == "central" }) {
ivyResolvers.add(
IBiblioResolver().apply {
isM2compatible = true
isUsepoms = true
name = "central"
}
)
}
val ivySettings = IvySettings().apply {
val resolver =
if (ivyResolvers.size == 1) ivyResolvers.first()
else ChainResolver().also {
it.name = "chain"
for (resolver in ivyResolvers) {
it.add(resolver)
}
}
addResolver(resolver)
setDefaultResolver(resolver.name)
}
val ivy = Ivy.newInstance(ivySettings)
val moduleDescriptor = DefaultModuleDescriptor.newDefaultInstance(
ModuleRevisionId.newInstance(groupId, "$artifactName-caller", "working")
)
val depsDescriptor = DefaultDependencyDescriptor(
moduleDescriptor,
ModuleRevisionId.newInstance(groupId, artifactName, conf, revision),
false, false, true
)
if (type != null) {
val depArtifact = DefaultDependencyArtifactDescriptor(depsDescriptor, artifactName, type, type, null, null)
depsDescriptor.addDependencyArtifact(conf, depArtifact)
}
val dependencyScopes = listOf("master") + (options.dependencyScopes ?: listOf("compile"))
depsDescriptor.addDependencyConfiguration("default", dependencyScopes.joinToString(","))
moduleDescriptor.addDependency(depsDescriptor)
val isTransitive = options.transitive != false
val resolveOptions = ResolveOptions().apply {
confs = arrayOf("default")
log = LogOptions.LOG_QUIET
isOutputReport = false
if (!isTransitive) {
this.isTransitive = false
}
}
//init resolve report
// TODO: find out why direct resolving doesn't work
// val report = ivy.resolve(moduleDescriptor, resolveOptions)
//creates an ivy configuration file
val ivyFile = Files.createTempFile("ivy", ".xml").toFile().apply { deleteOnExit() }
XmlModuleDescriptorWriter.write(moduleDescriptor, ivyFile)
val report = ivy.resolve(ivyFile.toURI().toURL(), resolveOptions)
val diagnostics = report.allProblemMessages.map { it.asErrorDiagnostics(locationWithId = sourceCodeLocation) }
return if (report.hasError()) makeFailureResult(diagnostics)
else report.allArtifactsReports.map { it.localFile }.asSuccess(diagnostics)
}
override fun addRepository(
repositoryCoordinates: RepositoryCoordinates,
options: ExternalDependenciesResolver.Options,
sourceCodeLocation: SourceCode.LocationWithId?
): ResultWithDiagnostics<Boolean> {
val url = repositoryCoordinates.toRepositoryUrlOrNull()
?: return false.asSuccess()
val root = url.toExternalForm()
// Check whether this repository was already added
val prevRepoIndex = ivyResolvers.indexOfFirst { it.root == root }
if (prevRepoIndex != -1) {
// If yes, move it to the end of the list.
// It will decrease its resolution priority
val resolver = ivyResolvers[prevRepoIndex]
ivyResolvers.removeAt(prevRepoIndex)
ivyResolvers.add(resolver)
} else {
ivyResolvers.add(
IBiblioResolver().apply {
isM2compatible = true
name = url.host
this.root = root
}
)
}
return true.asSuccess()
}
companion object {
init {
Message.setDefaultLogger(DefaultMessageLogger(1))
}
}
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.mainKts
import org.jetbrains.kotlin.mainKts.impl.Directories
import org.jetbrains.kotlin.mainKts.impl.IvyResolver
import java.io.File
import java.nio.ByteBuffer
import java.security.MessageDigest
@@ -15,6 +14,7 @@ import kotlin.script.dependencies.ScriptDependenciesResolver
import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.*
import kotlin.script.experimental.dependencies.*
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver
import kotlin.script.experimental.host.FileBasedScriptSource
import kotlin.script.experimental.host.FileScriptSource
import kotlin.script.experimental.host.ScriptingHostConfiguration
@@ -101,7 +101,7 @@ fun configureConstructorArgsFromMainArgs(context: ScriptEvaluationConfigurationR
}
class MainKtsConfigurator : RefineScriptCompilationConfigurationHandler {
private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), IvyResolver())
private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver())
override operator fun invoke(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> =
processAnnotations(context)
@@ -21,8 +21,7 @@ dependencies {
testCompile(kotlinStdlib("jdk8"))
testCompile(project(":kotlin-scripting-ide-services-unshaded"))
testCompile(project(":kotlin-scripting-compiler"))
testCompile(project(":kotlin-scripting-dependencies"))
testCompile(project(":kotlin-main-kts"))
testCompile(project(":kotlin-scripting-dependencies-maven"))
testCompile(project(":compiler:cli"))
testRuntimeOnly(project(":kotlin-compiler"))
@@ -32,8 +31,7 @@ dependencies {
embeddableTestRuntime(project(":kotlin-scripting-ide-services", configuration="runtimeElements"))
embeddableTestRuntime(project(":kotlin-scripting-compiler-impl-embeddable", configuration="runtimeElements"))
embeddableTestRuntime(project(":kotlin-scripting-dependencies", configuration="runtimeElements"))
// For tests with IvyResolver
embeddableTestRuntime(project(":kotlin-main-kts"))
embeddableTestRuntime(project(":kotlin-scripting-dependencies-maven-all"))
embeddableTestRuntime(kotlinStdlib("jdk8"))
embeddableTestRuntime(testSourceSet.output)
}
@@ -6,11 +6,11 @@
package org.jetbrains.kotlin.scripting.ide_services.test_util
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.mainKts.impl.IvyResolver
import java.io.File
import kotlin.script.dependencies.ScriptContents
import kotlin.script.experimental.api.*
import kotlin.script.experimental.dependencies.*
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver
import kotlin.script.experimental.jvm.withUpdatedClasspath
// in case of flat or direct resolvers the value should be a direct path or file name of a jar respectively
@@ -22,7 +22,7 @@ annotation class DependsOn(val value: String = "")
open class ScriptDependenciesResolver {
private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), IvyResolver())
private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver())
private val addedClasspath = mutableListOf<File>()
fun resolveFromAnnotations(script: ScriptContents): ResultWithDiagnostics<List<File>> {