Setup and basic Commands for Maven

Setup and basic Commands for Maven

Creating a VM in Azure and adding the port.

  1. Login to Azure portal

  2. Create a VM,

  3. After creating VM, add an inbound rule in VM's network port 8080.

    Below are the command to JDK as it is pre-requisite to install maven,

     sudo apt-get update -y
     sudo apt install openjdk-11-jre -y #JDK installation
    
     sudo apt-get install maven -y #Install Maven
    

  4. Now Cloning the Java repository,

     git clone <Copied github_URL>
    

  5. Apache Maven has a well-defined build lifecycle that consists of phases and goals. These lifecycles is our focus area,

  • Clean Lifecycle

    clean: Removes all files generated by the previous build. This phase helps ensure a clean slate for the next build.

  • Default Lifecycle

    validate: Checks the project's structure and dependencies for correctness.

    compile: Compiles the project's source code.

    test: Runs unit tests using a suitable testing framework.

    package: Takes the compiled code and packages it into a distributable format (e.g., JAR, WAR, or EAR).

    After the above command, you may run the application by typing below command,

      java -jar <application_name.jar> # It will run the application
    

    Now copy the VM public URL and paste in new tab of your browser in below format to see your app running.

      https://<Maven_VM_public_IP>:8080/
    

    verify: Runs integration tests to verify the correctness of the package.

    install: Installs the package into the local repository for use as a dependency in other projects.

    deploy: Copies the package to a remote repository for sharing with other developers and projects.

Here are some commonly used mvn commands:

  1. mvn clean: This command cleans the project by deleting the target directory and other build artifacts. It's often used to start with a clean slate before building the project again.

     mvn clean
    
  2. mvn compile: This command compiles the Java source code in your project.

     mvn compile
    
  3. mvn test: This command runs unit tests in your project.

     mvn test
    
  4. mvn package: This command packages your project into a distributable format (e.g., JAR, WAR, or EAR) without installing it to the local repository.

     mvn package
    
  5. mvn install: This command compiles, tests, and packages your project, and then installs it in your local Maven repository (~/.m2/repository). Other projects can use it as a dependency.

     mvn install
    
  6. mvn deploy: This command deploys your project to a remote Maven repository. It's often used to share your project with other developers or teams.

     mvn deploy
    
  7. mvn clean install: This is a common combination of commands. It cleans the project, compiles, tests, packages, and installs it to your local repository.

     mvn clean install
    
  8. mvn clean package: This command cleans the project, compiles the code, and packages it without installing it to the local repository.

     mvn clean package
    
  9. mvn dependency:tree: This command displays the dependency tree for your project, showing all dependencies and their versions.

     mvn dependency:tree
    
  10. mvn archetype:generate: This command generates a new Maven project from a predefined template or archetype. It will prompt you to choose an archetype and provide project details.

    mvn archetype:generate