DevOps Classroom Series – 12/Feb/2021

Maven contd..

  • POM stands for Project Object model and is fundamental file in Maven. This is an xml file that will be present in the base directory of the project as pom.xml

  • This file contains information about project and various configurations used by maven to build the project

  • In the maven file the basic components are

    • project root : This is projects root taag
    • modelVersion: This is maven schema version number and is always 4.0.0
    • groupId: Name of your organization or departmetn
    • artifactId: project id
    • version: version of your project. In version if you specify SNAPSHOT it refers as if the version is still under development. To specify release the convention is to remove SNAPSHOT from version.
  • Super POM:

    • This mavens default POM. All POMs inherit from a parent or default POM
    • Navigate to be basedir of the project and execute “`mvn help:effective-pom
      <?xml version="1.0" encoding="Cp1252"?>
      <!-- ====================================================================== -->
      <!--                                                                        -->
      <!-- Generated by Maven Help Plugin on 2021-02-12T08:07:30+05:30            -->
      <!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
      <!--                                                                        -->
      <!-- ====================================================================== -->
      <!-- ====================================================================== -->
      <!--                                                                        -->
      <!-- Effective POM for project                                              -->
      <!-- 'com.qualitythought.devops:hello-maven:jar:1.0-SNAPSHOT'               -->
      <!--                                                                        -->
      <!-- ====================================================================== -->
      <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>
      <groupId>com.qualitythought.devops</groupId>
      <artifactId>hello-maven</artifactId>
      <version>1.0-SNAPSHOT</version>
      <repositories>
          <repository>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>Central Repository</name>
          <url>https://repo.maven.apache.org/maven2</url>
          </repository>
      </repositories>
      <pluginRepositories>
          <pluginRepository>
          <releases>
              <updatePolicy>never</updatePolicy>
          </releases>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>Central Repository</name>
          <url>https://repo.maven.apache.org/maven2</url>
          </pluginRepository>
      </pluginRepositories>
      <build>
          <sourceDirectory>D:\temp\codeforbuilding\hello-maven\src\main\java</sourceDirectory>
          <scriptSourceDirectory>D:\temp\codeforbuilding\hello-maven\src\main\scripts</scriptSourceDirectory>
          <testSourceDirectory>D:\temp\codeforbuilding\hello-maven\src\test\java</testSourceDirectory>
          <outputDirectory>D:\temp\codeforbuilding\hello-maven\target\classes</outputDirectory>
          <testOutputDirectory>D:\temp\codeforbuilding\hello-maven\target\test-classes</testOutputDirectory>
          <resources>
          <resource>
              <directory>D:\temp\codeforbuilding\hello-maven\src\main\resources</directory>
          </resource>
          </resources>
          <testResources>
          <testResource>
              <directory>D:\temp\codeforbuilding\hello-maven\src\test\resources</directory>
          </testResource>
          </testResources>
          <directory>D:\temp\codeforbuilding\hello-maven\target</directory>
          <finalName>hello-maven-1.0-SNAPSHOT</finalName>
          <pluginManagement>
          <plugins>
              <plugin>
              <artifactId>maven-antrun-plugin</artifactId>
              <version>1.3</version>
              </plugin>
              <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.2-beta-5</version>
              </plugin>
              <plugin>
              <artifactId>maven-dependency-plugin</artifactId>
              <version>2.8</version>
              </plugin>
              <plugin>
              <artifactId>maven-release-plugin</artifactId>
              <version>2.5.3</version>
              </plugin>
          </plugins>
          </pluginManagement>
          <plugins>
          <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>2.5</version>
              <executions>
              <execution>
                  <id>default-clean</id>
                  <phase>clean</phase>
                  <goals>
                  <goal>clean</goal>
                  </goals>
              </execution>
              </executions>
          </plugin>
          <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>2.6</version>
              <executions>
              <execution>
                  <id>default-testResources</id>
                  <phase>process-test-resources</phase>
                  <goals>
                  <goal>testResources</goal>
                  </goals>
              </execution>
              <execution>
                  <id>default-resources</id>
                  <phase>process-resources</phase>
                  <goals>
                  <goal>resources</goal>
                  </goals>
              </execution>
              </executions>
          </plugin>
          <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>2.4</version>
              <executions>
              <execution>
                  <id>default-jar</id>
                  <phase>package</phase>
                  <goals>
                  <goal>jar</goal>
                  </goals>
              </execution>
              </executions>
          </plugin>
          <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <executions>
              <execution>
                  <id>default-compile</id>
                  <phase>compile</phase>
                  <goals>
                  <goal>compile</goal>
                  </goals>
              </execution>
              <execution>
                  <id>default-testCompile</id>
                  <phase>test-compile</phase>
                  <goals>
                  <goal>testCompile</goal>
                  </goals>
              </execution>
              </executions>
          </plugin>
          <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.12.4</version>
              <executions>
              <execution>
                  <id>default-test</id>
                  <phase>test</phase>
                  <goals>
                  <goal>test</goal>
                  </goals>
              </execution>
              </executions>
          </plugin>
          <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.4</version>
              <executions>
              <execution>
                  <id>default-install</id>
                  <phase>install</phase>
                  <goals>
                  <goal>install</goal>
                  </goals>
              </execution>
              </executions>
          </plugin>
          <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.7</version>
              <executions>
              <execution>
                  <id>default-deploy</id>
                  <phase>deploy</phase>
                  <goals>
                  <goal>deploy</goal>
                  </goals>
              </execution>
              </executions>
          </plugin>
          <plugin>
              <artifactId>maven-site-plugin</artifactId>
              <version>3.3</version>
              <executions>
              <execution>
                  <id>default-site</id>
                  <phase>site</phase>
                  <goals>
                  <goal>site</goal>
                  </goals>
                  <configuration>
                  <outputDirectory>D:\temp\codeforbuilding\hello-maven\target\site</outputDirectory>
                  <reportPlugins>
                      <reportPlugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-project-info-reports-plugin</artifactId>
                      </reportPlugin>
                  </reportPlugins>
                  </configuration>
              </execution>
              <execution>
                  <id>default-deploy</id>
                  <phase>site-deploy</phase>
                  <goals>
                  <goal>deploy</goal>
                  </goals>
                  <configuration>
                  <outputDirectory>D:\temp\codeforbuilding\hello-maven\target\site</outputDirectory>
                  <reportPlugins>
                      <reportPlugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-project-info-reports-plugin</artifactId>
                      </reportPlugin>
                  </reportPlugins>
                  </configuration>
              </execution>
              </executions>
              <configuration>
              <outputDirectory>D:\temp\codeforbuilding\hello-maven\target\site</outputDirectory>
              <reportPlugins>
                  <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                  </reportPlugin>
              </reportPlugins>
              </configuration>
          </plugin>
          </plugins>
      </build>
      <reporting>
          <outputDirectory>D:\temp\codeforbuilding\hello-maven\target\site</outputDirectory>
      </reporting>
      </project>
    
  • Build Phases in Maven:

    • prepare-resources
    • validate
    • compile
    • test
    • package
    • install
    • deploy
    • clean
    • default
    • site
  • These build phases can be specified to maven as goals

  • In maven we have two popular packaging formats

    • jar: java archive
    • war: web archive
  • Build Profile: These are set of configuration values which can be used to set or override default values of maven build. There are three types of build profiles

    • Per Project: These are defined in basedir/pom.xml
    • Per User: These are defined in maven settings (%HOME-DIR%/.m2/settings.xml)
    • Global: Defined in the Maven global settings file (%M2_HOME%/conf/settings.xml)

Maven Repository

  • Repository is a directory where all the projects jar, plugins or any other project artifacts can be stored and used by Maven only
  • There are three repository types
    • local repository
    • remote repository
    • central repository
  • Local Repository:
    • This is the folder location on the machine where maven is installed
    • Default location of local repository is (%HOME-DIR%/.m2/)
  • Central Repository:
    • This is repository maintained by Maven community and it contains large number of common librarues
    • When maven does not find dependencies in the local repository it will start searching in the central repository
  • Remote Repository: Maven provides a concept of remote repository which is organization own custom repository containing libraries developed and other project jars. The remote repository location can be specifed in the projects pom.xml or in the settings.xml file

Dependency

  • When developers write code, they use the libararies for reusing the code.
  • In maven dependencies can be added in pom.xml file in dependencies section
<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.qualitythought.devops</groupId>
   <artifactId>hello-maven</artifactId>
   <version>1.0-SNAPSHOT</version>

   <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-cli</artifactId>
        <version>2.4.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>23.0</version>
    </dependency>

   </dependencies>
</project>
  • Lets try to build game of life project Refer Here
mvn compile
mvn test
mvn package
  • compile goal will compile the source code and generates .class files in the target folder
  • test goal will run the unit tests written in src/test folder
  • package goal will create the jar/war file as specified in pom.xml
  • install goal will copy the jar/war file and pom file to local repository (~/.m2)
  • deploy goal will copy the jar/war file into configured repository (central or remote)
  • clean goal will remove the generated target folders
  • If you want to do a full build to generate a package mvn clean package
  • Exercise:
    • Try to create java package by executing mvn clean package on the following

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin