Transcript
Page 1: S/W Design and Modularity using Maven

S/W Design and Modularityusing Maven 2

Tibor Digaňa

Page 2: S/W Design and Modularity using Maven

Goals

• Modularity• Maven• Demos

Page 3: S/W Design and Modularity using Maven

Agenda

• Necessary understanding of modularity helps you better understand the Maven features;

• Mandatory Maven theory with demos;• Solving Real Project Problem;• Extra Demos: Maven plugins, FTP Deployment,

SCM, Site, Javadoc, JIRA in Maven, Query Builder project and other, building WAR in JEE, Maven vs. corporative build process, TeamCity, Ant in Maven, classpath via dependency plugin, …

Page 4: S/W Design and Modularity using Maven

Modularity and the Maven?

• Without feeling for Modularity you are doing in Maven something that you do not know what it is;

• If you consider the Maven as a build tool, then you are completely wrong;

• Maven like other tool, e.g. Apache Ivy, helps you building a modular application.

Page 5: S/W Design and Modularity using Maven

What is Maven?

Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Page 6: S/W Design and Modularity using Maven

Maven is just a tool!

• Just like JDK and Java programming language, does not guarantee right design in your hands, using the same twice you can produce useful or bad design;

• Using Maven right way, whatever corporative rule;• The Maven solves information hiding in modular

application, and provides dependencies inheritance;• Maven solves compile-time dependencies in modular

application;• Maven has runtime scope in dependencies as well;• Maven controls build life cycle in specific packaging;• If you only want to compile pure sources, use the Ant.

Page 7: S/W Design and Modularity using Maven

Maven is just a tool!

• OOP has attributes for information hiding and the Maven has attributes of information hiding in modularity as well;

• Builds an artifact based on module POM;• Encapsulates POM with dependencies in e.g. JAR package;• Inheritance of dependencies;• Overrides dependencies;• Includes/Excludes dependencies;• Polymorphic behavior of artifact does not exist;• Only configuration using profiles;

Page 8: S/W Design and Modularity using Maven

Granularity and Modules

• Classes are “somehow” logically grouped in to packages (unit);

• Granularity is the measure of a convenient unit for organizing small and large applications

Page 9: S/W Design and Modularity using Maven

Design Patterns and Principles

• For a concrete problem definition, the design principle specifies the relationship between object reference types;

• Design Principles help you design units like modules, packages and classes in the package in favor of reusability, maintainability and robustness.

Page 10: S/W Design and Modularity using Maven

Granularity as a General Problem of DesignTwo bad concepts in packaging

• One package in entire application• Over-packagingBoth lead to a disorder (hell of the classes)

where packages have no context and domain is missing.

Hard to find rules in visibility of the classes.So, information hiding must apply to packages in

compile-time and run-time!

Page 11: S/W Design and Modularity using Maven

Design Principles go with UML

• The Open Closed Principle (OCP)• The Liskov Substitution Principle (LSP)• The Dependency Inversion Principle (DIP)• The Interface Segregation Principle (ISP)• The Reuse/Release Equivalence Principle (REP)• The Common Reuse Principle (CRP)• The Common Closure Principle (CCP)• The Acyclic Dependency Principle (ADP)

Reference: http://www.objectmentor.com/resources/articles/granularity.pdfhttp://www.objectmentor.com/resources/publishedArticles.html

Page 12: S/W Design and Modularity using Maven

Composite/Structured Design by Glendford J. Myers

Page 13: S/W Design and Modularity using Maven

DemoUsing Maven in Real Application

Page 14: S/W Design and Modularity using Maven

Fine grained Modularity

• Each domain package is a module• Domain package is atomic –indivisible context• Pros/cons• Continuous development on modules which

are not able to compile

Page 15: S/W Design and Modularity using Maven

Dependencies Matrix of a Real Application without Maven

Page 16: S/W Design and Modularity using Maven

Can we apply fine grained modularity in this s/w design ?

• No, we cannot apply it without redesigning dependencies

• Reason: For instancethe interface parser.ISheetParser depends on data.SheetDocument and vice-versa.

• Current s/w design has cyclic dependencies in packages.

Page 17: S/W Design and Modularity using Maven

Data depends on Parser Package

Page 18: S/W Design and Modularity using Maven

Parser Depends on Data Package

Page 19: S/W Design and Modularity using Maven

How to prevent from Cyclic Dependencies?

• Acyclic Dependency Principle (ADP)• Add an extra module/package with

an abstract Class or an Interface

Page 20: S/W Design and Modularity using Maven

