Creating a Java project using Maven

 

What is Maven

Maven is essentially a project management and comprehension tool and as such provides a way to help with managing:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution

    Installation

    Maven is a Java tool, so you must have Java installed in order to proceed.

    First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:

    mvn --version

    It should print out your installed version of Maven, for example:

    Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
    Maven home: C:\Program Files\Maven\apache-maven-3.9.1-bin\apache-maven-3.9.1
    Java version: 17.0.7, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-17
    Default locale: en_US, platform encoding: Cp1252
    OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"                     


    Windows Prerequisites

    Maven is written in Java (and primarily used to build Java programs). Thus, the major prerequisite is the Java SDK. You need to install the Java SDK (e.g. from Oracle's download site).

    Once Java is installed, you must ensure that the commands from the Java SDK are in your PATH environment variable. Running, for example,

    java -version

    must show the right version number.

    You run Maven by invoking a command-line tool: mvn.cmd from the bin directory of the Maven. To do this conveniently, ${maven.home}\bin must be in your PATH, just like the Java SDK commands. You can add directories to your PATH in the control panel; the details vary by Windows version.


    Creating a Project

    You need somewhere for your project to reside. Create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:

    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

    The mvn archetype:generate command is used with Apache Maven to generate a new project based on a specific project template, known as an archetype. In the provided command:

    • DgroupId: This specifies the group ID for the project. The group ID is often used to identify the organization or group that the project belongs to.
    • DartifactId: This specifies the artifact ID for the project. The artifact ID is the name of the project.  
    • DarchetypeArtifactId: This specifies the artifact ID of the archetype to be used for generating the project. An archetype is a project template or a blueprint.
    • DarchetypeVersion: This specifies the version of the archetype to be used.

    Under this directory(my-app) you will notice the following standard project structure.

    my-app
    |-- pom.xml
    `-- src
        |-- main
        |   `-- java
        |       `-- com
        |           `-- mycompany
        |               `-- app
        |                   `-- App.java
        `-- test
            `-- java
                `-- com
                    `-- mycompany
                        `-- app
                            `-- AppTest.java

    The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml file is the project's Project Object Model, or POM.

    The POM

    The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:

    <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>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>my-app</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    <build>
    ...
    </build>
    </project>

    Build the Project

    mvn package

    The command line will print out various actions, and end with the following:

     ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.953 s
    [INFO] Finished at: 2023-12-11T20:05:10+01:00
    [INFO] ------------------------------------------------------------------------

    A Build Lifecycle is Made Up of Phases

    Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle.

    For example, the default lifecycle comprises of the following phases (for a complete list of the lifecycle phases, refer to the Lifecycle Reference):

    • validate - validate the project is correct and all necessary information is available
    • compile - compile the source code of the project
    • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
    • package - take the compiled code and package it in its distributable format, such as a JAR.
    • verify - run any checks on results of integration tests to ensure quality criteria are met
    • install - install the package into the local repository, for use as a dependency in other projects locally
    • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.


    Java 9 or later

    By default your version of Maven might use an old version of the maven-compiler-plugin that is not compatible with Java 9 or later versions. To target Java 9 or later, you should at least use version 3.6.0 of the maven-compiler-plugin and set the maven.compiler.release property to the Java release you are targetting (e.g. 9, 10, 11, 12, etc.).

    In the following example, we have configured our Maven project to use version 3.8.1 of maven-compiler-plugin and target Java 17:

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>17</maven.compiler.release>
    </properties>
    <build>
    <pluginManagement>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    </plugin>
    </plugins>
    </pluginManagement>
    </build>





    Comments

    Popular posts from this blog

    RabbitMQ Clusters with Spring Boot and Docker

    Physicists who built ultrafast ‘attosecond’ lasers win Nobel Prize