Wednesday, May 31, 2017

Maven - How to setup the maven pom files to support builds and deployments.


Parent POM File


 

A parent pom file should be created for each application and can be used to define standard settings for all of the components that reference it.  If needed a parent pom file can be created for each type of components libraries versus MuleSoft APIs.

 

The parent pom needs to include a groupId, artifactId and version to keep it unique.  For example a parent pom for A04 would be:

 

<groupId>com.alok.a04</groupId>

<artifactId>apiparent</artifactId>

<version>0.1.0</version>

 

Where a parent pom file for KVO could be defined as:

 

<groupId>com.alok.kvo</groupId>

<artifactId>parent</artifactId>

<version>0.1.0</version>

 

As example parent pom file is available at buttom.

 

The parent pom for APIs should have the following settings in it.

 


The properties section should contain version numbers and other properties that should be shared across all APIs.  The API pom files should use these properties and not redefine them whenever possible.

 

A property defined as <jdk.version>1.8</jdk.version> can be used in other parts of the pom file as ${jdk.version}



The repositories section can be used as a single place to define all of the artifact repositories that need to be accessed for library dependencies.  By defining the repositories here you do not have to maintain them in each API.



The pluginRepository section can be used to define where to get all plugins in a single place.  Like the repositoiry section this allow you to manage this in one location which can be used in all pom files that reference the parent.

Build


The build section

Distribution Management


The distributionManagement section defines the repository information for Jenkins to be able to update Artifactory with the binary after a successful build.

 


 

API POM File


Parts of this section were taken from git-scm.com and modified to fit the general organization and tone of the rest of the document. See the original text (and more!) at http://git-scm.com

 

Parent


The parent pom should be defined in the api pom to include all of the default definitions.  A Parent pom definition to use the A04 API Parent would be like this:

    <!-- Parent POM for common definitions -->

    <parent>

        <artifactId>apiparent</artifactId>

        <groupId>com.alok.A04</groupId>

        <version>0.0.2</version>

    </parent>

 

Identification


Each pom file need to identify itself with a unique groupId and artifactId.  The groupId should be formatted as com.alok.<APPL_ID>.<Type>  For example:

An A04 Api:                com.alok.a04.api

An A04 Library:          com.alok.a04.lib

An KVO Api:                com.alok.kvo.api

An KVO Library:          com.alok.kvo.lib

 

Using the correct Group and Artifact IDs is important as this is what will be used for how the binary is shown in Artifactory and to help identify which teams to contact if there are issues with the build, deployment or run-time execution of the code.

Version


As part of the Identification of the pom, a version needs to be defined.   Semantic Version 2.0, http://semver.org/ should be followed for the version number.

 

MAJOR.MINOR.PATCH

 

Initial development can use 0.1.0 and when you are ready to deploy the first version it can be changed to 1.0.0.

 

All changes to the api that are backward compatible should updated the MINOR version number 1.1.0, 1.2.0, 1.3.0, etc.  When a non backwards compatible change is made the MAJOR version number should be updated 1.0.0, 2.0.0, 3.0.0, etc.

 

Repositories


The API pom just needs to define the repository that the parent is located in.  By doing this you will have access to all of the repositories that are defined in the parent without have to manage them individually

 
Library POM Files

 
Every commit pushed to Origin must satisfy three criteria: it must be atomic, digestible, and it must include an adequate comment. 


======================================example======================
<?xml version="1.0" encoding="UTF-8"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alok.a04</groupId>
<artifactId>apiparent</artifactId>
<version>0.0.2</version>
<packaging>pom</packaging>
<name>API Parent POM File</name>
<!-- Artifactory Deployment Setup -->


-<distributionManagement>

-<repository>



<id>deploymentRepo</id>
<name>deploymentRepo</name>
<url>${artifactory.url}/artifactory/AlokDigitalApi</url>
</repository>
</distributionManagement>
<!-- Global properties and versions -->


-<properties>
<!-- DevOps Environment Settings -->