Let’s prevent from cyclic dependencies in continuous development

• Make custom dependency rules unbreakable in package hierarchy

Page 21: S/W Design and Modularity using Maven

Let’s prevent from cyclic dependencies in continuous development

Page 22: S/W Design and Modularity using Maven

Let’s prevent from cyclic dependencies in continuous development

Unfortunately you cannot successfully export Dependency Violation settings to “Project_Default.xml” either to settings.jar via File|Export Settings.

Thus you still need IDEA project files in your CVS.

Page 23: S/W Design and Modularity using Maven

Let’s prevent from cyclic dependencies in continuous development

Page 24: S/W Design and Modularity using Maven

In the Maven this is more sophisticated

• The key is relationship between modules• Maven is a build tool• Maven controls the build life cycle• Maven can be used in IDE without IDE’s

project files (including some global settings.jar)

Page 25: S/W Design and Modularity using Maven

Applying Maven as a Tool

• A bad design of POM.XML when modularity is not properly considered;

• POM.XML reflecting a good modularity concept

Page 26: S/W Design and Modularity using Maven

Minimum information for modular app

• pom.xml• mvn idea:idea

(mvn org.apache.maven.plugins:maven-idea-plugin:idea)

Page 27: S/W Design and Modularity using Maven

Demo

Let’s create the IDEA project files from pom.xml

Page 28: S/W Design and Modularity using Maven

Minimum information for modular app

Using Maven in IDEA we Do NOT need IDEA project files in CVS.

Mainly you need Maven command line, pom.xml and settings.jar

Page 29: S/W Design and Modularity using Maven

Development using IDEA and Maven

Two approaches to get IDEA/Maven project1. Having pom.xml create IDEA project files

mvn idea:idea2. Create pom.xml altogether with project files

directly in IDEACreate New Project|Create project from scratch|Maven Module|…

Page 30: S/W Design and Modularity using Maven

DemoNow follow the second approach in IDEA.

Page 31: S/W Design and Modularity using Maven

Demo with complete project

• Setting Maven Home directory in IDEA• Maven settings.xml in IDEA• Specifying local repository in IDEA

Page 32: S/W Design and Modularity using Maven

Introduction to the Standard Directory Layout

Page 33: S/W Design and Modularity using Maven

As a convention we use these filesin ${basedir}

• LICENSE.TXT• NOTICE.TXT• README.TXT

Page 34: S/W Design and Modularity using Maven

Maven Project Settings in IDEA

Page 35: S/W Design and Modularity using Maven

Demo Started Build

• Start the build in profile “development”• Start the build in profile “release”

Page 36: S/W Design and Modularity using Maven

Maven 2 Installation Package

• ${maven.home}/bin/*.*runtime and debugging scripts

• ${maven.home}/boot/*.jarclassloading framework

• ${maven.home}/conf/settings.xml• ${maven.home}/lib/*.jar

Maven 2 core library for core classloader

Page 37: S/W Design and Modularity using Maven

Maven 2 Core Classloader

• The Apache Codehaus Classworlds JAR is used to boot the classloader graph.

• The core classloader uses libraries in ${maven.home}/lib and extensions.

• These libraries in Maven 2 are visible to current and subsequent projects.

• Changes in Maven 3.1 + OSGiReferences

http://maven.apache.org/guides/mini/guide-maven-classloading.html

http://www.parleys.com/#st=5&id=1630

Page 38: S/W Design and Modularity using Maven

Maven 2 Plugin Classloader

• Has plugin’s dependencies list as specified by plugins/plugin section in POM XML;

• Does not use project’s classloader, but may require using custom class loader of projects compile, runtime, and test class path.

References

http://maven.apache.org/guides/mini/guide-maven-classloading.html

Page 39: S/W Design and Modularity using Maven

Building Phases in Maven Core

• *uber.jar!/**/*.*/components.xmlcontains default phases per packaging type

Reference

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Page 40: S/W Design and Modularity using Maven

DemoSuper POM

• In Maven 2 installation dir (by default: $(user.home)/.m2/) with model version 4.0.0

• lib/maven-2.2.1-uber.jar/org/apache/maven/project/pom-4.0.0.xml• Each POM and parent POM extends Super POM• Super POM defines central-release-plugin/repository

http://repo1.maven.org/maven2• Standard Maven Directory Layout and properties

${directory} == ${pom.basedir}/target outputDirectory == classes finalName == ${project.artifactId}-${project.version} testOutputDirectory == test-classes, etc.

• pluginManagement for phases of default build life cycle includingclean, and site phases and their plugin versions

