From a4d706df63d176dfa9233965a89daae6295ff200 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 12:56:05 +0300 Subject: [PATCH 1/9] Fixed maven plugin ### What's done: - fixed checking configFile It closes #1814 --- .../com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..e7a2f49285 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 @@ -96,7 +96,7 @@ abstract class DiktatBaseMojo : AbstractMojo() { */ override fun execute() { val configFile = resolveConfig() - if (configFile.isRegularFile()) { + if (!configFile.isRegularFile()) { throw MojoExecutionException("Configuration file $diktatConfigFile doesn't exist") } log.info("Running diKTat plugin with configuration file $configFile and inputs $inputs" + @@ -160,7 +160,7 @@ abstract class DiktatBaseMojo : AbstractMojo() { return generateSequence(mavenProject) { it.parent } .map { it.basedir.toPath().resolve(diktatConfigFile) } .run { - firstOrNull { it.isRegularFile() } ?: first() + firstOrNull { it.isRegularFile() } ?: file } } } From 80a0fc2293c9aac32d120a7eb37ebc9b53e9b94d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 14:50:20 +0300 Subject: [PATCH 2/9] restored logic for input and exclude in maven plugin --- .../diktat/plugin/maven/DiktatBaseMojo.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 e7a2f49285..879b975041 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 @@ -110,10 +111,19 @@ abstract class DiktatBaseMojo : AbstractMojo() { outputStream = getReporterOutput(), sourceRootDir = sourceRootDir.takeIf { reporterType == DiktatReporterType.SARIF }, ) + val (excludedDirs, excludedFiles) = excludes.map(::File).partition { it.isDirectory } + val files = inputs + .asSequence() + .map(::File) + .flatMap { + it.files(excludedDirs, excludedFiles) + } + .map { it.toPath() } + .toList() val args = DiktatRunnerArguments( configInputStream = configFile.inputStream(), sourceRootDir = sourceRootDir, - files = inputs.map(::Path), + files = files, baselineFile = baseline?.toPath(), reporterArgsList = listOf(reporterArgs), ) @@ -163,4 +173,15 @@ abstract class DiktatBaseMojo : AbstractMojo() { firstOrNull { it.isRegularFile() } ?: file } } + + @Suppress("TYPE_ALIAS") + private fun File.files( + excludedDirs: List, + excludedFiles: List, + ): Sequence = walk() + .filter { file -> + file.isDirectory || file.toPath().isKotlinCodeOrScript() + } + .filter { it.isFile } + .filterNot { file -> file in excludedFiles || excludedDirs.any { file.startsWith(it) } } } From 987a511152192ca2d36f41cfb88561cbbd844895 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 15:36:30 +0300 Subject: [PATCH 3/9] fixed warning about provided dependency in maven plugin --- diktat-maven-plugin/build.gradle.kts | 20 ++++++++++++++++++++ gradle/libs.versions.toml | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/diktat-maven-plugin/build.gradle.kts b/diktat-maven-plugin/build.gradle.kts index 3022df404d..8812a40e2c 100644 --- a/diktat-maven-plugin/build.gradle.kts +++ b/diktat-maven-plugin/build.gradle.kts @@ -12,6 +12,8 @@ plugins { dependencies { implementation(libs.maven.plugin.api) + implementation(libs.maven.artifact) + implementation(libs.maven.model) compileOnly(libs.maven.plugin.annotations) compileOnly(libs.maven.core) @@ -48,3 +50,21 @@ publishing { } } configurePublications() + +publishing { + publications { + withType { + pom { + withXml { + val dependencies = asElement().getElementsByTagName("dependency") + for (i in 0 until dependencies.length) { + val dependency = dependencies.item(i) + if (dependency.firstChild.firstChild.nodeValue == "org.apache.maven") { + dependency.lastChild.firstChild.nodeValue = "provided" + } + } + } + } + } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6d327453d8..da70e68efe 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -123,8 +123,9 @@ assertj-core = { module = "org.assertj:assertj-core", version.ref = "assertj" } junit-platform-suite = { module = "org.junit.platform:junit-platform-suite-engine", version.ref = "junit-platfrom" } # maven +maven-artifact = { module = "org.apache.maven:maven-artifact", version.ref = "maven-api" } +maven-model = { module = "org.apache.maven:maven-model", version.ref = "maven-api" } 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" } From b1c7c9ce2d41a91805bdeb0b75c4eaf465a68f41 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 15:51:41 +0300 Subject: [PATCH 4/9] strange issue with firstChild and nodeValue --- diktat-maven-plugin/build.gradle.kts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/diktat-maven-plugin/build.gradle.kts b/diktat-maven-plugin/build.gradle.kts index 8812a40e2c..751be045c1 100644 --- a/diktat-maven-plugin/build.gradle.kts +++ b/diktat-maven-plugin/build.gradle.kts @@ -59,8 +59,10 @@ publishing { val dependencies = asElement().getElementsByTagName("dependency") for (i in 0 until dependencies.length) { val dependency = dependencies.item(i) - if (dependency.firstChild.firstChild.nodeValue == "org.apache.maven") { - dependency.lastChild.firstChild.nodeValue = "provided" + val groupIdNode = dependency.firstChild + val scopeNode = dependency.lastChild + if (groupIdNode.nodeValue == "org.apache.maven") { + scopeNode.nodeValue = "provided" } } } From fe592a7e9977724d461b4e6249f89cdb4efb0ae4 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 16:24:43 +0300 Subject: [PATCH 5/9] strange issue with firstChild and nodeValue 2 --- diktat-maven-plugin/build.gradle.kts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/diktat-maven-plugin/build.gradle.kts b/diktat-maven-plugin/build.gradle.kts index 751be045c1..13594e319f 100644 --- a/diktat-maven-plugin/build.gradle.kts +++ b/diktat-maven-plugin/build.gradle.kts @@ -56,13 +56,17 @@ publishing { withType { pom { withXml { - val dependencies = asElement().getElementsByTagName("dependency") - for (i in 0 until dependencies.length) { - val dependency = dependencies.item(i) - val groupIdNode = dependency.firstChild - val scopeNode = dependency.lastChild - if (groupIdNode.nodeValue == "org.apache.maven") { - scopeNode.nodeValue = "provided" + val dependencyNodes = asElement().getElementsByTagName("dependency") + for (i in 0 until dependencyNodes.length) { + val dependencyNode = dependencyNodes.item(i) + val childNodes = dependencyNode.childNodes + .let { nodes -> + (0 until nodes.length).map { nodes.item(it) } + } + val groupIdNode = childNodes.single { it.nodeName == "groupId" } + val scopeNode = childNodes.single { it.nodeName == "scope" } + if (groupIdNode.textContent == "org.apache.maven") { + scopeNode.textContent = "provided" } } } From 861c87830af1374985385602613e42c0ec0ad11b Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 17:31:46 +0300 Subject: [PATCH 6/9] reverted changes for provided in maven plugin --- diktat-maven-plugin/build.gradle.kts | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/diktat-maven-plugin/build.gradle.kts b/diktat-maven-plugin/build.gradle.kts index 13594e319f..03658fd060 100644 --- a/diktat-maven-plugin/build.gradle.kts +++ b/diktat-maven-plugin/build.gradle.kts @@ -50,27 +50,3 @@ publishing { } } configurePublications() - -publishing { - publications { - withType { - pom { - withXml { - val dependencyNodes = asElement().getElementsByTagName("dependency") - for (i in 0 until dependencyNodes.length) { - val dependencyNode = dependencyNodes.item(i) - val childNodes = dependencyNode.childNodes - .let { nodes -> - (0 until nodes.length).map { nodes.item(it) } - } - val groupIdNode = childNodes.single { it.nodeName == "groupId" } - val scopeNode = childNodes.single { it.nodeName == "scope" } - if (groupIdNode.textContent == "org.apache.maven") { - scopeNode.textContent = "provided" - } - } - } - } - } - } -} From 00a74ac1044925d7f07f1e8f76e18c1b470d983c Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 18:16:35 +0300 Subject: [PATCH 7/9] removed provided dependencies --- diktat-maven-plugin/build.gradle.kts | 3 --- .../com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt | 1 - gradle/libs.versions.toml | 3 --- 3 files changed, 7 deletions(-) diff --git a/diktat-maven-plugin/build.gradle.kts b/diktat-maven-plugin/build.gradle.kts index 03658fd060..e2dba09cdd 100644 --- a/diktat-maven-plugin/build.gradle.kts +++ b/diktat-maven-plugin/build.gradle.kts @@ -11,9 +11,6 @@ plugins { } dependencies { - implementation(libs.maven.plugin.api) - implementation(libs.maven.artifact) - implementation(libs.maven.model) 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 879b975041..eec63c51f1 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 @@ -19,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 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index da70e68efe..1830cd4cd9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -123,11 +123,8 @@ assertj-core = { module = "org.assertj:assertj-core", version.ref = "assertj" } junit-platform-suite = { module = "org.junit.platform:junit-platform-suite-engine", version.ref = "junit-platfrom" } # maven -maven-artifact = { module = "org.apache.maven:maven-artifact", version.ref = "maven-api" } -maven-model = { module = "org.apache.maven:maven-model", version.ref = "maven-api" } maven-core = { module = "org.apache.maven:maven-core", 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" } From 1a86bb55e32b5c7314a0cbc22559bc0c8947c77a Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 18:25:23 +0300 Subject: [PATCH 8/9] diktatFix --- .../diktat/plugin/maven/DiktatBaseMojo.kt | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) 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 eec63c51f1..398aac660f 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 @@ -110,19 +110,10 @@ abstract class DiktatBaseMojo : AbstractMojo() { outputStream = getReporterOutput(), sourceRootDir = sourceRootDir.takeIf { reporterType == DiktatReporterType.SARIF }, ) - val (excludedDirs, excludedFiles) = excludes.map(::File).partition { it.isDirectory } - val files = inputs - .asSequence() - .map(::File) - .flatMap { - it.files(excludedDirs, excludedFiles) - } - .map { it.toPath() } - .toList() val args = DiktatRunnerArguments( configInputStream = configFile.inputStream(), sourceRootDir = sourceRootDir, - files = files, + files = files(), baselineFile = baseline?.toPath(), reporterArgsList = listOf(reporterArgs), ) @@ -173,6 +164,18 @@ abstract class DiktatBaseMojo : AbstractMojo() { } } + 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, From 6211a81bbcdab7022ac7328cb8aeb4c01d507278 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 22 Nov 2023 18:46:24 +0300 Subject: [PATCH 9/9] review notes --- .../diktat/plugin/maven/DiktatBaseMojo.kt | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) 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 398aac660f..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 @@ -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 "" ) @@ -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,9 +156,7 @@ abstract class DiktatBaseMojo : AbstractMojo() { return generateSequence(mavenProject) { it.parent } .map { it.basedir.toPath().resolve(diktatConfigFile) } - .run { - firstOrNull { it.isRegularFile() } ?: file - } + .firstOrNull { it.isRegularFile() } } private fun files(): List { @@ -182,8 +177,9 @@ abstract class DiktatBaseMojo : AbstractMojo() { excludedFiles: List, ): Sequence = walk() .filter { file -> - file.isDirectory || file.toPath().isKotlinCodeOrScript() + file.isFile && file.toPath().isKotlinCodeOrScript() + } + .filterNot { file -> + file in excludedFiles || excludedDirs.any { file.startsWith(it) } } - .filter { it.isFile } - .filterNot { file -> file in excludedFiles || excludedDirs.any { file.startsWith(it) } } }