Maven Hello World

🚚 Getting better in Java development

Maven Hello World

Maven is an incredible tool and also a standard in the constructions of projects in Java. One of the principal advantages it has is the dependencies management and that like JUnit and other ones you can find on MavenRepository. And all of this centered in a file called POM.xml.

📦 How to install Maven?

One of the few requirements to use Maven is the JDK installed on the system (You can check if you have it running the next command java --version).

Maven for Windows

If you're on Windows, Maven can be downloaded here: Maven

The file that you need to download is called Binary Zip Achieve

And finally you'll need to setup the enviroment variable:

  1. Firt you need to unzip your file in a route that you want to have it (And not move it again).
  2. For this you'll now to enter the Advanced system configuration
  3. Now you'll have to make the variable JAVA_HOME defined.
  4. And now you'll need to create two new variables pointing to the same direction you left the maven. In my case is C:\Users\User\.maven\apache-maven-3.6.3\.
  5. And finally, we need to edit the variable path adding the new route pointing to %M2_HOME\bin.

Maven for Linux Distributions

If you're awesome and you're using Linux (or WSL on Windows) you can install maven with your package manager:

  • Ubuntu (WSL and derivated):
    • sudo apt install mvn
  • Arch (And derivated):
    • sudo pacman -S mvn

✏ How to create projects in Maven

First we need to create a folderto create a project and get in it with the terminal.

Once into the folder of the project we can begin to run our command of maven (The basic command is mvn)

Acoording the quick guide of maven (that you can read it here, we can use this command to create a project with all the structure of Maven:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app

Which will be like this:

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

Where the archetype:generate indicates that we're creating a project. -DgroupId=com.mycompany.app defines the estructure of our project (how it's different from others) while -DartifactId=my-app generates the name of our compiled file.

Off course that the command will ask us for a lot of information and parameters, that's why if we want to make them all by default, we can use the -DinteractiveMode=false parameter, that can remove the interactive mode.

And finally one of the other parameters that can be useful is the -DarchetypeArtifactId=maven-archetype-quickstart one, you can use it to indicate the kind of application that you want to develop. You can read about the applications here

⚙ Compile with Maven

One of the most incredible Maven advantages is the compile and package features. We can use this with the next command:

mvn compile

This will generate the target folder where we have our classes and our compiled files from the project. Now, if we want to get the executable file (jar) of the project, now we need to use this one:

mvn package

Now, in order to clean our target folder, we must use the next command:

mvn clean