# Maven

Maven is a widely-used build automation tool for Java projects. It simplifies the process of building and managing Java applications by providing a standard project structure, dependency management, and build lifecycle.

Maven uses a project object model (POM) file, typically named `pom.xml`, to define project configuration and dependencies. It downloads dependencies from remote repositories, compiles source code, and packages the project into a distributable format.

### Maven Installation

To install Maven, follow these steps:

1. **Download Maven**: Visit the Apache Maven download page (<https://maven.apache.org/download.cgi>) and download the latest binary distribution for your operating system.
2. **Extract Maven**:
   * On Windows: Extract the downloaded archive to a directory (e.g., `C:\Program Files\apache-maven-x.y.z`).
   * On Linux or macOS: Use `tar` to extract the archive to a directory (e.g., `/opt/apache-maven-x.y.z`).
3. **Set Environment Variables**:

   * On Windows: Add the `M2_HOME` and `MAVEN_HOME` environment variables, and add `%M2_HOME%\bin` to your `PATH`.
   * On Linux or macOS: Add the following lines to your shell profile file (`~/.bashrc`, `~/.bash_profile`, or `~/.zshrc`):

   ```bash
   export M2_HOME=/path/to/apache-maven-x.y.z
   export PATH=$M2_HOME/bin:$PATH
   ```
4. **Verify Installation**: Open a new terminal window and run `mvn -version` to verify that Maven is installed correctly.

### Maven Project Structure

A typical Maven project has a well-defined directory structure, which makes it easy to manage and build. Here's a simplified project structure:

```
my-maven-project/
├── src/
│   ├── main/
│   │   ├── java/          # Java source code
│   │   └── resources/     # Resources like configuration files
│   └── test/
│       ├── java/          # Test source code
│       └── resources/     # Test resources
├── target/                # Compiled classes and built artifacts
└── pom.xml                # Project configuration file
```

### Creating a Simple Maven Project

Let's create a simple Maven project:

1. **Open a Terminal/Command Prompt**.
2. **Navigate to the Directory Where You Want to Create the Project**.
3. **Create a New Maven Project**: Run the following command to create a new Maven project:

   ```bash
   mvn archetype:generate -DgroupId=com.example -DartifactId=my-maven-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
   ```

   Replace `com.example` and `my-maven-project` with your desired group ID and project name.
4. **Navigate to the Project Directory**:

   ```bash
   cd my-maven-project
   ```
5. **View the Project Structure**: You'll see the project structure similar to what was described earlier.

### Adding Dependencies

Maven simplifies dependency management. To add dependencies to your project, edit the `pom.xml` file and add the following dependency block inside the `<dependencies>` element:

```xml
<dependency>
    <groupId>group-id</groupId>
    <artifactId>artifact-id</artifactId>
    <version>version</version>
</dependency>
```

For example, to add the Apache Commons Lang library, your `pom.xml` would include:

```xml
<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>
</dependencies>
```

After editing the `pom.xml`, run `mvn clean install` to download the dependencies.

### Building and Packaging a Maven Project

To build and package your Maven project, use the following Maven goals:

* **Clean**: Removes the `target` directory.

  ```bash
  mvn clean
  ```
* **Compile**: Compiles the source code.

  ```bash
  mvn compile
  ```
* **Test**: Runs tests.

  ```bash
  mvn test
  ```
* **Package**: Packages the project (e.g., creates a JAR or WAR file).

  ```bash
  mvn package
  ```
* **Install**: Installs the project artifact to your local repository.

  ```bash
  mvn install
  ```

These are some of the most common Maven goals. You can execute these commands in your project's root directory.

Maven's capabilities extend far beyond what's covered here, but this guide should give you a solid foundation for managing and building your Java projects with Maven.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codewithmeiy.gitbook.io/core-java/build-architectures/maven.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
