devbox shell before generating your Maven or Gradle
projects, so that the tools use the right version of the JDK for creating your project.
Example Repo
Adding the JDK to your project
devbox add jdk binutils, or in your devbox.json
devbox search jdk.
Other distributions of the JDK (such as OracleJDK and Eclipse Temurin) are available in Nixpkgs, and
can be found using
NixPkg Search
Gradle
Example Repo Gradle is a popular, multi-language build tool that is commonly used with JVM projects. To setup an example project using Gradle, follow the instructions below:-
Create a project folder:
my-project/and calldevbox initinside it. Then add these packages:devbox add jdkanddevbox add gradle.- Replace
jdkwith the version of JDK you want. Get the exact nix-pkg name fromsearch.nixos.org.
- Replace
-
Then do
devbox shellto get a shell with thatjdknix pkg. -
Then do:
gradle init-
In the generated
build.gradlefile, put the following text block: -
While in devbox shell, run
echo $JAVA_HOMEand take note of its value. -
Create a
gradle.propertiesfile like below and put value of$JAVA_HOMEinstead of <JAVA_HOME_VALUE> in the file.
-
In the generated
-
gradle buildshould compile the package and create abuild/directory that contains an executable jar file. -
gradle runshould print “Hello World!”. -
Add
build/to.gitignore.
devbox.json would look like the following:
Maven
Example Repo Maven is an all-in-one CI-CD tool for building testing and deploying Java projects. To setup a sample project with Java and Maven in devbox follow the steps below:- Create a dummy folder:
dummy/and calldevbox initinside it. Then add the nix-pkg:devbox add jdkanddevbox add maven.- Replace
jdkwith the version of JDK you want. Get the exact nix-pkg name fromsearch.nixos.org.
- Replace
- Then do
devbox shellto get a shell with thatjdknix pkg. - Then do:
mvn archetype:generate -DgroupId=com.devbox.mavenapp -DartifactId=devbox-maven-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false- In the generated
pom.xmlfile, replace java version in<maven.compiler.source>with the specific version you are testing for.
- In the generated
mvn packageshould compile the package and create atarget/directory.java -cp target/devbox-maven-app-1.0-SNAPSHOT.jar com.devbox.mavenapp.Appshould print “Hello World!”.- Add
target/to.gitignore.
devbox.json would look like the following: