Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove maven dependency resolver. #566

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ ext.deps = [
],
'klob' : 'com.github.shyiko.klob:klob:0.2.1',
ec4j : 'org.ec4j.core:ec4j-core:0.2.0',
'aether' : [
'api' : "org.eclipse.aether:aether-api:${versions.aether}",
'spi' : "org.eclipse.aether:aether-spi:${versions.aether}",
'util' : "org.eclipse.aether:aether-util:${versions.aether}",
'impl' : "org.eclipse.aether:aether-impl:${versions.aether}",
'connector': [
'basic': "org.eclipse.aether:aether-connector-basic:${versions.aether}",
],
'transport': [
'file': "org.eclipse.aether:aether-transport-file:${versions.aether}",
'http': "org.eclipse.aether:aether-transport-http:${versions.aether}",
]
],
// Required by aether
'guava' : 'com.google.guava:guava:18.0',
// Used to silence aether-transport-http
'slf4j' : 'org.slf4j:slf4j-nop:1.6.2',
'aetherProvider': 'org.apache.maven:maven-aether-provider:3.2.5',
'picocli' : 'info.picocli:picocli:3.9.6',
// Testing libraries
'junit' : 'junit:junit:4.12',
Expand Down
10 changes: 0 additions & 10 deletions ktlint/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ dependencies {
implementation deps.kotlin.stdlib
implementation deps.kotlin.compiler
implementation deps.klob
implementation deps.aether.api
implementation deps.aether.spi
implementation deps.aether.util
implementation deps.aether.impl
implementation deps.aether.connector.basic
implementation deps.aether.transport.file
implementation deps.aether.transport.http
implementation deps.guava
implementation deps.slf4j
implementation deps.aetherProvider
implementation deps.picocli

testImplementation deps.junit
Expand Down
85 changes: 0 additions & 85 deletions ktlint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,91 +73,6 @@
<artifactId>klob</artifactId>
<version>${klob.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>${aether.maven.provider.version}</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-spi</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- maven-aether-provider's transitive dependency -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${aether.maven.provider.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model-builder</artifactId>
<version>${aether.maven.provider.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-repository-metadata</artifactId>
<version>${aether.maven.provider.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-spi</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-file</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-http</artifactId>
<version>${aether.version}</version>
</dependency>
<!-- used to silence aether-transport-http -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
Expand Down
180 changes: 37 additions & 143 deletions ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.pinterest.ktlint.internal.GitPreCommitHookSubCommand
import com.pinterest.ktlint.internal.GitPrePushHookSubCommand
import com.pinterest.ktlint.internal.IntellijIDEAIntegration
import com.pinterest.ktlint.internal.KtlintVersionProvider
import com.pinterest.ktlint.internal.MavenDependencyResolver
import com.pinterest.ktlint.internal.PrintASTSubCommand
import com.pinterest.ktlint.internal.expandTilde
import com.pinterest.ktlint.internal.fileSequence
Expand All @@ -23,6 +22,8 @@ import com.pinterest.ktlint.internal.printHelpOrVersionUsage
import java.io.File
import java.io.IOException
import java.io.PrintStream
import java.net.URL
import java.net.URLClassLoader
import java.net.URLDecoder
import java.nio.file.Paths
import java.util.ArrayList
Expand All @@ -39,12 +40,6 @@ import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread
import kotlin.system.exitProcess
import org.eclipse.aether.RepositoryException
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.repository.RemoteRepository
import org.eclipse.aether.repository.RepositoryPolicy
import org.eclipse.aether.repository.RepositoryPolicy.CHECKSUM_POLICY_IGNORE
import org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_NEVER
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
Expand Down Expand Up @@ -186,35 +181,12 @@ class KtlintCommandLine {
)
private var reporters = ArrayList<String>()

@Deprecated("See https://github.com/pinterest/ktlint/issues/451")
@Option(
names = ["--repository"],
description = [
"An additional Maven repository (Maven Central/JCenter/JitPack are active by default) " +
"(value format: <id>=<url>)"
],
hidden = true
)
private var repositories = ArrayList<String>()

@Option(
names = ["--repository-update", "-U"],
description = ["Check remote repositories for updated snapshots"]
)
private var forceUpdate: Boolean? = null

@Option(
names = ["--ruleset", "-R"],
description = ["A path to a JAR file containing additional ruleset(s)"]
)
private var rulesets = ArrayList<String>()

@Option(
names = ["--skip-classpath-check"],
description = ["Do not check classpath for potential conflicts"]
)
private var skipClasspathCheck: Boolean = false

@Option(
names = ["--stdin"],
description = ["Read file from stdin"]
Expand Down Expand Up @@ -256,10 +228,7 @@ class KtlintCommandLine {
val start = System.currentTimeMillis()

// load 3rd party ruleset(s) (if any)
val dependencyResolver = lazy(LazyThreadSafetyMode.NONE) { buildDependencyResolver() }
if (!rulesets.isEmpty()) {
loadJARs(dependencyResolver, rulesets)
}
if (rulesets.isNotEmpty()) loadJARs(rulesets)

// Detect custom rulesets that have not been moved to the new package
if (ServiceLoader.load(com.github.shyiko.ktlint.core.RuleSetProvider::class.java).any()) {
Expand All @@ -279,7 +248,7 @@ class KtlintCommandLine {
ruleSetProviders.forEach { System.err.println("[DEBUG] Discovered ruleset \"${it.first}\"") }
}
val tripped = AtomicBoolean()
val reporter = loadReporter(dependencyResolver) { tripped.get() }
val reporter = loadReporter() { tripped.get() }
data class LintErrorWithCorrectionInfo(val err: LintError, val corrected: Boolean)
val userData = listOfNotNull(
"android" to android.toString(),
Expand Down Expand Up @@ -376,9 +345,17 @@ class KtlintCommandLine {
}
}

private fun loadReporter(dependencyResolver: Lazy<MavenDependencyResolver>, tripped: () -> Boolean): Reporter {
data class ReporterTemplate(val id: String, val artifact: String?, val config: Map<String, String>, var output: String?)
val tpls = (if (reporters.isEmpty()) listOf("plain") else reporters)
private data class ReporterTemplate(
val id: String,
val artifact: String?,
val config: Map<String, String>,
val output: String?
)

private fun loadReporter(tripped: () -> Boolean): Reporter {
val configuredReporters = if (reporters.isEmpty()) listOf("plain") else reporters

val tpls = configuredReporters
.map { reporter ->
val split = reporter.split(",")
val (reporterId, rawReporterConfig) = split[0].split("?", limit = 2) + listOf("")
Expand All @@ -391,13 +368,15 @@ class KtlintCommandLine {
}
.distinct()
val reporterLoader = ServiceLoader.load(ReporterProvider::class.java)
val reporterProviderById = reporterLoader.associate { it.id to it }.let { map ->
val missingReporters = tpls.filter { !map.containsKey(it.id) }.mapNotNull { it.artifact }.distinct()
if (!missingReporters.isEmpty()) {
loadJARs(dependencyResolver, missingReporters)
reporterLoader.reload()
reporterLoader.associate { it.id to it }
} else map
val reporterProviderById = reporterLoader.associateBy { it.id }
val missingReporters = tpls
.filter { !reporterProviderById.containsKey(it.id) }
.mapNotNull { it.artifact }
.distinct()
if (missingReporters.isNotEmpty()) {
loadJARs(missingReporters)
reporterLoader.reload()
reporterLoader.associateBy { it.id }
}
if (debug) {
reporterProviderById.forEach { (id) -> System.err.println("[DEBUG] Discovered reporter \"$id\"") }
Expand Down Expand Up @@ -503,105 +482,20 @@ class KtlintCommandLine {

private fun <T> List<T>.head(limit: Int) = if (limit == size) this else this.subList(0, limit)

private fun buildDependencyResolver(): MavenDependencyResolver {
val mavenLocal = File(File(System.getProperty("user.home"), ".m2"), "repository")
mavenLocal.mkdirsOrFail()
val dependencyResolver = MavenDependencyResolver(
mavenLocal,
listOf(
RemoteRepository.Builder(
"central", "default", "https://repo1.maven.org/maven2/"
).setSnapshotPolicy(
RepositoryPolicy(
false, UPDATE_POLICY_NEVER,
CHECKSUM_POLICY_IGNORE
)
).build(),
RemoteRepository.Builder(
"bintray", "default", "https://jcenter.bintray.com"
).setSnapshotPolicy(
RepositoryPolicy(
false, UPDATE_POLICY_NEVER,
CHECKSUM_POLICY_IGNORE
)
).build(),
RemoteRepository.Builder(
"jitpack", "default", "https://jitpack.io"
).build()
) + repositories.map { repository ->
val colon = repository.indexOf("=").apply {
if (this == -1) {
throw RuntimeException(
"$repository is not a valid repository entry " +
"(make sure it's provided as <id>=<url>"
)
}
// fixme: isn't going to work on JDK 9
private fun loadJARs(artifacts: List<String>) {
val jarUrls = artifacts
.map {
val artifactFile = File(expandTilde(it))
if (!artifactFile.exists()) {
System.err.println("Error: $it does not exist")
exitProcess(1)
}
val id = repository.substring(0, colon)
val url = repository.substring(colon + 1)
RemoteRepository.Builder(id, "default", url).build()
},
forceUpdate == true
)
if (debug) {
dependencyResolver.setTransferEventListener { e ->
System.err.println(
"[DEBUG] Transfer ${e.type.toString().toLowerCase()} ${e.resource.repositoryUrl}" +
e.resource.resourceName + (e.exception?.let { " (${it.message})" } ?: "")
)
artifactFile.toURI().toURL()
}
}
return dependencyResolver
}

// fixme: isn't going to work on JDK 9
private fun loadJARs(dependencyResolver: Lazy<MavenDependencyResolver>, artifacts: List<String>) {
(ClassLoader.getSystemClassLoader() as java.net.URLClassLoader)
.addURLs(
artifacts.flatMap { artifact ->
if (debug) {
System.err.println("[DEBUG] Resolving $artifact")
}
val result = try {
dependencyResolver.value.resolve(DefaultArtifact(artifact)).map { it.toURI().toURL() }
} catch (e: IllegalArgumentException) {
val file = File(expandTilde(artifact))
if (!file.exists()) {
System.err.println("Error: $artifact does not exist")
exitProcess(1)
}
listOf(file.toURI().toURL())
} catch (e: RepositoryException) {
if (debug) {
e.printStackTrace()
}
System.err.println("Error: $artifact wasn't found")
exitProcess(1)
}
if (debug) {
result.forEach { url -> System.err.println("[DEBUG] Loading $url") }
}
if (!skipClasspathCheck) {
if (result.any { it.toString().substringAfterLast("/").startsWith("ktlint-core-") }) {
System.err.println(
"\"$artifact\" appears to have a runtime/compile dependency on \"ktlint-core\".\n" +
"Please inform the author that \"com.pinterest:ktlint*\" should be marked " +
"compileOnly (Gradle) / provided (Maven).\n" +
"(to suppress this warning use --skip-classpath-check)"
)
}
if (result.any { it.toString().substringAfterLast("/").startsWith("kotlin-stdlib-") }) {
System.err.println(
"\"$artifact\" appears to have a runtime/compile dependency on \"kotlin-stdlib\".\n" +
"Please inform the author that \"org.jetbrains.kotlin:kotlin-stdlib*\" should be marked " +
"compileOnly (Gradle) / provided (Maven).\n" +
"(to suppress this warning use --skip-classpath-check)"
)
}
}
result
}
)
val classLoader = ClassLoader.getSystemClassLoader() as URLClassLoader
classLoader.addURLs(jarUrls)
}

private fun parseQuery(query: String) =
Expand All @@ -618,8 +512,8 @@ class KtlintCommandLine {
map
}

private fun java.net.URLClassLoader.addURLs(url: Iterable<java.net.URL>) {
val method = java.net.URLClassLoader::class.java.getDeclaredMethod("addURL", java.net.URL::class.java)
private fun URLClassLoader.addURLs(url: Iterable<URL>) {
val method = URLClassLoader::class.java.getDeclaredMethod("addURL", URL::class.java)
method.isAccessible = true
url.forEach { method.invoke(this, it) }
}
Expand Down
Loading