Skip to content

Commit

Permalink
Fixed ktlint's integration with Maven (pinterest#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Nov 30, 2017
1 parent 6c1f18c commit f49e0b6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
15 changes: 15 additions & 0 deletions ktlint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@
</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>
Expand Down
9 changes: 5 additions & 4 deletions ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import java.nio.file.Paths
import java.security.MessageDigest
import java.util.ArrayList
import java.util.Arrays
import java.util.LinkedHashMap
import java.util.NoSuchElementException
import java.util.ResourceBundle
import java.util.Scanner
Expand Down Expand Up @@ -238,7 +239,7 @@ ${ByteArrayOutputStream().let { this.printUsage(it); it }.toString().trimEnd().s
}
val start = System.currentTimeMillis()
// load 3rd party ruleset(s) (if any)
val dependencyResolver by lazy { buildDependencyResolver() }
val dependencyResolver = lazy(LazyThreadSafetyMode.NONE) { buildDependencyResolver() }
if (!rulesets.isEmpty()) {
loadJARs(dependencyResolver, rulesets)
}
Expand Down Expand Up @@ -333,7 +334,7 @@ ${ByteArrayOutputStream().let { this.printUsage(it); it }.toString().trimEnd().s
}
}

private fun loadReporter(dependencyResolver: MavenDependencyResolver): Reporter {
private fun loadReporter(dependencyResolver: Lazy<MavenDependencyResolver>): Reporter {
data class ReporterTemplate(val id: String, val config: Map<String, String>, var output: String?)
val tpls = (if (reporters.isEmpty()) listOf("plain") else reporters)
.map { reporter ->
Expand Down Expand Up @@ -543,14 +544,14 @@ ${ByteArrayOutputStream().let { this.printUsage(it); it }.toString().trimEnd().s
return dependencyResolver
}

private fun loadJARs(dependencyResolver: MavenDependencyResolver, artifacts: List<String>) {
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.resolve(DefaultArtifact(artifact)).map { it.toURI().toURL() }
dependencyResolver.value.resolve(DefaultArtifact(artifact)).map { it.toURI().toURL() }
} catch (e: IllegalArgumentException) {
val file = File(expandTilde(artifact))
if (!file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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
Expand All @@ -32,6 +33,11 @@ class MavenDependencyResolver(baseDir: File, val repositories: Iterable<RemoteRe
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))
Expand Down

0 comments on commit f49e0b6

Please # to comment.