Skip to content

[Maven] Support for maven central snapshots #10997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions services/maven-central/maven-central-snapshots-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BaseXmlService } from '../index.js'

export default class MavenCentralSnapshotsBase extends BaseXmlService {
async fetch({ groupId, artifactId, schema }) {
const group = encodeURIComponent(groupId).replace(/\./g, '/')
const artifact = encodeURIComponent(artifactId)
return this._requestXml({
schema,
url: `https://central.sonatype.com/repository/maven-snapshots/${group}/${artifact}/maven-metadata.xml`,
httpErrors: { 404: 'artifact not found' },
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Joi from 'joi'
import { pathParams } from '../index.js'
import { parseDate, renderDateBadge } from '../date.js'
import { nonNegativeInteger } from '../validators.js'
import MavenCentralSnapshotsBase from './maven-central-snapshots-base.js'

const updateResponseSchema = Joi.object({
metadata: Joi.object({
versioning: Joi.object({
lastUpdated: nonNegativeInteger,
}).required(),
}).required(),
}).required()

export default class MavenCentralSnapshotsLastUpdate extends MavenCentralSnapshotsBase {
static category = 'activity'

static route = {
base: 'maven-central-snapshots/last-update',
pattern: ':groupId/:artifactId',
}

static openApi = {
'/maven-central-snapshots/last-update/{groupId}/{artifactId}': {
get: {
summary: 'Maven Central Snapshots Last Update',
parameters: pathParams(
{ name: 'groupId', example: 'com.google.guava' },
{ name: 'artifactId', example: 'guava' },
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace this with an example that works

Copy link
Contributor

@StefMa StefMa May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "problem" with an example here is, that thery remove snapshots after a 90 days.
See https://central.sonatype.org/publish/publish-portal-snapshots/

Specifically, -SNAPSHOT releases are cleaned up after a period of time (currently 90 days). We believe that this should be sufficient, as projects under active development will be pushing new versions of the same -SNAPSHOT component with some frequency.

So even if we go with an SNAPSHOT release today, it might break in 90 days 🫠
So we might need an really active project to have some kind of a stable example...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sure. But if you look at the example used in the service tests, it does actually exist
https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml
the versions might expire over time, but the repo is there
https://central.sonatype.com/repository/maven-snapshots/com/google/guava/guava/maven-metadata.xml
is never going to return anything.

),
},
},
}

static defaultBadgeData = { label: 'last updated' }

async handle({ groupId, artifactId }) {
const { metadata } = await this.fetch({
groupId,
artifactId,
schema: updateResponseSchema,
})

const date = parseDate(
String(metadata.versioning.lastUpdated),
'YYYYMMDDHHmmss',
)

return renderDateBadge(date)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isFormattedDate } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('last update date')
.get('/me.mrdoc.minecraft/dlibcustomextensions.json')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last update when artifact not found')
.get('/com.fail.test/this-does-not-exist.json')
.expectBadge({
label: 'last updated',
message: 'artifact not found',
})
46 changes: 46 additions & 0 deletions services/maven-central/maven-central-snapshots.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { redirector, pathParam, queryParam } from '../index.js'
import { description } from '../maven-metadata/maven-metadata.js'

export default redirector({
category: 'version',
isDeprecated: false,
route: {
base: 'maven-central-snapshots/v',
pattern: ':groupId/:artifactId/:versionPrefix?',
},
openApi: {
'/maven-central-snapshots/v/{groupId}/{artifactId}': {
get: {
summary: 'Maven Central Snapshots Version',
description,
parameters: [
pathParam({ name: 'groupId', example: 'com.google.guava' }),
pathParam({ name: 'artifactId', example: 'guava' }),
Comment on lines +17 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also replace this with an example that works

queryParam({
name: 'versionPrefix',
example: '29',
description: 'Filter only versions with this prefix.',
}),
queryParam({
name: 'versionSuffix',
example: '-android',
description: 'Filter only versions with this suffix.',
}),
Comment on lines +19 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secondly, lets look at the version badge.

This isn't really your fault, but the maven central version badge has a long-standing issue. #6188
I think it is likely we will remove the versionPrefix and versionSuffix params (see #6188 (comment) )
If we are going to add a specific route for this (although you can already make badges for this service anyway e.g: https://img.shields.io/maven-metadata/v.svg?label=maven-central-snapshots&metadataUrl=https%3A//central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml as the routes are just redirectors for the more general maven-metadata route), then I think we should not expose the versionPrefix and versionSuffix params on the documentation page. Lets not encourage users to start using a feature that is likely to disappear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused with the URL you posted here.
Right know, version 1.0.0-SNAPSHOT is released.
See https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml

However, if I open the URL, it gives me 0.0.3-SNAPSHOT back.

Screenshot 2025-05-21 at 12 18 35 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, see #6188 At the moment we're making an assumption about the ordering of versions which doesn't hold across every registry.

Tbh, I think we probably need to get #6188 resolved (see #11077 - I've not had a chance to follow up on that yet) and then circle back to this one having done that.

],
},
},
},
transformPath: () => '/maven-metadata/v',
transformQueryParams: ({ groupId, artifactId, versionPrefix }) => {
const group = encodeURIComponent(groupId).replace(/\./g, '/')
const artifact = encodeURIComponent(artifactId)
const metadataUrl = `https://central.sonatype.com/repository/maven-snapshots/${group}/${artifact}/maven-metadata.xml`
return {
metadataUrl,
label: 'maven-central-snapshots',
versionPrefix,
}
},
overrideTransformedQueryParams: true,
dateAdded: new Date('2021-06-12'),
})
10 changes: 10 additions & 0 deletions services/maven-central/maven-central-snapshots.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('latest version redirection')
.get('/me.mrdoc.minecraft/dlibcustomextensions.json') // https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/
.expectRedirect(
`/maven-metadata/v.json?label=maven-central-snapshots&metadataUrl=${encodeURIComponent(
'https://central.sonatype.com/repository/maven-snapshots/me/mrdoc/minecraft/dlibcustomextensions/maven-metadata.xml',
)}`,
)
Loading