( Note: You can read the full series at: https://blog.mandraketech.in/series/java-http-server )
Now that there is enough context on the objectives of this series, let's get started with setting up our dev environment.
My preference is to use Docker, and Visual Studio Code (VSCode). With this combination, I can set up a devcontainer ( https://containers.dev ) and hence have nothing to install on my machine. This allows me to control the tools that will be available to my application, on the same machine, in production. And also, set up a database, and reverse proxy along with.
Recently I figured that there is a way to set up a reverse ssh-tunnel too, with the help of Cloudflare ( https://www.cloudflare.com/en-in/products/tunnel/ ). There are various other offerings, but I am going with the recommendation of the maintainer of Awesome Tunneling.
Based on the above two approaches, and a custom Docker image for JDK (improvised from my work published in Installing JDK in Docker ), I have created a new git repo ( https://gitlab.com/mandraketech/java-vscode-dev ). This is a simple dev environment that will create a Java dev environment. It has a JDK installed ( Liberica or Eclipse Temurin, based on the env file ), Maven and a script, in the tools directory, that will fetch the latest versions for both those tools, so you can update, on need. There is, in the README.md
, instructions to create an Empty Java maven project using the maven-archetype-quickstart
, in case you are looking for a fresh start. The .devcontainer.json
contains instructions on running one of the three available environments:
With Cloudflare Tunnel ( uses a ssh tunnel, and certificates for https support )
With Caddy as the reverse-proxy / loadbalancer ( supports https )
Port directly exposed via devcontainer tunneling
So, let me get started with creating a new environment. I have git, Docker and VS Code installed. Since all the development will happen within the DevContainer, the OS in use, for the dev environment, will only matter in the first few steps.
First step, is to fork the above dev environment, and create a project on Gitlab. When forking, I am only going to fork the main
branch, because my project does not need the dev environment's local branches.
I am going to call it "HttpServer based Todo App", and set up the security policies. It is going to be a public repo, so you are welcome to use/fork/contribute merge requests.
git clone git@gitlab.com:mandraketech/httpserver-based-todo-app.git
Start VS Code with the source directory selected, in the above case http-server-based-todo-app
.
If you want to pull the dev environment from the "upstream" repo, when it is updated, the instructions are in the tools/README.md
And then I am going to create a .env
file ( using the .env.template ) to set up the external name using which we will connect to the server. I am going to use localhost:8080
, for now, since I am working with the default container environment that forwards the port from VSCode.
Now, "Reopen in Container" and we have a dev environment with Java installed. Open a terminal inside the container.
First thing is to generate an empty java project, and make sure we have everything in place to proceed with development.
cd ~/code
mvn archetype:generate -DgroupId=com.example -DartifactId=sample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mv sample/* .
rmdir sample
mvn wrapper:wrapper -Dtype=only-script
After this, there is a new java project, with a pom file, and the maven-wrapper.
Let's enable Java 21. In pom.xml
add the lines
<properties>
<maven.compiler.release>21</maven.compiler.release>
</properties>
Test the setup:
./mvnw clean compile
java -cp ./target/classes com.example.App
./mvnw test
You should see the Hello World
output. And the test results.
Add the mandatory .gitignore
to make sure we do not commut unnecessary files:
# if you are on mac
.DS_Store
# .env is a "private" file. Do not commit
.env
# java build directory
target
Commit this code, to the repo. Now we are ready to get to the Http Server piece in the next article.