-
Notifications
You must be signed in to change notification settings - Fork 29
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
Swift2Java package plugin should fetch dependencies #135
Comments
Ugh, so the eclipse/maven APIs are a bit of a pain to work with... maybe we can work it out though. Or look into the alternatives. It might be also worth looking into using Gradle as an API |
The gradle API does still require we create a project to work on it seems. We could use a virtual file system though. plugins {
id("java")
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation("dev.gradleplugins:gradle-api:8.10.1")
}
tasks.test {
useJUnitPlatform()
} import org.gradle.tooling.GradleConnector;
import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
String[] dependencies = {
"org.apache.commons:commons-lang3:3.12.0",
"com.google.guava:guava:33.3.1-jre",
"com.fasterxml.jackson.core:jackson-databind:2.18.0",
};
var classpath = getClasspathWithDependency(dependencies);
System.out.println("classpath = " + classpath);
}
/**
* May throw runtime exceptions including {@link org.gradle.api.internal.artifacts.ivyservice.TypedResolveException}
* if unable to resolve a dependency.
*/
private static String getClasspathWithDependency(String[] dependencies) throws IOException {
File projectDir = Files.createTempDirectory("java-swift-dependencies").toFile();
projectDir.mkdirs();
File buildFile = new File(projectDir, "build.gradle");
try (PrintWriter writer = new PrintWriter(buildFile)) {
writer.println("plugins { id 'java-library' }");
writer.println("repositories { mavenCentral() }");
writer.println("dependencies {");
for (String dependency : dependencies) {
writer.println("implementation(\"" + dependency + "\")");
}
writer.println("}");
writer.println("""
task printRuntimeClasspath {
def runtimeClasspath = sourceSets.main.runtimeClasspath
inputs.files(runtimeClasspath)
doLast {
println("CLASSPATH:${runtimeClasspath.asPath}")
}
}
""");
}
File settingsFile = new File(projectDir, "settings.gradle.kts");
try (PrintWriter writer = new PrintWriter(settingsFile)) {
writer.println("""
rootProject.name = "swift-java-resolve-dependencies-temp-project"
""");
}
var connection = GradleConnector.newConnector()
.forProjectDirectory(projectDir)
.connect();
try (connection) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
connection.newBuild().forTasks(":printRuntimeClasspath")
.setStandardError(new NoopOutputStream())
.setStandardOutput(printStream)
.run();
var all = outputStream.toString();
var classpath = Arrays.stream(all.split("\n"))
.filter(s -> s.startsWith("CLASSPATH:"))
.map(s -> s.substring("CLASSPATH:".length()))
.findFirst().orElseThrow(() -> new RuntimeException("Could not find classpath output from ':printRuntimeClasspath' task."));
return classpath;
}
}
private static class NoopOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
// ignore
}
}
} which gives us:
|
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:
Should be a pretty stable way to get the job done.
We can get a classpath for dependencies:
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:
but it'd pull in Scala...
The text was updated successfully, but these errors were encountered: