From b5c1a37413f44dc5bcdbf33c79573799b50e2dc0 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 23 Nov 2023 11:51:25 +0300 Subject: [PATCH] Fixed maven plugin (#1816) - fixed checking configFile - restored logic for input and exclude in maven plugin (removed in #1655) - fixed warning about provided dependency in maven plugin --- diktat-maven-plugin/build.gradle.kts | 1 - .../diktat/plugin/maven/DiktatBaseMojo.kt | 39 ++++++++++++++----- gradle/libs.versions.toml | 2 - 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/diktat-maven-plugin/build.gradle.kts b/diktat-maven-plugin/build.gradle.kts index 3022df404d..e2dba09cdd 100644 --- a/diktat-maven-plugin/build.gradle.kts +++ b/diktat-maven-plugin/build.gradle.kts @@ -11,7 +11,6 @@ plugins { } dependencies { - implementation(libs.maven.plugin.api) compileOnly(libs.maven.plugin.annotations) compileOnly(libs.maven.core) diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index 32307c7a77..2e9a19d4c1 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -5,6 +5,7 @@ import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.api.DiktatReporterCreationArguments import com.saveourtool.diktat.api.DiktatReporterType import com.saveourtool.diktat.diktatRunnerFactory +import com.saveourtool.diktat.util.isKotlinCodeOrScript import org.apache.maven.plugin.AbstractMojo import org.apache.maven.plugin.Mojo @@ -18,7 +19,6 @@ import java.io.FileOutputStream import java.io.OutputStream import java.nio.file.Path import java.nio.file.Paths -import kotlin.io.path.Path import kotlin.io.path.inputStream import kotlin.io.path.isRegularFile @@ -95,10 +95,7 @@ abstract class DiktatBaseMojo : AbstractMojo() { * @throws MojoExecutionException if an exception in __KtLint__ has been thrown */ override fun execute() { - val configFile = resolveConfig() - if (configFile.isRegularFile()) { - throw MojoExecutionException("Configuration file $diktatConfigFile doesn't exist") - } + val configFile = resolveConfig() ?: throw MojoExecutionException("Configuration file $diktatConfigFile doesn't exist") log.info("Running diKTat plugin with configuration file $configFile and inputs $inputs" + if (excludes.isNotEmpty()) " and excluding $excludes" else "" ) @@ -113,7 +110,7 @@ abstract class DiktatBaseMojo : AbstractMojo() { val args = DiktatRunnerArguments( configInputStream = configFile.inputStream(), sourceRootDir = sourceRootDir, - files = inputs.map(::Path), + files = files(), baselineFile = baseline?.toPath(), reporterArgsList = listOf(reporterArgs), ) @@ -149,9 +146,9 @@ abstract class DiktatBaseMojo : AbstractMojo() { * If [diktatConfigFile] is absolute, it's path is used. If [diktatConfigFile] is relative, this method looks for it in all maven parent projects. * This way config file can be placed in parent module directory and used in all child modules too. * - * @return a configuration file. File by this path might not exist. + * @return a configuration file. File by this path exists. */ - private fun resolveConfig(): Path { + private fun resolveConfig(): Path? { val file = Paths.get(diktatConfigFile) if (file.isAbsolute) { return file @@ -159,8 +156,30 @@ abstract class DiktatBaseMojo : AbstractMojo() { return generateSequence(mavenProject) { it.parent } .map { it.basedir.toPath().resolve(diktatConfigFile) } - .run { - firstOrNull { it.isRegularFile() } ?: first() + .firstOrNull { it.isRegularFile() } + } + + private fun files(): List { + val (excludedDirs, excludedFiles) = excludes.map(::File).partition { it.isDirectory } + return inputs + .asSequence() + .map(::File) + .flatMap { + it.files(excludedDirs, excludedFiles) } + .map { it.toPath() } + .toList() } + + @Suppress("TYPE_ALIAS") + private fun File.files( + excludedDirs: List, + excludedFiles: List, + ): Sequence = walk() + .filter { file -> + file.isFile && file.toPath().isKotlinCodeOrScript() + } + .filterNot { file -> + file in excludedFiles || excludedDirs.any { file.startsWith(it) } + } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6d327453d8..1830cd4cd9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -124,9 +124,7 @@ junit-platform-suite = { module = "org.junit.platform:junit-platform-suite-engin # maven maven-core = { module = "org.apache.maven:maven-core", version.ref = "maven-api" } -maven-embedder = { module = "org.apache.maven:maven-embedder", version.ref = "maven-api" } maven-compat = { module = "org.apache.maven:maven-compat", version.ref = "maven-api" } -maven-plugin-api = { module = "org.apache.maven:maven-plugin-api", version.ref = "maven-api" } maven-plugin-annotations = { module = "org.apache.maven.plugin-tools:maven-plugin-annotations", version.ref = "maven-plugin-tools" } maven-plugin-testing-harness = { module = "org.apache.maven.plugin-testing:maven-plugin-testing-harness", version.ref = "maven-plugin-testing-harness" } plexus-cipher = { module = "org.codehaus.plexus:plexus-cipher", version.ref = "plexus" }