I have been “coding” in Java for more than a year now. I am pretty good at solving coding puzzles, using complicated inheritance, and getting a pretty good AP scores. However, what I didn’t learn at schools or online coding community was, how to create a real-world project that uses public open-source libraries, and package my code into a package that can be used by my team, and even the open source community.
Overview
This is where a package management aad build automation tools such as Maven come in. I picked Maven because:
- Automates the build process: Maven automates the build process by providing a standard directory structure and a set of plugins that can be used to perform common tasks, such as compiling code, running tests, and packaging the application. This makes it easy to build and deploy Java applications, and it helps to ensure that the build process is consistent across different environments.
- Manages dependencies: Maven automatically manages dependencies by downloading the required JAR files from a central repository. This saves developers the time and effort of manually downloading and managing dependencies. It also ensures that the correct versions of dependencies are used, which can help to avoid compatibility issues.
- Provides a standard project structure: Maven enforces a standard project structure, which makes it easy to understand and navigate Java projects. This structure includes directories for source code, test code, resources, and documentation.
- Is extensible: Maven is extensible, which means that developers can create their own plugins to add additional functionality. This makes Maven a flexible tool that can be customized to meet the specific needs of different projects.
Overall, Maven is a powerful and versatile tool that can help to improve the development, deployment, and maintenance of Java applications.
References
I found the following references helpful to start using Maven in my Java projects: -Building Java Projects with Maven -Maven in 5 Minutes
Use Maven
First, install Maven
choco install maven
Then, run the following command
mvn archetype:generate -DgroupId=com.xyz.abcapp -DartifactId=appabc -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
which will create a project “appabc” with a single Java file in com.xyz.abcapp.App
└── src
└── main
└── java
└── com
└── xyz
└── abcapp
└── App.java
Helper Class
Add a helper class under util
package com.xyz.util;
public class Person {
public String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
Change the App.java to below code that calls Person class.
package com.xyz.abcapp;
import com.xyz.util.Person;
public class App
{
public static void main(String[] args) {
App myObj = new App();
Person you = new Person("foo");
System.out.println("Your name is " + you);
}
}
You can ‘package’ your two class codebase into a jar file by running mvn package
. You will see a jar file shows up under target directory. You can then run it by java -jar target/xxx.jar
External Jar
Now you can all the helper class ‘Person’ in the library (.jar) from any external java class. Here is an example (test.java)
import com.xyz.util.Person;
public class Test
{
public static void main(String[] args) {
Test myObj = new Test();
Person you = new Person("from Test.java");
System.out.println("I can use a package! The output is " + you);
}
}
Add a dependency in Maven
Here is how you can add an open source library to your project:
- Go to mvnrepository
- Search for the library you want to add
- Copy the dependency statement into your pom.xml’s dependencies section
- rebuild via mvn, and the library and all its dependencies will be automatically downloaded