Closed
Description
If we're intent on making the "write a swift app, use some java libs" workflow real we're going to need to handle dependencies. Very few libraries are just one jar, and managing this by hand is not really realistic.
So, we need to resolve and fetch dependencies. Thankfully, there's libraries which do this, options include:
Our best bet is probably the Maven resolver:
- maven resolver https://maven.apache.org/resolver/index.html / https://maven.apache.org/resolver/resolving-dependencies.html
Should be a pretty stable way to get the job done.
We can get a classpath for dependencies:
RepositorySystem repoSystem = newRepositorySystem();
RepositorySystemSession session = newSession( repoSystem );
Dependency dependency =
new Dependency( new DefaultArtifact( "org.apache.maven:maven-core:3.9.6" ), "compile" );
RemoteRepository central = new RemoteRepository.Builder( "central", "default", "https://repo.maven.apache.org/maven2/" ).build();
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot( dependency );
collectRequest.addRepository( central );
DependencyNode node = repoSystem.collectDependencies( session, collectRequest ).getRoot();
DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setRoot( node );
repoSystem.resolveDependencies( session, dependencyRequest );
PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
node.accept( nlg );
System.out.println( nlg.getClassPath() );
Other options
Ivy is an option, sbt used to use this, but I'm not sure how good it has been maintained.
Which has very minimal API:
import coursier._
val files = Fetch()
.addDependencies(dep"org.tpolecat:doobie-core_2.12:0.6.0")
.run()
but it'd pull in Scala...