Exigen Services confidential Exigen Services confidential
Apache Maven 2 Overview
Part 2
Advanced Topics of Apache Maven 2
Anatoly Kondratyev
September 2012
20 July 2015
Exigen Services confidential
The Goal
• Understand several Maven capabilities
• Build multi-module project with Maven
• Some special pom blocks
• Nexus configuration notes
2
Exigen Services confidential
WORKING WITH MULTI-MODULE
PROJECTS
Maven in real world
3
Exigen Services confidential
Project contents
4
GWT
application
EJB BEJB A
My Library
Exigen Services confidential
What to do with Maven?
• 4 independent artifacts with dependencies
• Build in one step?
• Organize versioning?
• Keeping up to date?
• Wrong!
• Maven Inheritance and Aggregation
• Solves the above problems
• Right!
5
Exigen Services confidential
Maven Inheritance & Aggregation
• <packaging>pom</packaging>
• Super pom
• Data in parent pom is inherited
• Maven dependency reactor
• Notes
• No cyclic dependencies
• No same modules
6
Exigen Services confidential
Maven Inheritance & Aggregation
Parent
pom
EJB A
ejb
EJB B
ejb
Gwt
war
My library
jar
7
Exigen Services confidential
Maven in action
8
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>ru.exigenservices</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>EJB-A</module>
<module>EJB-B</module>
<module>Gwt</module>
<module>MyLibrary</module>
</modules>
</project>
Exigen Services confidential
Maven in action
• What about deployment?
• EAR needed
• Special step needed
• Maybe divide frontend and backend?
• NB! One pom – one artifact
9
Exigen Services confidential
Maven in action
Parent
pom
Frontend
pom
Gwt
war
Frontend
wrapper
ear
Backend
pom
EJB A
ejb
EJB B
ejb
Backend
wrapper
ear
My library
jar
10
Exigen Services confidential
Multimodal hierarchy
• Parent pom
<project …>
<groupId>exigen</groupId>
<artifactId>parent</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>backend</module>
<module>frontend</module>
</modules>
</project>
• Frontend pom
<project …>
<parent>
<groupId>exigen</groupId>
<artifactId>parent</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>frontend</artifactId>
<packaging>pom</packaging>
<modules>
<module>Gwt</module>
</modules>
</project>
11
Exigen Services confidential
Dependency&Plugin management
• Changeable, overriddable and inheritable
• In parent
• GroupId & ArtifactId
• Version
• Everything you can configure
• Type, packaging
• In child
• GroupId & ArtifactId
12
Exigen Services confidential
Dependency&Plugin Management
Parent:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
Child:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
</dependencies>
13
Exigen Services confidential
Flat vs Tree, Flat
.
|-- my-module
| `-- pom.xml
`--parent
`--pom.xml
• Pom.xml for my-module:
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<relativePath>.../parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-module</artifactId>
</project>
15
Exigen Services confidential
Maven reactor
• Collects all the available modules to build
• Sorts the projects into the correct build order
• a project dependency on another module in the build
• different rules with plugins dependencies
• the order declared in the <modules> element (if no other rule
applies)
• Builds the selected projects in order
• Be aware of cycles and same modules on different
parents
16
Exigen Services confidential
Conclusion
• Inheritance and aggregation
• Flat/Tree structure
• Maven reactor
• Dependency&Plugin management
• Deploy to Nexus/Weblogic problem
17
Exigen Services confidential
PROPERTIES, PROFILES,
EXECUTION BLOCKS
What will help you
18
Exigen Services confidential
Maven properties
• Just as common Ant properties
• ${property_name}
• Case sensitive
• Upper case for environment variables
• Dot(.) notated path
19
Exigen Services confidential
Predefined properties
• Build in properties
• ${basedir} – directory with pom
• ${version} – artifact version
• Project properties
• ${project.build.directory}
• ${project.build.outputDirectory} (target/classes)
• ${project.name}
• ${project.version}
• Local user settings
• ${settings.localRepository}
• Environment properties
• ${env.M2_HOME}
20
Exigen Services confidential
Maven profiles
• Maven profile – special way for configuring
build
• Different environments – different results
• Renaming
• Different build cycles
• Special plugin configuration
• Just different targets
• For different users
22
Exigen Services confidential
Maven profiles
• Per project
• pom.xml
• Per user
• %USER_HOME%/.m2/settings.xml
• Per computer (Global)
• %M2_HOME%/conf/settings.xml
23
Exigen Services confidential
Maven profiles
• Activation
• By hand (-P profile1,profile2)
• <activeProfiles>
• <activation>
• By environment settings
• By properties
24
Exigen Services confidential
Maven profiles
<profiles>
<profile>
<activation>
<jdk>[1.3,1.6)</jdk>
</activation>
...
</profile>
<profile>
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
25
Exigen Services confidential
Mojo
• Plugin
• Executing action
• Mojo – magical charm in hoodoo
• Just a Goal
• Plugin consists of Mojos
• Some parameters
• MOJO aka POJO (Plain-old-Java-object)
27
Exigen Services confidential
Mojo
• When we should use mojos?
• Run from command line
• Different execution parameters for different
configurations
• Group of mojos from same plugin with
different configuration
28
Exigen Services confidential
Execution blocks
Plugin:time
<plugin>
<groupId>com.mycompany.example</groupId>
<artifactId>plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>first</id>
<phase>test</phase>
<goals>
<goal>time</goal>
</goals>
<configuration> … </configuration>
</execution>
<execution>
<id>default</id>
…
<!– No phase block -->
</execution>
</executions>
</plugin>
29
Exigen Services confidential
SCM SourceCodeManagement
• CVS, Mercurial, Perforce, Subversion, etc.
• Commit/update
• scm:bootstrap – build project
• Maven Release Plugin
• Snapshot  Version
• Check everything
• Commit
30
Exigen Services confidential
Maven archetypes
• Create folders, poms, general stuff
• Archetype  Project
• For creating project:
• archetype:generate
• Select archetype from list
• Set up groupId, artifactId, version, …
• Get all project structure and draft files
• maven-archetype-quickstart
• For creating archetype:
• archetype:create-from-project
31
Exigen Services confidential
Provided Arhetypes
• maven-archetype-archetype
• Sample archetype
• maven-archetype-j2ee-simple
• Simplified sample J2EE application
• maven-archetype-plugin
• Sample Maven plugin
• maven-archetype-quickstart
• Sample Maven project
• maven-archetype-webapp
• Sample Maven Webapp project
• More information:
http://maven.apache.org/archetype/maven-archetype-bundles/
32
Exigen Services confidential
archetype:create-from-project
• Sample project
• mvn archetype:create-from-project
• src catalog
• Pom.xml (maven-archetype)
• Some resources
• Modify code (…targetgenerated-sourcesarchetype)
• archetype-metadata.xml
• Update properties and default values; review
• Go to target, run “mvn install”
• To test archetype
“mvn archetype:generate -DarchetypeCatalog=local”
33
Exigen Services confidential
NEXUS
SETTING.XML
Good configuration - great advantage
34
Exigen Services confidential
Sonatype Nexus
• Artifact repository
• Nexus and Nexus Pro
• Rather simple
• Widely used
• Other Maven Repository Managers
• Apache Archiva
• Artifactory
• Comparison
http://docs.codehaus.org/display/MAVENUSER/Maven+Repository+Manager+Fe
ature+Matrix
35
Exigen Services confidential
Nexus hints
• Nexus configuration + local configuration
• Proxy repositories
• Add everything and cache it!
• Add to Public Repositories group
• Restrictions on uploading artifacts
• UI: Artifact Upload
36
Exigen Services confidential
Settings.xml
• Servers
<servers>
<server>
<id>nexus</id>
<username>…</username>
<password>…</password>
</server>
</servers>
• Mirrors (2.0.9)
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localserver:8081/nexus/content/groups/public/
</url>
</mirror>
</mirrors>
• Active Profile
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
• Profile
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
…
</pluginRepositories>
</profile>
</profiles>
37
Exigen Services confidential
updatePolicy
• UpdatePolicy
• Always
• Daily (default)
• Interval:X (X – integer in minutes)
• Never
• Repository Snapshots
• Enabled true/false
38
Exigen Services confidential
LICENSE, ORGANIZATION,
DEVELOPERS, CONTRIBUTORS
When you have free time…
39
Exigen Services confidential
Licenses
• How your project could be used?
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
40
Exigen Services confidential
Organization
• Just tell everybody!
<organization>
<name>Exigen Services</name>
<url>http://www.exigenservices.ru/</url>
</organization>
41
Exigen Services confidential
Developers & Contributors block
• Id, name, email
• Organization, organizationUrl
• Roles
• Timezone
• Properties
• Contributors don’t have Id
42
Exigen Services confidential
Developers block
<developers>
<developer>
<id>anatoly.k</id>
<name>Anatoly</name>
<email>Anatoly.Kondratiev@exigenservices.com</email>
<organization>Exigen</organization>
<organizationUrl>http://www.exigenservices.ru/</organizationUrl>
<roles>
<role>Configuration Manager</role>
<role>Developer</role>
</roles>
<timezone>+3</timezone>
<properties>
<skype>anatoly.kondratyev</skype>
</properties>
</developer>
</developers>
Exigen Services confidential
Final notes
• A great amount of plugins
• Ant-run
• Mirrors on local repo, not on computer
44
Exigen Services confidential
QUESTIONS
Now it’s your turn…
45

Apache maven 2. advanced topics

  • 1.
    Exigen Services confidentialExigen Services confidential Apache Maven 2 Overview Part 2 Advanced Topics of Apache Maven 2 Anatoly Kondratyev September 2012 20 July 2015
  • 2.
    Exigen Services confidential TheGoal • Understand several Maven capabilities • Build multi-module project with Maven • Some special pom blocks • Nexus configuration notes 2
  • 3.
    Exigen Services confidential WORKINGWITH MULTI-MODULE PROJECTS Maven in real world 3
  • 4.
    Exigen Services confidential Projectcontents 4 GWT application EJB BEJB A My Library
  • 5.
    Exigen Services confidential Whatto do with Maven? • 4 independent artifacts with dependencies • Build in one step? • Organize versioning? • Keeping up to date? • Wrong! • Maven Inheritance and Aggregation • Solves the above problems • Right! 5
  • 6.
    Exigen Services confidential MavenInheritance & Aggregation • <packaging>pom</packaging> • Super pom • Data in parent pom is inherited • Maven dependency reactor • Notes • No cyclic dependencies • No same modules 6
  • 7.
    Exigen Services confidential MavenInheritance & Aggregation Parent pom EJB A ejb EJB B ejb Gwt war My library jar 7
  • 8.
    Exigen Services confidential Mavenin action 8 <project …> <modelVersion>4.0.0</modelVersion> <groupId>ru.exigenservices</groupId> <artifactId>my-parent</artifactId> <version>1.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>EJB-A</module> <module>EJB-B</module> <module>Gwt</module> <module>MyLibrary</module> </modules> </project>
  • 9.
    Exigen Services confidential Mavenin action • What about deployment? • EAR needed • Special step needed • Maybe divide frontend and backend? • NB! One pom – one artifact 9
  • 10.
    Exigen Services confidential Mavenin action Parent pom Frontend pom Gwt war Frontend wrapper ear Backend pom EJB A ejb EJB B ejb Backend wrapper ear My library jar 10
  • 11.
    Exigen Services confidential Multimodalhierarchy • Parent pom <project …> <groupId>exigen</groupId> <artifactId>parent</artifactId> <version>1.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>backend</module> <module>frontend</module> </modules> </project> • Frontend pom <project …> <parent> <groupId>exigen</groupId> <artifactId>parent</artifactId> <version>1.0.1-SNAPSHOT</version> </parent> <artifactId>frontend</artifactId> <packaging>pom</packaging> <modules> <module>Gwt</module> </modules> </project> 11
  • 12.
    Exigen Services confidential Dependency&Pluginmanagement • Changeable, overriddable and inheritable • In parent • GroupId & ArtifactId • Version • Everything you can configure • Type, packaging • In child • GroupId & ArtifactId 12
  • 13.
    Exigen Services confidential Dependency&PluginManagement Parent: <dependencyManagement> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement> Child: <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> </dependency> </dependencies> 13
  • 14.
    Exigen Services confidential Flatvs Tree, Flat . |-- my-module | `-- pom.xml `--parent `--pom.xml • Pom.xml for my-module: <project> <parent> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> <relativePath>.../parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>my-module</artifactId> </project> 15
  • 15.
    Exigen Services confidential Mavenreactor • Collects all the available modules to build • Sorts the projects into the correct build order • a project dependency on another module in the build • different rules with plugins dependencies • the order declared in the <modules> element (if no other rule applies) • Builds the selected projects in order • Be aware of cycles and same modules on different parents 16
  • 16.
    Exigen Services confidential Conclusion •Inheritance and aggregation • Flat/Tree structure • Maven reactor • Dependency&Plugin management • Deploy to Nexus/Weblogic problem 17
  • 17.
    Exigen Services confidential PROPERTIES,PROFILES, EXECUTION BLOCKS What will help you 18
  • 18.
    Exigen Services confidential Mavenproperties • Just as common Ant properties • ${property_name} • Case sensitive • Upper case for environment variables • Dot(.) notated path 19
  • 19.
    Exigen Services confidential Predefinedproperties • Build in properties • ${basedir} – directory with pom • ${version} – artifact version • Project properties • ${project.build.directory} • ${project.build.outputDirectory} (target/classes) • ${project.name} • ${project.version} • Local user settings • ${settings.localRepository} • Environment properties • ${env.M2_HOME} 20
  • 20.
    Exigen Services confidential Mavenprofiles • Maven profile – special way for configuring build • Different environments – different results • Renaming • Different build cycles • Special plugin configuration • Just different targets • For different users 22
  • 21.
    Exigen Services confidential Mavenprofiles • Per project • pom.xml • Per user • %USER_HOME%/.m2/settings.xml • Per computer (Global) • %M2_HOME%/conf/settings.xml 23
  • 22.
    Exigen Services confidential Mavenprofiles • Activation • By hand (-P profile1,profile2) • <activeProfiles> • <activation> • By environment settings • By properties 24
  • 23.
    Exigen Services confidential Mavenprofiles <profiles> <profile> <activation> <jdk>[1.3,1.6)</jdk> </activation> ... </profile> <profile> <activation> <property> <name>environment</name> <value>test</value> </property> </activation> ... </profile> </profiles> 25
  • 24.
    Exigen Services confidential Mojo •Plugin • Executing action • Mojo – magical charm in hoodoo • Just a Goal • Plugin consists of Mojos • Some parameters • MOJO aka POJO (Plain-old-Java-object) 27
  • 25.
    Exigen Services confidential Mojo •When we should use mojos? • Run from command line • Different execution parameters for different configurations • Group of mojos from same plugin with different configuration 28
  • 26.
    Exigen Services confidential Executionblocks Plugin:time <plugin> <groupId>com.mycompany.example</groupId> <artifactId>plugin</artifactId> <version>1.0</version> <executions> <execution> <id>first</id> <phase>test</phase> <goals> <goal>time</goal> </goals> <configuration> … </configuration> </execution> <execution> <id>default</id> … <!– No phase block --> </execution> </executions> </plugin> 29
  • 27.
    Exigen Services confidential SCMSourceCodeManagement • CVS, Mercurial, Perforce, Subversion, etc. • Commit/update • scm:bootstrap – build project • Maven Release Plugin • Snapshot  Version • Check everything • Commit 30
  • 28.
    Exigen Services confidential Mavenarchetypes • Create folders, poms, general stuff • Archetype  Project • For creating project: • archetype:generate • Select archetype from list • Set up groupId, artifactId, version, … • Get all project structure and draft files • maven-archetype-quickstart • For creating archetype: • archetype:create-from-project 31
  • 29.
    Exigen Services confidential ProvidedArhetypes • maven-archetype-archetype • Sample archetype • maven-archetype-j2ee-simple • Simplified sample J2EE application • maven-archetype-plugin • Sample Maven plugin • maven-archetype-quickstart • Sample Maven project • maven-archetype-webapp • Sample Maven Webapp project • More information: http://maven.apache.org/archetype/maven-archetype-bundles/ 32
  • 30.
    Exigen Services confidential archetype:create-from-project •Sample project • mvn archetype:create-from-project • src catalog • Pom.xml (maven-archetype) • Some resources • Modify code (…targetgenerated-sourcesarchetype) • archetype-metadata.xml • Update properties and default values; review • Go to target, run “mvn install” • To test archetype “mvn archetype:generate -DarchetypeCatalog=local” 33
  • 31.
    Exigen Services confidential NEXUS SETTING.XML Goodconfiguration - great advantage 34
  • 32.
    Exigen Services confidential SonatypeNexus • Artifact repository • Nexus and Nexus Pro • Rather simple • Widely used • Other Maven Repository Managers • Apache Archiva • Artifactory • Comparison http://docs.codehaus.org/display/MAVENUSER/Maven+Repository+Manager+Fe ature+Matrix 35
  • 33.
    Exigen Services confidential Nexushints • Nexus configuration + local configuration • Proxy repositories • Add everything and cache it! • Add to Public Repositories group • Restrictions on uploading artifacts • UI: Artifact Upload 36
  • 34.
    Exigen Services confidential Settings.xml •Servers <servers> <server> <id>nexus</id> <username>…</username> <password>…</password> </server> </servers> • Mirrors (2.0.9) <mirrors> <mirror> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://localserver:8081/nexus/content/groups/public/ </url> </mirror> </mirrors> • Active Profile <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> • Profile <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://central</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> … </pluginRepositories> </profile> </profiles> 37
  • 35.
    Exigen Services confidential updatePolicy •UpdatePolicy • Always • Daily (default) • Interval:X (X – integer in minutes) • Never • Repository Snapshots • Enabled true/false 38
  • 36.
    Exigen Services confidential LICENSE,ORGANIZATION, DEVELOPERS, CONTRIBUTORS When you have free time… 39
  • 37.
    Exigen Services confidential Licenses •How your project could be used? <licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> <comments>A business-friendly OSS license</comments> </license> </licenses> 40
  • 38.
    Exigen Services confidential Organization •Just tell everybody! <organization> <name>Exigen Services</name> <url>http://www.exigenservices.ru/</url> </organization> 41
  • 39.
    Exigen Services confidential Developers& Contributors block • Id, name, email • Organization, organizationUrl • Roles • Timezone • Properties • Contributors don’t have Id 42
  • 40.
    Exigen Services confidential Developersblock <developers> <developer> <id>anatoly.k</id> <name>Anatoly</name> <email>Anatoly.Kondratiev@exigenservices.com</email> <organization>Exigen</organization> <organizationUrl>http://www.exigenservices.ru/</organizationUrl> <roles> <role>Configuration Manager</role> <role>Developer</role> </roles> <timezone>+3</timezone> <properties> <skype>anatoly.kondratyev</skype> </properties> </developer> </developers>
  • 41.
    Exigen Services confidential Finalnotes • A great amount of plugins • Ant-run • Mirrors on local repo, not on computer 44
  • 42.