Page 41: S/W Design and Modularity using Maven

What did we create?

<groupId>com.idsscheer</groupId><artifactId>spreadsheet</artifactId>

<version>2.0.1</version>

Page 42: S/W Design and Modularity using Maven

Identifying Artifact

• Min required fields is groupId:artifactId:versionso-called standard coordinate. The groupId and artifactId might be inherited from parent POM;

• The Maven sometimes print out coordinatesgroupId:artifactId:packaging:versiongroupId:artifactId:packaging:classifier:version

• More generally ‘fileName’ of deploy library${artifactId}-${version}-${classifier}.${packaging}e.g. spreadsheet-2.0.0-jdk1.5.jarUnless fileName is overridden in build section.

• Dependency type can be jar, war, etc., or combined with an extension, e.g. test-jar.

Page 43: S/W Design and Modularity using Maven

Demo

• Newly created structured using Maven• Compare with old project structure of

directory layout• Clarity and minimum info in new structure

Page 44: S/W Design and Modularity using Maven

Offline Command Line Options

• -o #maven build in offline mode• -npu #no plugin updates• -npl #no plugin latest

Page 45: S/W Design and Modularity using Maven

Maven Project Model

Explaining XML tags in POM

Reference:

http://maven.apache.org/ref/2.2.1/maven-model/maven.html

Page 46: S/W Design and Modularity using Maven

settings.xmlhttp://maven.apache.org/settings.html

Page 47: S/W Design and Modularity using Maven

Modularity Pros• Asynchronous project flow• A project module in IDE without the rest• Easier maintenance• Minimizes coupling• Using interfaces/mock object testable design• Reusability• Error prone• Conceptual diagram

References:

http://www.objectmentor.com/resources/articles/granularity.pdf

http://www.infoq.com/articles/modular-java-what-is-it

http://java.dzone.com/articles/java-modularity-2-why

Page 48: S/W Design and Modularity using Maven

Modularity only If no pure Reusability?

• Still makes sense to have modular app if pure reusability (one usage of a module).

Page 49: S/W Design and Modularity using Maven

Modularity sustains a continuousProgress of Development

• From Spaghetti code to modular app

Page 50: S/W Design and Modularity using Maven

What is NOT Artifact Reusability

• Textually inserted bunch of program to another program; (junit, junit-dep);

• Re-assembly library into another library;

(Reusability is the most often claimed goal of OOD.)

Reference:

http://www.objectmentor.com/resources/articles/granularity.pdf

Page 51: S/W Design and Modularity using Maven

What is Artifact Reusability

• My project links with another dependency.• Whenever a new dependency artifact is fixed

or enhanced, I receive new version.

Reference:

http://www.objectmentor.com/resources/articles/granularity.pdf

Page 52: S/W Design and Modularity using Maven

Modularity Cons

• Hell of classes if wrong separation concept. Means mismatched domain package/module.

• Hell of transitive dependencies.• Runtime scope different from compile scope.• People assigned to a module development

may not necessarily have a global picture of view, which on the other hand improved work concentrations in current project of module.

Page 53: S/W Design and Modularity using Maven

Artifact Versioning

• <major>.<minor>.<incremental>-<qualifier>• 1.3-beta-01• 1.3.5• 1.3.5-SNAPSHOT• 1.3.5-1234567890

References:

http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-pom-syntax.html

http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-project-dependencies.html#pom-relationships-sect-version-ranges

Page 54: S/W Design and Modularity using Maven

Notice

Separating build number/qualifier is broken.Versions are naturally ordered as strings.Thus alpha-2 is greater than alpha-10.Therefore prefix with preceding zeros: alpha-02

Page 55: S/W Design and Modularity using Maven

Notice

If specifying dependency or plugin version like:<version>1.2.3</version>This is Maven’s freedom to prefer version 1.2.3or take other in conflicts.

Explicit version must be encapsulated:<version>[1.2.3]</version>

Reference (conflicts resolution):

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Page 56: S/W Design and Modularity using Maven

Artifact Versions in Range

• [4.0,)• (,2.0)• [2.0]• [3.8,4.0)

Page 57: S/W Design and Modularity using Maven

Two contradictory goals

• Need for an overall of “big picture” in multi module projects.

• The goal of high module independence is to know as little as possible of system reminders

Page 58: S/W Design and Modularity using Maven

Conclusion on what has changed in p4://Modules/libs/spreadsheet/Develop/2.0.x

• Added LICENSE.TXT; NOTICE.TXT; README.TXT; src/main/config/settings.xml