<artifactory.url>http://URL.com:8081</artifactory.url>
<jdk.version>1.8</jdk.version>
<lib-error.version>1.0.0</lib-error.version>
<mule.maven.version>2.1.1</mule.maven.version>
<mule.munit.support.version>3.7.3</mule.munit.support.version>
<mule.tools.version>1.1</mule.tools.version>
<mule.version>3.7.3</mule.version>
<munit.version>1.1.1</munit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- Maven repositories -->


-<repositories>

-<repository>

-<releases>
<enabled>true</enabled>
</releases>

-<snapshots>
<enabled>true</enabled>
</snapshots>
<id>mulesoft-release-local</id>
<name>mulesoft-release-local</name>
<url>${artifactory.url}/artifactory/mule-release-local</url>
</repository>

-<repository>

-<releases>
<enabled>true</enabled>
</releases>

-<snapshots>
<enabled>true</enabled>
</snapshots>
<id>mulesoft-releases</id>
<name>mulesoft-releases</name>
<url>${artifactory.url}/artifactory/MuleReleases</url>
</repository>

-<repository>

-<releases>
<enabled>true</enabled>
</releases>

-<snapshots>
<enabled>true</enabled>
</snapshots>
<id>mulesoft-releases-ee</id>
<name>mulesoft-releases-ee</name>
<url>${artifactory.url}/artifactory/MuleReleases-ee</url>
</repository>

-<repository>
<id>MavenCentral</id>
<name>MavenCentral</name>
<url>http://repo1.maven.org/maven2/</url>
<layout>default</layout>
</repository>
</repositories>

-<pluginRepositories>

-<pluginRepository>
<id>MavenCentral-plugin</id>
<name>MavenCentral-plugin</name>
<url>http://repo1.maven.org/maven2/</url>
</pluginRepository>

-<pluginRepository>

-<snapshots>
<enabled>false</enabled>
</snapshots>
<id>mulesoft-release</id>
<name>mulesoft release repository</name>
<url>http://repository.mulesoft.org/releases/</url>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>

-<build>

-<pluginManagement>

-<plugins>

-<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>clirr-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->


-<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>

-<configuration>

-<lifecycleMappingMetadata>

-<pluginExecutions>

-<pluginExecution>

-<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.8,)</versionRange>

-<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>

-<action>
<execute/>
</action>
</pluginExecution>

-<pluginExecution>

-<pluginExecutionFilter>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-app-maven-plugin</artifactId>
<versionRange>[1.1,)</versionRange>

-<goals>
<goal>attach-test-resources</goal>
<goal>filter-resources</goal>
</goals>
</pluginExecutionFilter>

-<action>
<execute/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

-<plugins>

-<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.2</version>
</plugin>

-<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>

-<configuration>
<downloadJavadocs>true</downloadJavadocs>
<downloadSources>true</downloadSources>
</configuration>
</plugin>

-<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4</version>

-<executions>

-<execution>
<id>enforce-execution-environment</id>

-<goals>
<goal>enforce</goal>
</goals>

-<configuration>

-<rules>

-<requireMavenVersion>
<version>[2.2.1,)</version>
</requireMavenVersion>

-<requireJavaVersion>
<version>[1.6,)</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>

-<plugin>
<groupId>com.google.code.sortpom</groupId>
<artifactId>maven-sortpom-plugin</artifactId>
<version>2.3.1</version>

-<executions>

-<execution>
<phase>verify</phase>

-<goals>
<goal>sort</goal>
</goals>
</execution>
</executions>

-<configuration>
<createBackupFile>false</createBackupFile>
<sortProperties>true</sortProperties>
<sortDependencies>scope,groupId,artifactId</sortDependencies>
<nrOfIndentSpace>4</nrOfIndentSpace>
</configuration>
</plugin>

-<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>

-<configuration>
<showDeprecation>true</showDeprecation>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>

-<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>

-<executions>

-<execution>

-<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

-<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>

-<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>

-<executions>

-<execution>
<id>jacoco-initialize</id>

-<goals>
<goal>prepare-agent</goal>
</goals>
</execution>

-<execution>
<id>jacoco-site</id>
<phase>package</phase>

-<goals>
<goal>report</goal>
</goals>
</execution>
</executions>

-<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
</plugin>

-<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
</project>

No comments:

Post a Comment