Maven Basics:
What is Maven?
Maven is a build tool that manages the project lifecycle, handling tasks such as compilation, testing, packaging, and dependency management. It uses a Project Object Model (POM) file, typically named pom.xml
, to configure the project.
It simplifies the build process, project management, and dependency resolution.
The Maven build lifecycle consists of a series of phases that define the order in which goals are executed. Here are the standard phases in Maven:
validate: Validates the project is correct and all necessary information is available.
compile: Compiles the source code of the project.
test: Runs tests on the compiled source code using a suitable unit testing framework.
package: Takes the compiled code and packages it in its distributable format, such as a JAR or WAR.
integration-test: Performs integration tests on the package.
verify: Runs checks on the results of integration tests to ensure quality criteria are met.
install: Installs the package into the local repository, for use as a dependency in other projects.
deploy: Copies the final package to the remote repository for sharing with other developers and projects.
These phases are executed sequentially, and each phase is made up of goals. Goals are specific tasks that run in each phase. For example, the compile
phase has a goal of compiling the source code, and the test
phase has a goal of running tests.
mvn clean install Explained:
The mvn clean install
command is a common and powerful Maven command used in the build process.
clean: This phase removes the
target
directory, which contains compiled classes, JARs, and other generated files from previous builds. It ensures a clean slate for the new build.install: This phase executes the default phases up to the
install
phase. It compiles the source code, runs tests, packages the application, and installs it in the local Maven repository (~/.m2/repository
).
In summary, mvn clean install
is often used to perform a full build, ensuring that the project is compiled, tested, and packaged, and the resulting artifact is installed locally.
Other Useful Maven Commands:
mvn clean: Cleans the project by removing the
target
directory.mvn test: Runs the tests of the project.
mvn package: Packages the compiled code into a distributable format, like a JAR or WAR file.
mvn dependency:tree: Displays the project's dependency tree.
mvn help:effective-pom: Shows the effective POM, including inheritance and active profiles.
mvn versions:display-dependency-updates: Displays the dependencies that have newer versions available.
The mvnw Wrapper:
The mvnw
script (or mvnw.cmd
on Windows) is a Maven wrapper script designed to ensure a consistent build environment across different machines. It allows you to use Maven without a globally installed version.
If you liked this blog, you can follow me on twitter, and learn something new with me.