What is build?
R: The process by which source code is converted into a stand-alone form that can be run on a computer or to the form itself. (Source: Techopedia)
In Java, build is the generation process of WAR, EAR, JAR, etc… There are many ways of doing this, such as:
- External Tools
Among the external tools for the build, are the Ant, Maven and Gradle. I will talk about Maven.
Build Process (Java)
Below, the drawing of a simple build process.
For a simple class that uses two JARs, the manual build process could be this:
javac -d ./src -cp lib/random_jar01.jar:lib/random_jar02.jar src/main/java/com/wordpress/ralphavalon/MyClass.java
jar cfv myApp.jar com/wordpress/ralphavalon/MyClass.class
It sounds simple. However it’s only 1 class and 2 libs. And I didn’t mention the MANIFEST¹.When it is a bigger project, there are several classes, several libs, resource files, different packages and folders divisions, sometimes projects have other projects as a dependency to compile, etc.
¹ Manifest.MF is a file that is used to identify the JAR, including metadata, information about the main class to run and classpath.
What is Maven?
Maven is a project integration tool. It’s best known for managing dependencies, but can also run tests, generate productivity reports, maintain code quality level (with code coverage testing) and many other things.
In addition to other features, Maven also does the project build process. With it, you can generate your artifact easily and quickly. A simple command, by default, download the configured dependencies, put them on Classpath, compiles the entire project, runs the tests (if any, and if they are in the appropriate structure) and generates the JAR / WAR with the resources in the appropriate structures.
It’s noteworthy that Maven is highly configurable, so if you want Maven to do something else between these processes, or do not do something, you can also configure. You can also set up independent tasks, such as a task to download and compile a WebService on the Internet or run SQL scripts.
How Maven works?
The basis for the use of Maven is pom.xml (Project Object Model). This file contains all the settings to be executed for the project, such as processes, dependencies, plugins, etc. This is an example of how it could be the pom to the case described above:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>applicationGroupId</groupId> <artifactId>myApp</artifactId> <version>1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>groupIdOfTheJar</groupId> <artifactId>random_jar01</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>groupIdOfTheJar</groupId> <artifactId>random_jar02</artifactId> <version>1.0</version> </dependency> </dependencies> </project>
- But uncle Ralph, and if a lib depends of another lib?
A: Maven takes care of that and download the dependencies of a dependency.
- But uncle Ralph, and if I have many dependencies? I need to decorate groupId, artifactId and version of all of them?
A: No. You just need to know which JAR you want and everything else you can find on sites such as: http://mvnrepository.com/ or so http://search.maven.org/. Many libs nowadays, in their own official website now provide the dependency definition for Maven. You can also set up external repositories to download some libs.
Appendix: Maven with IDEs
Currently, IDEs already have native support for Maven, so many of the things that are already automated, are even simpler, as the simple fact of adding a dependency in pom.xml and automatically the dependency is downloaded without the need of some command.
That’s it, folks. I hope you enjoyed.