Getting started with Java and Scala projects using Maven, intellij
Coming from a Python/Php and/or systm administration bacground with no little to Java experience is daunting.
- POM? What is that? (Is this like
setuptools
?) Not really, but sort of - Maven? What is it?
- How do I even run this thing?
- What's a war file?
- Why can't I just run this, can I run it without intellij?
- Why Scala? I thought this was Java
What is POM?
What is a POM file?
POM is a file needed by Apache Maven, which is "a software project management and comprehension tool."
The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.
Read & follow: Maven in 5 mins
Read an introduction to POM.
Why can't I just run this, can I run this java project without intellij? Production? How?
If you come with a production first mindset, you will want to know how to get into production as fast as possible. Removing an IDE from the process is the first step for this, and really understanding the application in the wild west of production is key to understanding the whole project.
If using Maven, you can use this tool to build and run the project, inspect the build artifacts like .war
files for java web based projects. For example, Jetty (a Java based web server), can be given a .war
file as a complete packaged unit web application. After using mvn package
, the target
folder gets populated with a .war
file (for web based projects) which can be given to jetty for running. This is analogous to placing a website into your /var/html/example.com
directory which apache2 would then read. For understanding what is a .war
file, see deployment web apps from the official jetty docs.
Why Scala? I thought this was Java
Different ways to reason about code are useful for tackling different problems and Scala is no exception. Also, different people find it easier to model problems in different ways. Scala is no exception to this. Practically, scala runs ontop of Java; but the code yu write is syntactically very different.
Pattern matching for me was one 'oh wow that's interesting' moments, a feeling of glee when another way of modeling and describing a problem just feels more appropriate. See Pattern Matching docs for an example.
Again, just practically speaking if you're used to Java then you will be supprised how little boiler plate code is needed, so the text you see before you is more directly modeling the problem you're tying to solve. But this isn't unique to Scala, you might say this for Python code too for example.
Maven? What is it?
Maven will read your pom.xml
file to build, package, and run your project (provided that's defined within your pom file. Maven has a concept of goals
which are pretty standard (build, package, instal) etc, but you can also create your own. It's typical when working with standard maven project to:
- Clone the project with
git clone <repo-url>
- Download all dependencies, build it:
mvn build
,mvn package
,mvn run
- Note that if you skip to just
mvn install
then all the previous 'steps' are carried out first automatically.
Maven cache directory
It's typcally the folder ~/.mvn
on linux based systems. When maven downloads your dependencies, it typically gets them from https://mvnrepository.com/repos/central , which is just like https://www.npmjs.com/package/ if you come from node or https://pypi.org/ package index if you come from python etc.
This folder gets very large for large projects, and simply saves you from downloading packages from the internet every time. Hint some maven projects allow you to do mvn -o install
which stores all related dependencies offline so you can build without an internet connection.