Apache Maven

Maven is a popular build automation tool that is widely used in Java projects. It provides a standard way to manage project dependencies, build configurations, and packaging.

Basic Maven Commands

  • mvn clean: Deletes the target/ directory. This command removes all the compiled output and test results.

  • mvn compile: Compiles the Java source code into bytecode and stores it in the target/classes/ directory.

  • mvn test: Runs the tests for the project.

  • mvn package: Packages the compiled code and resources into a distributable format, such as a JAR or a WAR file.

  • mvn install: Installs the project artifacts into the local Maven repository.

  • mvn deploy: Deploys the project artifacts to a remote Maven repository.

Maven Configuration

  • pom.xml file: This file is the main configuration file for your Maven project. It defines the project metadata, dependencies, plugins, and build configuration.

Project Metadata

  • groupId: Specifies the group or organization that the project belongs to.

  • artifactId: Specifies the name of the project.

  • version: Specifies the version of the project.

  • packaging: Specifies the packaging format for the project, such as JAR or WAR.

Dependencies

  • <dependencies>: This section is used to declare the dependencies for the project. The most common dependency scopes are:

    • compile: Specifies dependencies that are required for compiling the code and for the project to run.

    • test: Specifies dependencies that are required for testing the code.

    • provided: Specifies dependencies that are provided by the runtime environment, such as the Java API.

    • runtime: Specifies dependencies that are required for running the code but not for compiling it.

Plugins

  • <plugins>: This section is used to declare the plugins for the project. Maven provides many built-in plugins, such as compiler, surefire, and jar.

  • <plugin>: This element is used to define a plugin and its configuration.

  • <execution>: This element is used to define an execution of a plugin, which can have multiple goals.

Maven Build Lifecycle

Maven has three built-in build lifecycles: clean, default, and site. Each lifecycle is made up of phases, which represent different stages of the build process.

  • clean: Deletes the target/ directory.

    • pre-clean: Executes tasks before the clean phase.

    • clean: Deletes the target/ directory.

    • post-clean: Executes tasks after the clean phase.

  • default: Builds and packages the project.

    • validate: Validates the project and its dependencies.

    • compile: Compiles the Java source code.

    • test: Runs the tests for the project.

    • package: Packages the compiled code and resources into a distributable format.

    • verify: Verifies the integrity of the packaged code.

    • install: Installs the project artifacts into the local Maven repository.

    • deploy: Deploys the project artifacts to a remote Maven repository.

  • site: Generates documentation and reports for the project.

    • pre-site: Executes tasks before the site phase.

    • site: Generates documentation and reports for the project.

    • post-site: Executes tasks after the site phase.

    • site-deploy: Deploys the generated documentation and reports to a remote server.

Conclusion

Maven is a powerful build automation tool that provides a standard way to manage project dependencies, build

Last updated