88import java .nio .file .Path ;
99import java .util .ArrayList ;
1010import java .util .List ;
11+ import java .util .regex .Matcher ;
12+ import java .util .regex .Pattern ;
1113
1214import static org .junit .Assert .assertEquals ;
1315import 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