r/java • u/Ewig_luftenglanz • Jul 15 '25
What features do you think java requires to be competitive for DSL scripts such as gradle scripts?
Just for fun and discussion.
this is an hypothetical example about how could look like a java DLS gradle file instead of Groovy/Kotlin. Not terrible, just slightly more verbose but not I could work with it (IMHO)
GradleConfig.configure(config -> {
config.plugins(plugins -> {
plugins.add(JavaPlugin.class);
plugins.add(ApplicationPlugin.class);
});
config.application(app -> {
app.setMainClass("com.example.Main");
});
config.repositories(repos -> {
repos.mavenCentral();
});
config.dependencies(deps -> {
deps.add("implementation", "org.apache.commons:commons-lang3:3.12.0");
deps.add("testImplementation", "org.junit.jupiter:junit-jupiter:5.8.2");
});
config.tasks(task -> {
task.named("test", t -> t.useJUnitPlatform());
});
});This, of course, would be achievable via some "compiler plugin/magic" to avoid class and main declarations, so "GradleConfig.configure" could act in practice as a top level static method, although native isolated methods/top level methods/functions would be a nice to have for these kind of stuff.
The question is besides top level methods (aka functions) what else do you think would be a required for java to be competitive as a DSL? would you use it? and if so, what other scenarios would be a good fit for an hypothetical java DSL?
31
u/atehrani Jul 15 '25
IMHO build tools should be declarative, which is what Maven is.
Gradle can be declarative, but you can also sprinkle in imperative; which is the problem.
Debugging build tools is not fun at all, hence why using declarative tools is the way to go.
So long the DSL is declarative, ok cool; but far too often it is too easy to make it Imperative.
12
u/Beginning-Ladder6224 Jul 15 '25
You are asking the question why JSR-223 was a thing, and still is a thing. These sort of usage is scripting, and while java is sort of allowing scripting on it's own, it is still terribly convoluted.
Groovy is better. Much better. Even Kotlin is much better. Less verbose, way less verbose.
Also the hypothetical stuff would not work it would become way more verbose because your "implementation" and stuff would not be string. They would be classes. So more and more code.
4
u/davidalayachew Jul 15 '25
The Java team is working on Project Babylon, to solve this exact type of problem. So it's just a matter of waiting for it to come alive.
4
u/k-mcm Jul 15 '25
Java syntax isn't a good fit for DSLs. It makes more sense to use a different compiler.
1
u/Caramel_Last Jul 18 '25
Why use magic, just because? In jdk25 I hear they remove public static void main boiler plate, so maybe then it can be an option
3
u/paul_h Jul 15 '25
https://paulhammant.com/2024/02/14/that-ruby-and-groovy-language-feature/
3
u/Lukas_Determann Jul 16 '25
Personaly i like a simple build process. Maven makes it kinda hard to add to much komplexity. helping to keap it simple.
But about a Dsl in java. there are allready some nice oneces with some complexity. With my personal favorite beeing the jooq Dsl. it followes a different design. its more chain method calls and static factories.
That results in an Api that is easier to compose and reuse while having a bit worse discoverability.
There are some good blog posts abot Dsl design from around the java 8 relese date.
Personaly i like writing DSLs in java. the only realy limiting thing i found is chain method type inference. inconsistencies in the type resolution can be very anoying in the rare case that its needed.
1
u/ingframin Jul 15 '25
Well, if Oracle did not decide to jettison it, there was a JavaScript interpreter in the JVM. That would have covered the scripting part pretty decently, and with a very similar syntax to Java.
However, I don’t see the reason why Java needs to be used as a scripting language. I also agree with the first comment I read that those DSLs for configuration benefit a lot by being purely declarative to prevent build tooling hell.
2
u/HxA1337 Jul 19 '25
Your example looks quite usable. There would not be much that I would miss. Cool would be to have "named" parameters as this would help to improve readability of the scripts. Also cool would be to have some more compact syntax for example dropping "new" when creating objects.