Skip to content

feat(deps): make the MongoDB driver an optional dependency - #167

Open
ashleycaselli wants to merge 1 commit into
masterfrom
feat/166-optional-mongodb-driver
Open

ashleycaselli wants to merge 1 commit into
masterfrom
feat/166-optional-mongodb-driver

Conversation

@ashleycaselli

Copy link
Copy Markdown
Member

Closes #166

nanopub-java declared the legacy all-in-one org.mongodb:mongo-java-driver 3.12.14 (end of life) as a compile dependency. So every project using the library got a copy of com.mongodb.* and org.bson.*, which clashed with the modern driver in projects that declare one themselves (knowledgepixels/nanopub-registry#180).

Changes

  • Dependency: org.mongodb:mongodb-driver-sync 5.13.0, marked <optional>true</optional>. It is now only on the classpath of projects that declare a driver themselves.
  • NanopubDb: ported from the legacy API (com.mongodb.MongoClient, DB, DBCollection, BasicDBObject) to the current one (MongoClients.create(MongoClientSettings), MongoDatabase, MongoCollection<Document>, Filters.eq). Same connection parameters and behaviour. getMongoClient() now returns com.mongodb.client.MongoClient; nothing in the known dependent projects calls it.
  • NanopubStream: unchanged, since MongoCursor, Document and Binary are the same in the new driver.
  • README: notes that projects using NanopubDb or NanopubStream.fromMongoCursor must declare mongodb-driver-sync themselves.

CLI jar

The cli profile's assembly (jar-with-dependencies) packs the project's own optional dependencies, so the driver (mongodb-driver-sync, mongodb-driver-core, bson) is in the CLI jar without changes to the profile. Checked by:

  • listing the jar's contents;
  • running get --mongodb-host 127.0.0.1 --mongodb-port 1 … from the packed jar, which reaches the driver and times out on the closed port with a MongoTimeoutException, not a NoClassDefFoundError.

The driver is deliberately not added to the cli profile, as that profile activates whenever CI is set and could then bring the driver back into dependent builds on CI.

Verified

  • Without the driver on the classpath: GetNanopub, NanopubStream, FetchIndexFromDb, Run and NanopubImpl load and pass bytecode verification, and nanopubs parse.
  • Downstream: with the snapshot installed locally, nanodash's dependency tree no longer contains a MongoDB driver (before: mongo-java-driver 3.12.14). nanopub-registry declares mongodb-driver-sync itself.
  • New NanopubDbTest: with a mocked client, it checks the connection settings with and without credentials, the _id lookup, reading a stored nanopub from the test suite, an unknown artifact code, and isAccessible().
  • Full suite: 1279 tests, 0 failures.

🤖 Generated with Claude Code

nanopub-java declared the legacy all-in-one org.mongodb:mongo-java-driver
3.12.14 (end of life) as a compile dependency, so every project using
the library got a copy of com.mongodb.* and org.bson.*, clashing with the
modern driver in projects that declare one themselves (e.g.
nanopub-registry).

The dependency is now org.mongodb:mongodb-driver-sync 5.13.0, marked
optional: it is only on the classpath of projects that declare a driver.
The rest of the library loads no MongoDB class. NanopubDb is ported to
the current API (MongoClients, MongoDatabase, MongoCollection), so
getMongoClient() now returns com.mongodb.client.MongoClient.
NanopubStream needed no change. The command-line jar still includes the
driver, as the assembly packs the project's optional dependencies.

Projects that use NanopubDb or NanopubStream.fromMongoCursor have to
declare org.mongodb:mongodb-driver-sync themselves.

Closes #166

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the MongoDB driver an optional dependency

1 participant