summaryrefslogtreecommitdiff
path: root/modules/templates/java
diff options
context:
space:
mode:
Diffstat (limited to 'modules/templates/java')
-rw-r--r--modules/templates/java/README.md91
-rw-r--r--modules/templates/java/flake.nix40
2 files changed, 131 insertions, 0 deletions
diff --git a/modules/templates/java/README.md b/modules/templates/java/README.md
new file mode 100644
index 0000000..7cc5735
--- /dev/null
+++ b/modules/templates/java/README.md
@@ -0,0 +1,91 @@
+# Java Development Environment
+
+A simple Nix Flake providing a reproducible Java development environment.
+
+## Included Tools
+
+* **JDK 21** – Java compiler and runtime
+* **Maven** – Dependency management and project build tool
+* **Gradle** – Alternative build system
+* **JDT Language Server** – Language server used by many editors (Neovim, VS Code, Helix, etc.)
+
+## Usage
+
+Enter the development shell:
+
+```bash
+nix develop
+```
+
+When the shell starts, it will display the installed Java version and create an `out/` directory for compiled classes if it doesn't already exist.
+
+## Helper Commands
+
+The shell provides a few convenience aliases for small Java projects.
+
+### `jbuild`
+
+Compiles every Java source file under `src/` into the `out/` directory.
+
+```bash
+jbuild
+```
+
+Equivalent to:
+
+```bash
+find src -name "*.java" | xargs javac -d out
+```
+
+---
+
+### `jrun`
+
+Builds the project and runs:
+
+```text
+src.Main
+```
+
+```bash
+jrun
+```
+
+This assumes your application's entry point is `src.Main`.
+
+---
+
+### `jclean`
+
+Removes compiled output.
+
+```bash
+jclean
+```
+
+## Project Layout
+
+This template assumes a simple project structure:
+
+```text
+.
+├── flake.nix
+├── src/
+│ └── Main.java
+└── out/
+```
+
+For larger projects, consider using Maven or Gradle instead of the helper aliases.
+
+## Customization
+
+This template is intended as a starting point. You can easily modify it to:
+
+* use a different JDK version
+* add testing frameworks (JUnit, TestNG)
+* include formatting and linting tools
+* add debugging utilities
+* add database or web development dependencies
+
+Because everything is defined in the flake, the development environment remains reproducible across Linux, macOS, and other supported platforms.
+
diff --git a/modules/templates/java/flake.nix b/modules/templates/java/flake.nix
new file mode 100644
index 0000000..d69e2c6
--- /dev/null
+++ b/modules/templates/java/flake.nix
@@ -0,0 +1,40 @@
+{
+ description = "Base dev env for Java";
+
+ inputs = {
+ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
+ flake-utils.url = "github:numtide/flake-utils";
+ };
+
+ outputs = {
+ self,
+ nixpkgs,
+ flake-utils,
+ }:
+ flake-utils.lib.eachDefaultSystem (system: let
+ pkgs = nixpkgs.legacyPackages.${system};
+ in {
+ devShells.default = pkgs.mkShell {
+ packages = with pkgs; [
+ jdk21
+ maven
+ gradle
+ jdt-language-server # LSP for editors
+ ];
+
+ shellHook = ''
+ echo "Java $(java -version 2>&1 | head -1)"
+
+ alias jbuild='find src -name "*.java" | xargs javac -d out && echo "Build success"'
+ alias jrun='jbuild && java -cp out src.Main'
+ alias jclean='rm -rf out && echo "Cleaned"'
+
+ mkdir -p out
+
+ echo " jbuild — compile all sources"
+ echo " jrun — compile and run"
+ echo " jclean — remove compiled output"
+ '';
+ };
+ });
+}