of 78 /78
S/W Design and Modularity using Maven 2 Tibor Digaňa

S/W Design and Modularity using Maven

Embed Size (px)


The presentation is related to the firm where I rebuilt an existing non-maven project to the maven-based project with the best willingness of a proper modularity designs. The Demos in this presentation are related to source code which is not attached.

Text of S/W Design and Modularity using Maven

  • 1.S/W Design and Modularity using Maven 2Tibor Digaa

2. Goals Modularity Maven Demos 3. Agenda Necessary understanding of modularity helps youbetter understand the Maven features; Mandatory Maven theory with demos; Solving Real Project Problem; Extra Demos: Maven plugins, FTPDeployment, SCM, Site, Javadoc, JIRA inMaven, Query Builder project and other, buildingWAR in JEE, Maven vs. corporative buildprocess, TeamCity, Ant in Maven, classpath viadependency plugin, 4. Modularity and the Maven? Without feeling for Modularity you are doingin Maven something that you do not knowwhat it is; If you consider the Maven as a build tool, thenyou are completely wrong; Maven like other tool, e.g. Apache Ivy, helpsyou building a modular application. 5. What is Maven?Maven is a software project management andcomprehension tool. Based on the concept ofa Project Object Model (POM), Maven canmanage a projects build, reporting anddocumentation from a central piece ofinformation. 6. Maven is just a tool! Just like JDK and Java programming language, does notguarantee right design in your hands, using the sametwice you can produce useful or bad design; Using Maven right way, whatever corporative rule; The Maven solves information hiding in modularapplication, and provides dependencies inheritance; Maven solves compile-time dependencies in modularapplication; 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. 7. Maven is just a tool! OOP has attributes for information hiding and theMaven has attributes of information hiding inmodularity as well; Builds an artifact based on module POM; Encapsulates POM with dependencies in e.g. JARpackage; Inheritance of dependencies; Overrides dependencies; Includes/Excludes dependencies; Polymorphic behavior of artifact does not exist; Only configuration using profiles; 8. Granularity and Modules Classes are somehow logically grouped in topackages (unit); Granularity is the measure of a convenientunit for organizing small and large applications 9. Design Patterns and Principles For a concrete problem definition, the designprinciple specifies the relationship betweenobject reference types; Design Principles help you design units likemodules, packages and classes in the packagein favor of reusability, maintainability androbustness. 10. Granularity as a General Problem of Design Two 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 domainis missing.Hard to find rules in visibility of the classes.So, information hiding must apply to packages incompile-time and run-time! 11. 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 12. Composite/Structured Design by Glendford J. Myers 13. DemoUsing Maven in Real Application 14. Fine grained Modularity Each domain package is a module Domain package is atomic indivisible context Pros/cons Continuous development on modules whichare not able to compile 15. Dependencies Matrix of a Real Application without Maven 16. Can we apply fine grained modularity in this s/w design ? No, we cannot apply it without redesigningdependencies Reason: For instancethe interface parser.ISheetParser dependson data.SheetDocument and vice-versa. Current s/w design has cyclic dependencies inpackages. 17. Data depends on Parser Package 18. Parser Depends on Data Package 19. How to prevent from CyclicDependencies? Acyclic Dependency Principle (ADP) Add an extra module/package withan abstract Class or an Interface 20. Lets prevent from cyclic dependenciesin continuous development Make custom dependency rules unbreakablein package hierarchy 21. Lets prevent from cyclic dependenciesin continuous development 22. Lets prevent from cyclic dependenciesin continuous developmentUnfortunately 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. 23. Lets prevent from cyclic dependenciesin continuous development 24. In the Maven this is moresophisticated The key is relationship between modules Maven is a build tool Maven controls the build life cycle Maven can be used in IDE without IDEsproject files (including some globalsettings.jar) 25. Applying Maven as a Tool A bad design of POM.XML when modularity isnot properly considered; POM.XML reflecting a good modularityconcept 26. Minimum information for modular app pom.xml mvn idea:idea(mvn org.apache.maven.plugins:maven-idea-plugin:idea) 27. DemoLets create the IDEA project files from pom.xml 28. Minimum information for modular app Using Maven in IDEA we Do NOT need IDEAproject files in CVS. Mainly you need Maven command line, pom.xml and settings.jar 29. Development using IDEA and MavenTwo 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 IDEA Create New Project|Create project from scratch|Maven Module| 30. DemoNow follow the second approach in IDEA. 31. Demo with complete project Setting Maven Home directory in IDEA Maven settings.xml in IDEA Specifying local repository in IDEA 32. Introduction to the Standard DirectoryLayout 33. As a convention we use these filesin ${basedir} LICENSE.TXT NOTICE.TXT README.TXT 34. Maven Project Settings in IDEA 35. Demo Started Build Start the build in profile development Start the build in profile release 36. Maven 2 Installation Package ${maven.home}/bin/*.*runtime and debugging scripts ${maven.home}/boot/*.jarclassloading framework ${maven.home}/conf/settings.xml ${maven.home}/lib/*.jarMaven 2 core library for core classloader 37. Maven 2 Core Classloader The Apache Codehaus Classworlds JAR is usedto boot the classloader graph. The core classloader uses libraries in${maven.home}/lib and extensions. These libraries in Maven 2 are visible tocurrent and subsequent projects. Changes in Maven 3.1 + OSGiReferenceshttp://maven.apache.org/guides/mini/guide-maven-classloading.htmlhttp://www.parleys.com/#st=5&id=1630 38. Maven 2 Plugin Classloader Has plugins dependencies list as specified byplugins/plugin section in POM XML; Does not use projects classloader, but mayrequire using custom class loader of projectscompile, runtime, and test class path.Referenceshttp://maven.apache.org/guides/mini/guide-maven-classloading.html 39. Building Phases in Maven Core *uber.jar!/**/*.*/components.xmlcontains default phases per packaging typeReferencehttp://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html 40. Demo Super POM In Maven 2 installation dir (by default: $(user.home)/.m2/) with modelversion 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 41. What did we create?com.idsscheerspreadsheet2.0.1 42. Identifying Artifact Min required fields is groupId:artifactId:versionso-called standard coordinate. The groupId andartifactId 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., orcombined with an extension, e.g. test-jar. 43. Demo Newly created structured using Maven Compare with old project structure ofdirectory layout Clarity and minimum info in new structure 44. Offline Command Line Options -o #maven build in offline mode -npu #no plugin updates -npl #no plugin latest 45. Maven Project ModelExplaining XML tags in POMReference:http://maven.apache.org/ref/2.2.1/maven-model/maven.html 46. settings.xmlhttp://maven.apache.org/settings.html 47. 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 diagramReferences:http://www.objectmentor.com/resources/articles/granularity.pdfhttp://www.infoq.com/articles/modular-java-what-is-ithttp://java.dzone.com/articles/java-modularity-2-why 48. Modularity only If no pure Reusability? Still makes sense to have modular app if purereusability (one usage of a module). 49. Modularity sustains a continuous Progress of Development From Spaghetti code to modular app 50. What is NOT Artifact Reusability Textually inserted bunch of program toanother 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 51. What is Artifact Reusability My project links with another dependency. Whenever a new dependency artifact is fixedor enhanced, I receive new version.Reference:http://www.objectmentor.com/resources/articles/granularity.pdf 52. 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 developmentmay not necessarily have a global picture ofview, which on the other hand improved workconcentrations in current project of module. 53. Artifact Versioning ..- 1.3-beta-01 1.3.5 1.3.5-SNAPSHOT 1.3.5-1234567890References:http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-pom-syntax.htmlhttp://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-project-dependencies.html#pom-relationships-sect-version-ranges 54. NoticeSeparating 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 55. NoticeIf specifying dependency or plugin version like:1.2.3This is Mavens freedom to prefer version 1.2.3or take other in conflicts.Explicit version must be encapsulated:[1.2.3]Reference (conflicts resolution):http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html 56. Artifact Versions in Range [4.0,) (,2.0) [2.0] [3.8,4.0) 57. Two contradictory goals Need for an overall of big picture in multimodule projects. The goal of high module independence is toknow as little as possible of system reminders 58. 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) tosrc/main/java/ Moved applications from ./source/*.java to src/test/java/ Moved JUnit tests from ./testsource/*.java tosrc/test/java/ Moved one resource ./build/gen/**/SheetParser.tokensto src/main/resources/ 59. What to do when tests failSee the logs and localize the detailed error desc. running ASheetOperationTestCaseTests run: 5, Failures: 1, Errors: 4, Skipped:0, Time elapsed: 0.07 sec