Preferring snapshot versions of equal branch names

Hi,
we use maven-style snapshot versions in our project.

Is it possible to implement a rule to prefer versions with the same branch names over the masters versions (prefer 0.0.1-feature-foo-SNAPSHOT over 0.0.1-SNAPSHOT if on branch feature-foo)?

Gradle does not know anything about branches.
Branches are a VCS feature Gradle is not aware of itself.
If you want that, you need to implement it yourself.
The regular way is to define that version if you want to have it.

Yes I see that, but getting the current branch name is easy for me. Whats hard is, to tell gradle to use the branch snapshot version (0.0.1-feature-foo-SNAPSHOT) if it exists and the master version otherwise.

I don’t know what would be the best way to implement that rule. Looking at component selection for example, I could reject the master version if I would now that the branch version exists. Is there a way to check for some version during component selection?

Or is a different way better/easier?

I see.
I guess you have at least two options that are both not optimal.

If you know the URL of the repository where those versions would be published, you could try to download the POM for the branch to see whether it is there or not.

Or you could let Gradle do dependency resolution to figure out whether it is there, for example using an artifact resolution query like

if (
    dependencies
        .createArtifactResolutionQuery()
        .forModule("group", "module", "0.0.1-feature-foo-SNAPSHOT")
        .withArtifacts(MavenModule::class, MavenPomArtifact::class)
        .execute()
        .resolvedComponents
        .isEmpty()
) 
1 Like

Thats for your reply. I did not yet find the time to come back to that topic, but your suggestion looks like it’ll help there.

Unfortunately the artifactresolutionquery does not seem to work without a version (null) or with wildcards ‘+’, ‘*’).
Since we are trying to implement it via resolutionStrategy.eachDependecy, we do not know any version number yet.

Is there an API to query the repository (artifactory) for all versions of a dependency?

I don’t think such an API exists, or at least I’m not aware of one.
You probably have to manually download the respective maven-metadata.xml file to get a list of published versions.

Thanks for the help.

Just for future reference: We resorted to download maven-metadata.xml via REST directly:

def queryAllVersions(group, artifact) {
	def url = "https://our-artifactory/artifactory/${group.replaceAll("\\.", "/")}/${artifact}/maven-metadata.xml".toURL()
	def connection = url.openConnection()
	connection.requestMethod = 'GET'

	def user = providers.gradleProperty("ARTIFACTORY_USER").get()
	def password = providers.gradleProperty("ARTIFACTORY_PASSWORD").get()
	def encodedAuth = "$user:$password".bytes.encodeBase64().toString()
	def authHeaderValue = "Basic $encodedAuth";
	connection.setRequestProperty("Authorization", authHeaderValue);
	if (connection.responseCode == 200) {
		def metadata = new XmlParser().parse connection.content

		return metadata.versioning.versions.version.collect { VersionNumber.parse(it.text()) }.sort()
	} else if (connection.responseCode == 404) {
		return []
	} else {
		throw new IOException("Failed to GET ${url}: failed with ${connection.responseCode} ")
	}
}

There is also stuff like MavenVersionLister.listVersions. I played around with project.getRepositories().withType(MavenArtifactRepository.class).find().createResolver().remoteAccess.listModuleVersions(new DefaultBuildableModuleVersionListingResolveResult()) but I failed to make that work.