Skip to content

Commit bfb15f1

Browse files
test/docs: use latest Maven Central version dynamically
1 parent ca6d715 commit bfb15f1

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

docs/getting-started.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ If you want to consume the SDK from Maven Central (instead of source code in thi
88

99
- GroupId: `io.github.deathbycaptcha`
1010
- ArtifactId: `deathbycaptcha-java-library`
11-
- Version: `4.6.7`
11+
- Version: `LATEST`
1212

1313
`pom.xml` dependency:
1414

1515
```xml
1616
<dependency>
1717
<groupId>io.github.deathbycaptcha</groupId>
1818
<artifactId>deathbycaptcha-java-library</artifactId>
19-
<version>4.6.7</version>
19+
<version>LATEST</version>
2020
</dependency>
2121
```
2222

2323
Quick online resolution check:
2424

2525
```bash
2626
mvn -B org.apache.maven.plugins:maven-dependency-plugin:3.7.1:get \
27-
-Dartifact=io.github.deathbycaptcha:deathbycaptcha-java-library:4.6.7 \
27+
-Dartifact=io.github.deathbycaptcha:deathbycaptcha-java-library:LATEST \
2828
-Dtransitive=false \
2929
-DremoteRepositories=central::default::https://repo1.maven.org/maven2
3030
```

docs/library-usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ Use this dependency in your Maven project:
1010
<dependency>
1111
<groupId>io.github.deathbycaptcha</groupId>
1212
<artifactId>deathbycaptcha-java-library</artifactId>
13-
<version>4.6.7</version>
13+
<version>LATEST</version>
1414
</dependency>
1515
```
1616

1717
Optional online fetch verification:
1818

1919
```bash
2020
mvn -B org.apache.maven.plugins:maven-dependency-plugin:3.7.1:get \
21-
-Dartifact=io.github.deathbycaptcha:deathbycaptcha-java-library:4.6.7 \
21+
-Dartifact=io.github.deathbycaptcha:deathbycaptcha-java-library:LATEST \
2222
-Dtransitive=false \
2323
-DremoteRepositories=central::default::https://repo1.maven.org/maven2
2424
```

src/test/java/com/DeathByCaptcha/OnlineMavenBalanceIntegrationTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.nio.file.Path;
99
import java.util.ArrayList;
1010
import java.util.List;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
1113

1214
import static org.junit.Assert.assertEquals;
1315
import static org.junit.Assert.assertFalse;
@@ -24,7 +26,7 @@ public void testMavenCentralVersionAndBalanceCheck() throws java.lang.Exception
2426
Assume.assumeTrue("Skipping online integration test: DBC_USERNAME is missing", !username.isEmpty());
2527
Assume.assumeTrue("Skipping online integration test: DBC_PASSWORD is missing", !password.isEmpty());
2628

27-
String expectedVersion = "4.6.7";
29+
String expectedVersion = resolveLatestVersionFromMavenCentral();
2830
Path tempProject = Files.createTempDirectory("dbc-online-maven-it-");
2931
Path isolatedMavenRepo = tempProject.resolve(".m2/repository");
3032
Path expectedArtifactJar = isolatedMavenRepo.resolve(
@@ -191,6 +193,42 @@ private static String getenvTrimmed(String key) {
191193
return value == null ? "" : value.trim();
192194
}
193195

196+
private static String resolveLatestVersionFromMavenCentral() throws IOException, InterruptedException {
197+
CommandResult metadataResult = runCommand(
198+
Path.of("."),
199+
List.of(
200+
"mvn",
201+
"-B",
202+
"-q",
203+
"-U",
204+
"org.apache.maven.plugins:maven-help-plugin:3.4.1:evaluate",
205+
"-Dexpression=project.version",
206+
"-DforceStdout",
207+
"-Dartifact=io.github.deathbycaptcha:deathbycaptcha-java-library:LATEST",
208+
"-DremoteRepositories=central::default::https://repo1.maven.org/maven2"
209+
)
210+
);
211+
212+
if (metadataResult.exitCode != 0) {
213+
throw new IOException("Unable to resolve latest version from Maven Central:\n" + metadataResult.output);
214+
}
215+
216+
Pattern versionPattern = Pattern.compile("^\\d+\\.\\d+\\.\\d+(?:[-.].+)?$");
217+
for (String line : metadataResult.output.split("\\R")) {
218+
String value = line.trim();
219+
if (versionPattern.matcher(value).matches()) {
220+
return value;
221+
}
222+
}
223+
224+
Matcher matcher = Pattern.compile("(\\d+\\.\\d+\\.\\d+(?:[-.][^\\s]+)?)").matcher(metadataResult.output);
225+
if (matcher.find()) {
226+
return matcher.group(1);
227+
}
228+
229+
throw new IOException("Could not parse latest version from Maven output:\n" + metadataResult.output);
230+
}
231+
194232
private static String maskValue(String value) {
195233
if (value == null || value.isEmpty()) {
196234
return "<empty>";

0 commit comments

Comments
 (0)