• Added directories src/main/java, resources; src/test/java;• Modified spreadsheet.iml; *.ipr; *.iws;• Moved ./build/gen/**/*.java to src/main/java/**/parser/…• Moved ./source/*.java (except for applications) to

src/main/java/…• Moved applications from ./source/*.java to src/test/java/…• Moved JUnit tests from ./testsource/*.java to src/test/java/…• Moved one resource ./build/gen/**/SheetParser.tokens

to src/main/resources/…

Page 59: S/W Design and Modularity using Maven

What to do when tests fail

See the logs and localize the detailed error desc.• running ASheetOperationTestCase

Tests run: 5, Failures: 1, Errors: 4, Skipped: 0, Time elapsed: 0.07 sec <<< FAILURE!

• Localize the report in ./target/surefire-reports• ASheetOperationTestCase.txt or xml• And analyse the Java stack trace

Page 60: S/W Design and Modularity using Maven

DemoWhat to do when tests fail

• ASheetOperationTestCase.txt• ASheetOperationTestCase.xml

Page 61: S/W Design and Modularity using Maven

How do I add resources to my JAR?

Intended storage (or override by property in build)• ${basedir}/src/main/resources• ${basedir}/src/test/resources

A good practice is to create and use META-INF.Regarding the Java Services create in basedir:

src/main/resources/META-INF/service/<interface>

Page 62: S/W Design and Modularity using Maven

Standard Maven Directory Layout

Page 63: S/W Design and Modularity using Maven

Notice

JAR packaging plugin creates• Directory META-INF• File META-INF/MANIFEST.MFAnd additionally

target/classes/META-INF/${pom.groupId}/${pom.artifactId}/

• pom.properties• pom.xml

Page 64: S/W Design and Modularity using Maven

Retrieve Resource in Source Code

InputStream is = getClass().getResourceAsStream("/META-INF/application.properties" )

Page 65: S/W Design and Modularity using Maven

How do I filter resource files?

As in the previous slides the resource file is “application.properties”.

Put a reference to the property that will contain a filtered value using the syntax

${< property name >}

Property names must be unique in a profile.

Page 66: S/W Design and Modularity using Maven

Filtering Resources

Resource content examples: # application.properties application.name=$

{project.name} application.version=${project.version}

jdk.path=${env.JAVA_HOME}

application.root.dir=${user.dir}

message=${my.filter.value} command=${command.line.prop}

isOffline=${settings.offline}

Page 67: S/W Design and Modularity using Maven

Notice

• Semantics ${project.xyz} == ${pom.xyz} == ${xyz}• Semantics ${pom.xyz} and ${xyz} are Deprecated

Page 68: S/W Design and Modularity using Maven

Filtering Resources

Three properties references must be obtained:• my.filter.value• command.line.prop• settings.offline

Page 69: S/W Design and Modularity using Maven

Filtering Resources by a Filter File<project> <build> <filters> <filter>src/main/filters/filter.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build></project>

Page 70: S/W Design and Modularity using Maven

Filtering Resources by a Filter File

• An example of filter file content:# filter.propertiesmy.filter.value=hello!

What about language specific properties.Use a custom profile for specific language build.

Page 71: S/W Design and Modularity using Maven

Filtering Resources

How to filter resource property command.line.prop ?POM.XML:<project>

<properties>

<command.line.prop>hello</command.line.prop> </properties>

</project>

Or use system property handled in phase process-resources

mvn process-resources "-Dcommand.line.prop=hello again"

Page 72: S/W Design and Modularity using Maven

Notice

• System property has higher priority than a property with same name defined in POM;

• Properties must be unique in certain profile.

Page 73: S/W Design and Modularity using Maven

Filtering Resources

How to filter resource property settings.offline ?SETTINGS.XML:<settings> <offline>true</offline></settings>

Page 74: S/W Design and Modularity using Maven

Filtering Resources

Resources are processed in phaseprocess-resources

Example: mvn process-resouces

Page 75: S/W Design and Modularity using Maven

After Installing the Release Versions

custom the command (-Prelease is profile name)mvn -s src/main/config/settings.xml clean install –P release

1. First we installed version 2.0.22. And then 2.0.1

LibRepository/com/idsscheer/spreadsheet/

Page 76: S/W Design and Modularity using Maven

Artifact Installation the Release Versions

Page 77: S/W Design and Modularity using Maven

Artifact Installation the Release Versions

Page 78: S/W Design and Modularity using Maven

Artifact Installation the Snapshot Version

Installation 2.0.2-SNAPSHOTmvn -s src/main/config/settings.xml clean install


Recommended