Working with multi-module Maven project

Maven Module :
Maven module is a Maven Sub Project. So, to have a Maven module, first you need to have a Maven Parent Project.
In this parent project, you need to enable the packaging as โ€œpomโ€œ.

Once you create the parent project create Modules as follows,
[Right click on the parent project created] > New > Other > [Maven]
Maven Module


Archetype :
You can create the project as a simple one, by passing the archetype. Otherwise, select the relevant arche-type.
eg : maven-archetype-quickstart

Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made.
We use this to generate different types of projects such as war, jar, ear etc easily.


Aggregator POM / Parent POM :

A multi-module project is built from an aggregator POM that manages a group of submodules.
Here, a top-level module serving for joining multiple modules under one roof is called aggregator and aggegator does not include any information about dependencies. Aggregator has a small difference with parent pom. The source of to-be-inherited information about libraries and plugins is known as a parent POM. So, unlike aggregator, parent pom, includes all the properties, dependencyManagement and pluginManagement sections.

Normally, all configuration with dependencies are included to the parent pom and set it as the parent of the child modules, so theyโ€™ll inherit from it.

Submodules are regular Maven projects, and they can be built separately or through the aggregator POM.
It aggregates the projects as below

Parent / Aggregator POM :

<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>blog.code.knowledgehunting.corejava</groupId>
	<artifactId>ParentApp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<modules>
		<module>ChildApp1</module>
		<module>ChildApp2</module>
		<module>ChildApp3</module>
	</modules>

</project>

ChildApp1 โ€“ pom.xml

<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>
  <parent>
    <groupId>blog.code.knowledgehunting.corejava</groupId>
    <artifactId>ParentApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>ChildApp1</artifactId>
</project>

Benifits of multi module projects :

  • Reduce duplication.
  • Build multiple modules inlined to mavenโ€™s build life cycle. here, applicationโ€™s modules can be built in a single command.
  • Share a vast amount of configuration with other modules.

Mavenโ€™s build life cycle :

Mavenโ€™s build life cucle contains following like default pifecycle phases

  • 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.

Code : https://bitbucket.org/namalfernando/parentapp/src/master/


References :

  1. https://docs.jboss.org/tools/4.1.0.Final/en/maven_reference/html/creating_a_maven_application.html
  2. https://maven.apache.org/guides/introduction/introduction-to-archetypes.html
  3. http://rostislav-matl.blogspot.com/2011/12/maven-aggregator-vs-parent.html
  4. https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
  5. https://www.youtube.com/watch?v=28fsqKNjxrc

#eclipse, #maven

Speedup coding with eclipse editor templates

In eclipse IDE go toโ€ฆ

  1. Windows > Preferences
  2. Java > Editor > Templates
  3. Create a new template OR select the Existing one to edit
  4. Add this pattern in there
logger.info("${enclosing_method}().${word_selection} : " + ${word_selection}${});${cursor}

References :

  • http://poojapainter11.wordpress.com/2013/08/10/eclipse-shortcuts/
  • http://www.grobmeier.de/log4j-2-performance-close-to-insane-20072013.html
  • http://eannaburke.wordpress.com/2013/06/28/a-rudimentary-logger/

#eclipse, #ide, #utility

Find/Replace in eclipse using regex

We can find a regex pattern and do a replace as mentioned below. This is very helpful when we are doing bulk replacing.
eg : Comment all the lines containing a word (selectedWord)

Steps :

  1. Ctrl + F
  2. Find ^.selectedWord.$
  3. Replace //$0
  4. Options : Check Regular Expressions

#eclipse, #ide, #utility