Maven Quickstart – Live Learning Notes

I went through the quickstart of Java. My next step is to get familiar with Maven as it has become a defacto of working on a java project. So people either use Maven or Gradle. And I am also going to use Maven. So let’s get started.

Maven has 2 quickstart written on their official website. One is 5 mins long and another one is 30 mins long. I am going ahead with 30mins one. #1 https://maven.apache.org/guides/getting-started/index.html

As written there: Maven is essentially a project management and comprehension tool, because it takes care of all of it:

  1. Builds
  2. Documentation
  3. Reporting
  4. Dependencies
  5. SCMs
  6. Releases
  7. Distribution

Install

  1. Download first: #2 https://maven.apache.org/download.cgi
  2. Install the downloaded package.
  3. Verify the installation with mvn --version

Initialise a maven project

We will also use archetype #3 https://maven.apache.org/guides/introduction/introduction-to-archetypes.html
Now, from the command line: mvn archetype:generate

It will ask you questions, I went with defaults (just pressed enters). And some I needed to provide (for which there was no default value)

Define value for property 'groupId': com.mycompany.app
Define value for property 'artifactId': maven-learning
Define value for property 'version' 1.0-SNAPSHOT: : 0.0.0
Define value for property 'package' com.mycompany.app: :
Confirm properties configuration:
groupId: com.mycompany.app
artifactId: maven-learning
version: 0.0.0
package: com.mycompany.app
 Y: :

and my build was successful.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  05:56 min
[INFO] Finished at: 2021-01-05T16:29:07+05:30
[INFO] ------------------------------------------------------------------------

My maven-learning project directory structure is like this:

maven-learning
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── mycompany
    │               └── app
    │                   └── App.java
    └── test
        └── java
            └── com
                └── mycompany
                    └── app
                        └── AppTest.java

11 directories, 3 files

I also tried to start the project using my IDE (IntelliJ CE 2020.3.1)

And then setting up properties:

Hit “Next” and again my project tree is same.

maven-learning-ide
├── maven-learning-ide.iml
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── mycompany
    │               └── app
    │                   └── App.java
    └── test
        └── java
            └── com
                └── mycompany
                    └── app
                        └── AppTest.java

11 directories, 4 files

Let’s Compile and run tests

Now from the project directory you can try running commands like:

  1. mvn compile #this compiles and throw compilation errors
  2. mvn test #runs the tests. In my case AppTest.java is the only file
  3. mvn clean #this cleans the existing project build
  4. mvn package #this first compiles and create JAR file for your project in “project_root/target” dir.
  5. mvn install #this install the JAR created by package command.

After compiling, I tried running my App.java and AppTest.java from the IDE. And they are running fine.

Adding more dependencies

Referred: #4 https://maven.apache.org/guides/getting-started/index.html#how-do-i-use-external-dependencies

Adding “log4j” for logging. In order to add dependencies you need to add them in the “pom.xml” of your project. I can notice “Junit” is already there as a dependency, that is why we were able to run the test.

<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
      <scope>compile</scope>
</dependency>

After adding this dependency just run the mvn compile command. And maven will install the dependency for us. You may notice in its output that maven has detected some changes.

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.mycompany.app:maven-learning-ide >----------------
[INFO] Building maven-learning-ide 0.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ maven-learning-ide ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ maven-learning-ide ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/shiva/Temp/maven-learning-ide/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.068 s
[INFO] Finished at: 2021-01-05T17:14:26+05:30
[INFO] ------------------------------------------------------------------------

There are more topics in the getting started guide. Which are looking equally interesting: Here, I am adding the links to those parts:

  1. https://maven.apache.org/guides/getting-started/index.html#how-do-i-deploy-my-jar-in-my-remote-repository
  2. https://maven.apache.org/guides/getting-started/index.html#how-do-i-create-documentation
  3. https://maven.apache.org/guides/getting-started/index.html#how-do-i-build-other-types-of-projects

For now I would just like to go into “How do I create documentation” (the second in the list above)

  1. ran mvn site from the project root and also mvn compile
  2. noticed the new files in target folder in the project
  3. And I don’t want to deploy this site to GitHub. I just want to see this site on my local. To do that I ran the following command.
  4. mvn site:run
  5. Go to http://localhost:8080/

This was so awesome!! I enjoyed the maven so far. But the point is I have to get familiarise with other techs as well. Therefore, I am leaving this post here.

Thanks!

2 thoughts on “Maven Quickstart – Live Learning Notes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s