-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: Validate Maven version on publish (#1949)
* ci: Validate Gradle version on publish * use composite build plugin to validate version
- Loading branch information
1 parent
60dd609
commit 32244bf
Showing
8 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
repositories { | ||
jcenter() | ||
mavenCentral() | ||
} | ||
|
||
plugins { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
plugins { | ||
`kotlin-dsl` | ||
`java-gradle-plugin` | ||
} | ||
|
||
gradlePlugin { | ||
plugins.register("maven-version-check") { | ||
id = "maven-version-check" | ||
implementationClass = "com.github.flank.gradle.MavenVersionCheck" | ||
} | ||
} | ||
|
||
dependencies { | ||
testImplementation("junit:junit:4.13.2") // unfortunately it does not work with buildSrc Dependencies | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = "mavenVersionCheck" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.flank.gradle | ||
|
||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.PublishException | ||
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository | ||
import org.gradle.internal.impldep.org.jetbrains.annotations.VisibleForTesting | ||
import org.gradle.kotlin.dsl.withType | ||
|
||
class MavenVersionCheck : Plugin<Project> { | ||
override fun apply(project: Project) { | ||
project.tasks.withType<PublishToMavenRepository>().configureEach { | ||
doFirst { | ||
assertValidVersion(publication.version) | ||
} | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
internal fun assertValidVersion(version: String) { | ||
val validSnapshots = arrayOf("master-SNAPSHOT", "local-SNAPSHOT") | ||
val versionRegex = "2\\d\\.\\d{2}\\.\\d{1,2}".toRegex() | ||
if (version !in validSnapshots && !versionRegex.matches(version)) { | ||
throw PublishException("Maven version is not valid!") | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
maven_version_check/src/test/kotlin/MavenVersionCheckTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.flank.gradle | ||
|
||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class MavenVersionCheckTest { | ||
|
||
@Test | ||
fun `Should properly validate maven version for snapshots`() { | ||
with(MavenVersionCheck()) { | ||
assertValidVersion("master-SNAPSHOT") | ||
assertValidVersion("local-SNAPSHOT") | ||
} | ||
} | ||
|
||
@Test | ||
fun `Should properly validate maven version for not snapshots`() { | ||
with(MavenVersionCheck()) { | ||
assertValidVersion("21.02.0") | ||
assertValidVersion("22.01.1") | ||
assertValidVersion("23.11.11") | ||
} | ||
} | ||
|
||
@Test | ||
fun `Should throw exception for not proper maven version`() { | ||
with(MavenVersionCheck()) { | ||
assertTrue(runCatching { assertValidVersion("master_SNAPSHOT") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("local_version") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("version") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("test") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("snapshot") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("2113") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("2020.12.21") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("21.2.1") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("v21.02.01") }.isFailure) | ||
assertTrue(runCatching { assertValidVersion("19.02.1") }.isFailure) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters