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

Fixed maven plugin #1816

Merged
merged 10 commits into from
Nov 23, 2023
26 changes: 26 additions & 0 deletions diktat-maven-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -48,3 +50,27 @@ publishing {
}
}
configurePublications()

publishing {
publications {
withType<MavenPublication> {
pom {
withXml {
val dependencyNodes = asElement().getElementsByTagName("dependency")
for (i in 0 until dependencyNodes.length) {
nulls marked this conversation as resolved.
Show resolved Hide resolved
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"
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down Expand Up @@ -96,7 +97,7 @@
*/
override fun execute() {
val configFile = resolveConfig()
if (configFile.isRegularFile()) {
if (!configFile.isRegularFile()) {
nulls marked this conversation as resolved.
Show resolved Hide resolved
throw MojoExecutionException("Configuration file $diktatConfigFile doesn't exist")
}
log.info("Running diKTat plugin with configuration file $configFile and inputs $inputs" +
Expand All @@ -110,10 +111,19 @@
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)

Check warning on line 119 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L114-L119

Added lines #L114 - L119 were not covered by tests
}
.map { it.toPath() }
.toList()

Check warning on line 122 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L121-L122

Added lines #L121 - L122 were not covered by tests
val args = DiktatRunnerArguments(
configInputStream = configFile.inputStream(),
sourceRootDir = sourceRootDir,
files = inputs.map(::Path),
files = files,

Check warning on line 126 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L126

Added line #L126 was not covered by tests
baselineFile = baseline?.toPath(),
reporterArgsList = listOf(reporterArgs),
)
Expand Down Expand Up @@ -160,7 +170,18 @@
return generateSequence(mavenProject) { it.parent }
.map { it.basedir.toPath().resolve(diktatConfigFile) }
.run {
firstOrNull { it.isRegularFile() } ?: first()
firstOrNull { it.isRegularFile() } ?: file
kgevorkyan marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Suppress("TYPE_ALIAS")
private fun File.files(
excludedDirs: List<File>,
excludedFiles: List<File>,
): Sequence<File> = walk()
.filter { file ->

Check warning on line 182 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L181-L182

Added lines #L181 - L182 were not covered by tests
file.isDirectory || file.toPath().isKotlinCodeOrScript()
kgevorkyan marked this conversation as resolved.
Show resolved Hide resolved
}
.filter { it.isFile }

Check warning on line 185 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L185

Added line #L185 was not covered by tests
.filterNot { file -> file in excludedFiles || excludedDirs.any { file.startsWith(it) } }
}
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Loading