Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ class SemanticVersionPlugin : Plugin<Project> {
project.tasks.withType<GenerateMavenPom> {
this.dependsOn("semanticVersion")
}
project.gradle.projectsEvaluated {
val semanticVersionTasks = project.rootProject.allprojects.flatMap {
it.tasks.withType(SemanticVersionTask::class.java).toList()
}
project.tasks.withType<GenerateMavenPom>().configureEach {
dependsOn(semanticVersionTasks)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ abstract class SemanticVersionTask : DefaultTask() {
val group = dep.getElementsByTagName("groupId").item(0).textContent
val artifact = dep.getElementsByTagName("artifactId").item(0).textContent
val key = "${group}:${artifact}"
if (versions.containsKey(key)) {
val version = versions[key]
val version = versions[key] ?: publicationVersion(group, artifact)
if (version != null) {
dep.getElementsByTagName("version").item(0).textContent = version
}
}
Expand All @@ -113,4 +113,12 @@ abstract class SemanticVersionTask : DefaultTask() {
}
}
}

private fun publicationVersion(groupId: String, artifactId: String): String? =
project.rootProject.allprojects
.asSequence()
.mapNotNull { it.extensions.findByType(PublishingExtension::class.java) }
.flatMap { it.publications.withType(MavenPublication::class.java).asSequence() }
.find { it.groupId == groupId && it.artifactId == artifactId }
?.version
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.poolside.gradle.semanticversion

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import java.io.File
Expand All @@ -15,4 +16,11 @@ object PomParser {
}
}

data class Pom(val groupId: String, val artifactId: String, val version: String)
data class Pom(val groupId: String, val artifactId: String, val version: String, val dependencies: Dependencies? = null)

data class Dependencies(
@field:JacksonXmlElementWrapper(useWrapping = false)
val dependency: List<Dependency> = emptyList()
)

data class Dependency(val groupId: String, val artifactId: String, val version: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.poolside.gradle.semanticversion

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File

class PomParserTest {

@TempDir
lateinit var testProjectDir: File

@Test
fun `parses dependencies`() {
val pomFile = File(testProjectDir, "pom.xml")
pomFile.writeText(
"""
<project>
<groupId>dev.poolside.test</groupId>
<artifactId>my-library</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
</dependencies>
</project>
""".trimIndent()
)

val pom = PomParser.parse(pomFile.absolutePath)

assertEquals(
listOf(
Dependency("org.apache.commons", "commons-math3", "3.6.1"),
Dependency("com.google.guava", "guava", "30.1.1-jre")
),
pom.dependencies?.dependency
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,10 @@ class SemanticVersionPluginTest {
pomFile.forEachLine { println(it) }
val pom = PomParser.parse(pomFile.absolutePath)
assertEquals("0.1.0", pom.version)
val lib = pom.dependencies?.dependency?.find { it.groupId == "dev.poolside.test" }
if (lib != null) {
assertEquals("0.1.0", lib.version)
}
}
val valid = mutableListOf(
"${mavenRepo.absolutePath}/dev/poolside/test/my-library/0.1.0/my-library-0.1.0.jar",
Expand Down
Loading