Java build tools
You'll probably learn Maven but this is not the only tool out there happily... If you need to package your application as an independant .jar file, this page will also help you figure it out.
Gradle - when you are sick of Maven...
The default tool is Maven (mvn command) but you will quickly find that error messages are useless, that it is pretty slow to compile, that there is no run and init commands which is very annoying when you don't use IntelliJ. If you struggled to manage plugins in dozens of XML lines in pom.xml, you might appreciate the fact that adding a plugin in gradle in a single line change.
Init a project
There is gradle init command ! Run it without arguments if you want the prompts, or give them all at once.
See command details
gradle init \
# Setup a Java application
--type java-application \
# Using the Kotlin DSL to define build configurations
--dsl kotlin \
# Enable the default test framework JUnit
--test-framework junit-jupiter \
# Choose the package name, the only folder under app/src/main/java
--package scratch \
# The project names impacts the resulted jar
--project-name scratch \
# The regular structure used for a single project
--no-split-project \
# No incubating features that might change in the future
--no-incubating \
--java-version 21
You want a faster command to create scratch Gradle projects ? Use scr gradle after installing scr (lxup get scr Help: I don't have lxup !)
Run a project outside of IDE
There is gradle run command !
gradle run
And... there is an integrated watch mode 🔥🔥 ! This flag -t comes from continuous mode, not very intuitive I agree. During evaluations, this watch mode can be very handy.
gradle run -t
There is a quiet mode with -q, to show less output.
gradle run -t -q
There is sadly no easy and stable way to clear output between each run...
You want a faster command to run Gradle projects ? Use r after installing it (lxup get r Help: I don't have lxup !). The watch mode is available through the r -w command.
Uber JAR / Fat JAR - when your app.jar doesn't start
This is a very frustrating moment (if not hours searching for solution) because nobody told you why it is needed, your run mvn package and java -jar target/.../app.jar and it fails with a ClassNotFoundException stack trace... That's because the JAR doesn't include.
If you need to build a portable JAR (archive with all compiled Java classes), you need to include all dependencies, .. This is called a fat JAR or uber JAR.
With Gradle
See this article here on Creating "uber" or "fat" JARs - Gradle docs. You'll then be able to call a custom task like this: gradle uberJar.
With Maven
It's possible by dropping the maven-jar-plugin in your pom.xml, under project > build > plugins tags. It will impact how mvn package bundle everything.
See XML snippet
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>