Skip to content

Instantly share code, notes, and snippets.

@GavinRay97
Created December 8, 2022 21:21
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GavinRay97/bd7e36f3db9d3414b680d30a70f71f32 to your computer and use it in GitHub Desktop.
Save GavinRay97/bd7e36f3db9d3414b680d30a70f71f32 to your computer and use it in GitHub Desktop.
A Makefile to compile a Maven/Gradle-style project layout using a custom JDK (for use w/ IE, Valhalla)
[user@MSI java-jdk20-valhalla]$ make run
Java source files: src/main/java/org/example/Database.java src/main/java/org/example/PageLocation.java
Java class files: target/classes/org/example/Database.class target/classes/org/example/PageLocation.class
/home/user/downloads/jdk-20-vahalla-20-75/bin/java --enable-preview -cp target/classes org.example.Database
Hello World!
Page Location: PageLocation[databaseId=1, pageId=2]
# Makefile to compile and run Java sources manually
# because JDK 20 Valhalla support is not yet available in IntelliJ IDEA
JAVA_VERSION = 20
JAVAC = /home/user/downloads/jdk-20-vahalla-20-75/bin/javac
JAVA = /home/user/downloads/jdk-20-vahalla-20-75/bin/java
JAVA_COMPILE_OPTIONS = --enable-preview --release $(JAVA_VERSION)
JAVA_OPTIONS = --enable-preview
JAVA_MAIN_CLASS = org.example.Database
JAVA_SOURCES = $(wildcard src/main/java/**/**/*.java)
JAVA_CLASSES = $(patsubst src/main/java/%.java, target/classes/%.class, $(JAVA_SOURCES))
# Compile the Java source files
compile: $(JAVA_CLASSES)
$(info Java source files: $(JAVA_SOURCES))
$(info Java class files: $(JAVA_CLASSES))
# Run the Java main class
run: compile
$(JAVA) $(JAVA_OPTIONS) -cp target/classes $(JAVA_MAIN_CLASS)
# Clean the target directory
clean:
rm -rf target
# Compile the Java source files
target/classes/%.class: src/main/java/%.java
$(JAVAC) $(JAVA_COMPILE_OPTIONS) -d target/classes $<
# Create the target directory
target/classes:
mkdir -p target/classes
# Make the target directory a dependency of the Java class files
$(JAVA_CLASSES): target/classes
# Make the target directory a dependency of the compile target
compile: target/classes
# Ditto...
clean: target/classes
run: target/classes
default: target/classes
default: run
// ./src/main/java/org/example/Database.java
package org.example;
class Database {
public static void main(String[] args) {
var examplePageLocation = new PageLocation(1, 2);
System.out.println("Hello World!");
System.out.println("Page Location: " + examplePageLocation);
}
}
// ./src/main/java/org/example/PageLocations.java
package org.example;
public value record PageLocation(int databaseId, int pageId) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment