diff --git a/build.gradle b/build.gradle index 7f6c4eaf4e..744ab9934b 100644 --- a/build.gradle +++ b/build.gradle @@ -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', diff --git a/ktlint/build.gradle b/ktlint/build.gradle index 1503785313..03b13f5949 100644 --- a/ktlint/build.gradle +++ b/ktlint/build.gradle @@ -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 diff --git a/ktlint/pom.xml b/ktlint/pom.xml index 9c5094d17b..51216aed95 100644 --- a/ktlint/pom.xml +++ b/ktlint/pom.xml @@ -73,91 +73,6 @@ klob ${klob.version} - - org.apache.maven - maven-aether-provider - ${aether.maven.provider.version} - - - org.eclipse.aether - aether-api - - - org.eclipse.aether - aether-spi - - - org.eclipse.aether - aether-util - - - org.eclipse.aether - aether-impl - - - - - - org.apache.maven - maven-model - ${aether.maven.provider.version} - - - org.apache.maven - maven-model-builder - ${aether.maven.provider.version} - - - org.apache.maven - maven-repository-metadata - ${aether.maven.provider.version} - - - com.google.guava - guava - 18.0 - - - org.eclipse.aether - aether-api - ${aether.version} - - - org.eclipse.aether - aether-spi - ${aether.version} - - - org.eclipse.aether - aether-util - ${aether.version} - - - org.eclipse.aether - aether-impl - ${aether.version} - - - org.eclipse.aether - aether-connector-basic - ${aether.version} - - - org.eclipse.aether - aether-transport-file - ${aether.version} - - - org.eclipse.aether - aether-transport-http - ${aether.version} - - - - org.slf4j - slf4j-nop - 1.6.2 - info.picocli picocli diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt index e15f2f1038..edd07e2939 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt @@ -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 @@ -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 @@ -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 @@ -186,35 +181,12 @@ class KtlintCommandLine { ) private var reporters = ArrayList() - @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: =)" - ], - hidden = true - ) - private var repositories = ArrayList() - - @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() - @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"] @@ -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()) { @@ -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(), @@ -376,9 +345,17 @@ class KtlintCommandLine { } } - private fun loadReporter(dependencyResolver: Lazy, tripped: () -> Boolean): Reporter { - data class ReporterTemplate(val id: String, val artifact: String?, val config: Map, 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, + 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("") @@ -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\"") } @@ -503,105 +482,20 @@ class KtlintCommandLine { private fun List.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 =" - ) - } + // fixme: isn't going to work on JDK 9 + private fun loadJARs(artifacts: List) { + 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, artifacts: List) { - (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) = @@ -618,8 +512,8 @@ class KtlintCommandLine { map } - private fun java.net.URLClassLoader.addURLs(url: Iterable) { - val method = java.net.URLClassLoader::class.java.getDeclaredMethod("addURL", java.net.URL::class.java) + private fun URLClassLoader.addURLs(url: Iterable) { + val method = URLClassLoader::class.java.getDeclaredMethod("addURL", URL::class.java) method.isAccessible = true url.forEach { method.invoke(this, it) } } diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/MavenDependencyResolver.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/MavenDependencyResolver.kt deleted file mode 100644 index 36fd715aae..0000000000 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/MavenDependencyResolver.kt +++ /dev/null @@ -1,79 +0,0 @@ -package com.pinterest.ktlint.internal - -import java.io.File -import org.apache.maven.repository.internal.MavenRepositorySystemUtils -import org.eclipse.aether.DefaultRepositorySystemSession -import org.eclipse.aether.RepositorySystem -import org.eclipse.aether.RepositorySystemSession -import org.eclipse.aether.artifact.Artifact -import org.eclipse.aether.collection.CollectRequest -import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory -import org.eclipse.aether.graph.Dependency -import org.eclipse.aether.impl.DefaultServiceLocator -import org.eclipse.aether.repository.LocalRepository -import org.eclipse.aether.repository.RemoteRepository -import org.eclipse.aether.repository.RepositoryPolicy -import org.eclipse.aether.resolution.DependencyRequest -import org.eclipse.aether.spi.connector.RepositoryConnectorFactory -import org.eclipse.aether.spi.connector.transport.TransporterFactory -import org.eclipse.aether.transfer.TransferEvent -import org.eclipse.aether.transfer.TransferListener -import org.eclipse.aether.transport.file.FileTransporterFactory -import org.eclipse.aether.transport.http.HttpTransporterFactory -import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator - -class MavenDependencyResolver( - baseDir: File, - val repositories: Iterable, - forceUpdate: Boolean -) { - - private val repoSystem: RepositorySystem - private val session: RepositorySystemSession - - init { - val locator = MavenRepositorySystemUtils.newServiceLocator() - locator.addService(RepositoryConnectorFactory::class.java, BasicRepositoryConnectorFactory::class.java) - locator.addService(TransporterFactory::class.java, FileTransporterFactory::class.java) - locator.addService(TransporterFactory::class.java, HttpTransporterFactory::class.java) - locator.setErrorHandler(object : DefaultServiceLocator.ErrorHandler() { - override fun serviceCreationFailed(type: Class<*>?, impl: Class<*>?, ex: Throwable) { - throw ex - } - }) - repoSystem = locator.getService(RepositorySystem::class.java) - session = MavenRepositorySystemUtils.newSession() - session.localRepositoryManager = repoSystem.newLocalRepositoryManager(session, LocalRepository(baseDir)) - session.updatePolicy = if (forceUpdate) { - RepositoryPolicy.UPDATE_POLICY_ALWAYS - } else { - RepositoryPolicy.UPDATE_POLICY_NEVER - } - } - - fun setTransferEventListener(listener: (event: TransferEvent) -> Unit) { - (session as DefaultRepositorySystemSession).transferListener = object : TransferListener { - override fun transferProgressed(event: TransferEvent) = listener(event) - override fun transferStarted(event: TransferEvent) = listener(event) - override fun transferInitiated(event: TransferEvent) = listener(event) - override fun transferSucceeded(event: TransferEvent) = listener(event) - override fun transferCorrupted(event: TransferEvent) = listener(event) - override fun transferFailed(event: TransferEvent) = listener(event) - } - } - - fun resolve(vararg artifacts: Artifact): Collection { - System.err.println("[WARNING] Resolving third party rules/reporters from artifactory is deprecated!") - System.err.println("[WARNING] See: https://github.com/pinterest/ktlint/issues/451") - val collectRequest = CollectRequest() - artifacts.forEach { - collectRequest.addDependency(Dependency(it, "compile")) - } - repositories.forEach { - collectRequest.addRepository(it) - } - val node = repoSystem.collectDependencies(session, collectRequest).root - repoSystem.resolveDependencies(session, DependencyRequest().apply { root = node }) - return PreorderNodeListGenerator().apply { node.accept(this) }.files - } -} diff --git a/pom.xml b/pom.xml index ff9a268661..684fab10e7 100644 --- a/pom.xml +++ b/pom.xml @@ -53,8 +53,6 @@ 1.3 1.3 0.2.1 - 1.1.0 - 3.3.9 3.9.6 0.2.0 4.12