Managing your builds with Maven 2 Craig Walls LoneStar Software Symposium
About you… Java?.NET? Ruby/Rails? Erlang? –Java 6? Java 5? Java 1.4? Java 1.3? 1.2 or older? Who’s using Maven? How long? –Maven 1? Maven 2?
About me Agile developer with Semantra –Natural language data access Developing software professionally for over 13 years –Telecom, Finance, Retail, Education –Java for most of that –Also some C/C++, C#/.NET, Ruby Spring fanatic
Agenda Intro to Maven 2 Exploring the POM Dependencies Profiles Project relationships Archetypes Project web site generation Q & A
Maven resources “Get the most out of Maven 2 site generation” “The Maven 2 POM Demystified” “Better Builds with Maven: How-to Guide for Maven 2” “Maven: The Definitive Guide”
Intro to Maven 2
What is Maven? Build tool? Project management tool? Dependency management tool? Documentation tool?
Maven vs. Ant Ant Boilerplate build code Explicit target definitions Explicit project structure Dependencies must be provided by programmer Maven Declarative Common tasks are built-in Project structure imposed by Maven Dependencies declared (Maven gets them for you)
MYTH: Maven is hard Maven is different than Ant…but not necessarily harder. Q: Is Chinese harder than English? –1.2 billion people don’t think so. In most cases, Maven is significantly simpler than Ant. You must be willing to think differently about how you build your projects.
MYTH: Maven is slow Fact: Maven is slow when it needs to download a lot of dependencies. Dependencies are cached locally, so future builds will be faster. Typical builds are at least as fast (and maybe faster) than Ant.
Maven principles Convention over configuration Declarative execution Reuse of build logic Coherent organization of dependencies
Consistent directory structure How Maven knows where everything is. How the developer knows where everything is.
Maven lifecycle 1.validate 2.generate-sources 3.process-sources 4.generate-resources 5.process-resources 6.compile 7.process-classes 8.generate-test-sources 9.process-test-sources 10.generate-test-resources 11.process-test-resources 12. test-compile 13. test 14. package 15. pre-integration-test 16. integration-test 17. post-integration-test 18. verify 19. install 20. deploy
Running Maven % maven [options] [goal(s)] [phase(s)] Examples: % maven -o clean package % maven tomcat:deploy % maven site
Exploring the POM
Key POM sections Basic project info Build section Dependencies section Reporting section Profiles section
Basic project info Poker Service com.habuma Poker 1.0 war Sample Spring-WS service
Build section Poker …
Build section: Java 1.5 maven-compiler-plugin 1.5
Build section: Cargo org.codehaus.cargo cargo-maven2-plugin 0.2 tomcat5x remote runtime localhost 8080 admin password
Build Section: Jetty Adding the Jetty plugin: org.mortbay.jetty maven-jetty-plugin Start Jetty from Maven (and run your app): % mvn jetty:run
Misc properties 2.0 org.springframework spring ${spring.version} compile
Dependencies
Dependency resolution Dependencies are automatically resolved… –First looks in local repository: ~/.m2/repository –Then looks in one or more remote repositories Default: Transitive dependencies –Your project depends on Hibernate… –…which depends on cglib…which depends on… SNAPSHOT dependencies –Downloaded anew once every day
Other repositories Add a new repository to your build: Codehaus Codehaus Repository default
Dependency resolution (2) org.springframework spring 2.0 compile Resolves to: [repository]/org/springframework/spring/spring-2.0.jar
Dependency scopes compile - Dependency is made available in all classpaths (compile, test, and runtime) –Most dependencies will be in this scope provided - Dependency is made available during compile, but will be provided by JDK or container at runtime. –The Servlet API JAR is in this scope test - Dependency is made available only at test time –This scope is appropriate for JUnit. runtime - Dependency is made available in test and runtime classpaths, but not at compile-time system - Like “provided” but you must explicitly provide the dependency JAR file.
Online vs. offline Remote dependency resolution depends on internet connection. If you’ll be offline, run Maven with -o switch –Dependencies will only be resolved from local repository –Of course, dependencies must be in local repository
Those pesky firewalls Create ~/.m2/settings.xml: true http proxy.habuma.com 8080 mwalls letmeout01
What if a JAR isn’t in repository? If it’s open source, look again. It’s probably there. Licenses prevent commercial and some other (SUN) JAR files from being placed in public Maven repositories. –Options: Install in your own private corporate repository Install in your local repository
Installing JARs % mvn install:install-file -DgroupId=com.webmethods -DartifactId=glue -Dversion= Dpackaging=jar -Dfile=glue jar
Profiles
Profiles enable you to override certain properties for different scenarios. Defined in section: itest … Enacted with -P switch on command line.
Trick: Excluding integration tests In normal SureFire plugin maven-surefire-plugin **/*ITest* Test classes whose names end with “ITest” won’t be run.
Excluding integration tests(part 2) In “itest” profile itest org.apache.maven.plugins maven-surefire-plugin none Run integration tests with… % mvn test -Pitest
Project relationships
Parent POMs Projects can inherit project details from parent projects. –Properties, build details, and even dependencies are inherited Unless declared otherwise, a POM inherits from the “Super POM” –Think of it as Maven’s java.lang.Object Use in child POM: com.habuma basepom 1.0
Multi-module projects Base projects with POM containing… – pom – … Child projects to build individual JARs, WARs, etc. Could inherit common properties and dependencies from base project Child projects could be built individually or all at once (by building base project)
Archetypes
Zero to working app in 10 secs Archetypes are great way to kick-start a project. Running archetype generates a baseline project structure, ready to build. You add project details.
Running an archetype % mvn archetype:create -DarchetypeGroupId=com.habuma -DarchetypeArtifactId=spring-webapp-archetype -DarchetypeVersion=1.0 -DgroupId=com.habuma -DartifactId=MySillyWebApp -Dversion=0.1
Creating an archetype Build a project with the following structure:
An archetype’s pom.xml com.habuma spring-webapp-archetype maven-plugin 1.0
META-INF/archetype.xml webapp src/main/java/HomeController.java src/main/java/AddressFormController.java src/main/java/Address.java src/test/java/HomeControllerTest.java.project.classpath.springBeans src/main/webapp/WEB-INF/web.xml src/main/webapp/WEB-INF/mvc.xml …
archetype-resources Where the project template lives Includes any resources and source code that needs to be included in generated project. Source files are “packaged” automatically (be sure to include ${package} in.java files). Text files are filtered and placeholders are replaced.
Site generation
Developer list craig Craig Walls Head honcho Developer habuma.com -5
Continuous Integration Cruise Control
Issue Management JIRA jira.habuma.com
Licenses Apache License, Version repo
Mailing Lists Poker Service Mailing List
Reporting: JavaDoc maven-javadoc-plugin
Reporting: Java XRef org.codehaus.mojo jxr-maven-plugin
Reporting: Test coverage org.codehaus.mojo cobertura-maven-plugin
Reporting: Test reports org.apache.maven.plugins maven-surefire-plugin
Reporting: JDepend org.codehaus.mojo jdepend-maven-plugin
Additional site content Placed in src/site/… –…/apt : Almost plain text –…/fml : FAQ markup –…/xdoc : XDoc files –site.xml : Site structure
Final tips, gotchas and help
Tips & Gotchas Resolve dependency ambiguities manually Avoid SNAPSHOT dependencies Only deploy non- SNAPSHOT dependencies once Not all projects have quality POM files Be careful when upgrading Maven--test first!
Maven help help:active-profiles –Lists the profiles that are currently active for the build help:effective-pom –Displays the effective POM for the current build, with active profiles factored in help:effective-settings –Prints out calculated settings for the project, given any profile enhancement and the inheritence of global settings into the user settings. help:describe –Describes a plugin and its attributes
Q & A Feel free to ask by anytime: Recommended reading:
Thank you! Have a great day! Remember to return evaluations!