1+ import java.io.ByteArrayOutputStream
2+
13plugins {
24 id(" java" )
5+ id(" maven-publish" )
36}
47
5- group = " de.21no"
6- version = " 1.0.0-alpha"
7-
88repositories {
99 mavenCentral()
1010}
@@ -26,9 +26,65 @@ dependencies {
2626 testImplementation(" org.junit.jupiter:junit-jupiter" )
2727 // https://mvnrepository.com/artifact/org.mockito/mockito-core
2828 testImplementation(" org.mockito:mockito-core:5.14.2" )
29-
3029}
31-
3230tasks.test {
3331 useJUnitPlatform()
34- }
32+ }
33+
34+ // Dynamically set the version from the latest Git tag
35+ version = run {
36+ // Function to check if Git is available
37+ fun isGitAvailable (): Boolean {
38+ return try {
39+ exec {
40+ commandLine(" git" , " --version" )
41+ standardOutput = ByteArrayOutputStream ()
42+ errorOutput = ByteArrayOutputStream ()
43+ isIgnoreExitValue = true
44+ }.exitValue == 0
45+ } catch (e: Exception ) {
46+ false
47+ }
48+ }
49+
50+ if (isGitAvailable()) {
51+ try {
52+ val stdout = ByteArrayOutputStream ()
53+ exec {
54+ commandLine(" git" , " describe" , " --tags" , " --abbrev=0" )
55+ standardOutput = stdout
56+ errorOutput = ByteArrayOutputStream ()
57+ isIgnoreExitValue = true
58+ }
59+ val gitTag = stdout.toString().trim()
60+ gitTag.ifEmpty {
61+ " 1.0.0-SNAPSHOT" // Default version if no tag is found
62+ }
63+ } catch (e: Exception ) {
64+ " 1.0.0-SNAPSHOT" // Default version if Git command fails
65+ }
66+ } else {
67+ " 1.0.0-SNAPSHOT" // Default version if Git is not available
68+ }
69+ }
70+
71+ publishing {
72+ publications {
73+ create<MavenPublication >(" gpr" ) {
74+ from(components[" java" ])
75+ groupId = " de.n21no.realtime.pubsub"
76+ artifactId = " core"
77+ version = version
78+ }
79+ }
80+ repositories {
81+ maven {
82+ name = " GitHubPackages"
83+ url = uri(" https://maven.pkg.github.com/BackendStack21/realtime-pubsub-client-java" )
84+ credentials {
85+ username = System .getenv(" GITHUB_ACTOR" )
86+ password = System .getenv(" GITHUB_TOKEN" )
87+ }
88+ }
89+ }
90+ }
0 commit comments