Building with Apache Maven


Difficulty

Maven is an Open Source project made by Apache and today we will build with it.
It is a very powerful tool in terms of code compilation and development team collaboration.
The heart of a Maven project is the pom.xml configuration file.

One of the most important features of Maven is the automatic download of the libraries needed by a Java project, solving the dependencies.
“Dependency” means a class that is instantiated by a “new” keyword. So in Maven dependencies mean libraries “jars” that are necessary for the Java project to request services.
The POM (Project Object Model) describes the structure of a J2EE project, it is connected to a Remote Repository where in the project start-up phase it downloads all the dependencies listed in this file.
A local repository is created on this directory in the Windows environment:

C:\users\{UserName}\.m2\

So when the pom.xml requires a new dependency it is deposited in that folder. This path can also be configured and/or shared online with your development team.
Some key elements that can be used in the Maven configuration on a J2EE project are: plugins and goals.
The goals are functions that can be executed at the project level which, as the term itself suggests, identifies the objectives.

The most common goal is to build our project.
You’ll find it in the target/ directory of the project:

mvn package

An example pom.xml follows with comments:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!-- our project base informations -->
  <groupId>org.springframework</groupId>
  <artifactId>my-project</artifactId>
  <version>0.0.1</version>

  <!-- our parent pom configuration (in this case we're using Spring Boot) -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
  </parent>

  <!-- jar to import in our project with utils and libraries -->
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- other dependencies follow... -->
  </dependencies>
  

  <!-- our project start class -->
  <profiles>
    <profile>
      <id>helloApplication</id>
      <properties>
     <spring.boot.mainclass>hello.HelloWorldApplication</spring.boot.mainclass>
      </properties>
    </profile>
  </profiles>

  <!-- you can add properties like settings for jdk version or maven version -->
  <properties>
    <java.version>1.8</java.version>
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
  </properties>

  <!-- configure your base class when building -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>  
          <mainClass>com.obyte.MyApplication</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <!-- from which repository we can import dependencies -->
  <repositories>
    <repository> <!-- For Device -->
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/libs-milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
</project>

To install Maven you can follow the ufficial link:

https://maven.apache.org/install.html

0
Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.