Obtain list of versions for artifact group & name from repository

How can I obtain a sorted list of versions for an artifact group & name pair from a repository?

Are you using nexus? There’s the search API

1 Like

I want to be able to search any repository that works in Gradle (or at least as many as easily possible).

Is there no standard way to do this via Gradle or other APIs?

I’m using Maven Central right now, but I want to do this in a Gradle plugin that others can use regardless of which repository they use.

I want this because I tried using ”latest.release” as the version for all my dependencies, along with dependency locking to ensure that builds don’t change without my knowledge (so I will be alerted about new versions, so I can test them & upgrade, or, if I don’t have time, I can set the dependency manually to the older version my code was using). But I don’t want to use prerelease versions. It would be simpler to be able to continue to use ”latest.release” rather than having to manually set the version for any artifact whose latest version is a prerelease.

If some other special version string will achieve what I want, I can use that instead of ”latest.release”.

I’m not sure you can guarantee that all repositories will have support for this feature. My guess is you’ll need a custom implementation per repository type

Thanks for the info.

On second thought, you should take a look at the source code for gradle-versions-plugin which must have solved the problem

1 Like

Thanks for the info.

From what I found, I was able to implement what I wanted using the following code in Plugin<Project>#apply(Project) where UNSTABLE_VERSION_PATTERN = Pattern.compile("alpha|beta|latest|pr|snap"):

	project.configurations.all {configuration ->
		configuration.resolutionStrategy {resolutionStrategy ->
			resolutionStrategy.componentSelection.all {
				if (UNSTABLE_VERSION_PATTERN.matcher(it.candidate.version).find()) {
					it.reject("Found unstable version pattern: " + UNSTABLE_VERSION_PATTERN.pattern())
				}
			}

			resolutionStrategy.eachDependency {
				it.useVersion("+")
			}
		}
	}

The key was to use "+" instead of "latest.release" since the latter only includes the latest release in its candidate list, whereas the former includes all releases in its candidate list. So, if I used the latter, the only candidate is rejected; if I use the former, the most recent unstable candidates are rejected, but the most recent stable candidate is accepted.

I will eventually modify this to have configurable unstable version detection, and to only apply to dependencies whose version is "latest.stable".