Download presentation
Presentation is loading. Please wait.
Published byPrudence Thomas Modified over 8 years ago
1
Exploring Maven 2 Craig Walls Gateway Software Symposium 2007 craig@habuma.com Blog: http://www.springinaction.com Wiki: http://wiki.habuma.com Examples: svn://svn.geekisp.com/SiA svn:svn.geekisp.com/habuma
2
About you… Java?.NET? Ruby/Rails? Scala? 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? Favorite session so far? What brought you to this session?
3
About me Agile developer with Semantra –Natural language business intelligence 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
4
Agenda Introduction to Maven 2 Exploring the Project Object Model (aka, the POM) Maven in Action
5
Intro to Maven 2
6
What is Maven? Build tool? Project management tool? Dependency management tool? Documentation tool?
7
How do I get Maven? Top level Apache project –http://maven.apache.org Current version is 2.0.7 Don’t confuse Maven 2 with Maven 1
8
Maven vs. Ant Ant Boilerplate build code Explicit target definitions Explicit project structure Dependencies must be provided by developer Maven Declarative Common tasks are built-in Project structure imposed by Maven Dependencies declared (Maven gets them for you)
9
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.
10
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.
11
Maven principles Convention over configuration Declarative execution Reuse of build logic Coherent organization of dependencies
12
Consistent directory structure How Maven knows where everything is. How the developer knows where everything is.
13
Maven lifecycle validate generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources generate-test-resources process-test-resources test-compile test package pre-integration-test integration-test post-integration-test verify install deploy
14
Running Maven % mvn [options] [goal(s)] [phase(s)] Examples: % mvn -o clean package % mvn tomcat:deploy % mvn site
15
Exploring the POM
16
Key POM sections Basic project info Build section Reporting section Dependencies section Profiles section
17
Basic project info Basic project information… 4.0.0 Poker Service com.habuma Poker 1.0 war Sample Spring-WS service […]
18
Build settings Used to define build properties and configure plugins… Poker …
19
Reports settings Configure plugins for generating reports org.codehaus.mojo jxr-maven-plugin
20
Dependencies Tells Maven what other libraries this app depends upon… org.springframework spring 2.0 compile
21
Dependency resolution Dependencies are automatically resolved… –First looks in local repository: ~/.m2/repository –Then looks in one or more remote repositories Default: http://repo1.maven.org Transitive dependencies –Your project depends on Hibernate… –…which depends on cglib…which depends on… SNAPSHOT dependencies –Downloaded anew once every day
22
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.
23
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
24
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
25
Profiles Profiles enable you to override certain properties for different scenarios. itest … Enacted with -P switch on command line.
26
Maven in Action No…this isn’t a book title
27
Maven project kickstart Use the Maven archetype plugin to go from zero to working app in about 1 second… % mvn archetype:create -DgroupId=com.habuma -DartifactId=MyAwesomeApp
28
Other archetypes… maven-archetype-* –archetype –bundles –j2ee-simple –marmalade-mojo –mojo –plugin-site –plugin –portlet –profiles –quickstart –simple –site-simple –site –webapp
29
Using Maven with Eclipse Two (somewhat confusing) choices… –Eclipse plugin for Maven Generates Eclipse.project and.classpath files Ensures all dependencies are in Eclipse project’s classpath % mvn eclipse:eclipse –Maven plugin for Eclipse Teaches Eclipse to use pom.xml’s dependencies as classpath entries http://m2eclipse.codehaus.org Update site: http://m2eclipse.codehaus.org/updatesite
30
Compiling, testing, packaging,etc Most basic project build activities are accomplished through Maven’s lifecycle goals. –Compile: % mvn compile –Run unit tests: % mvn test –Create a JAR, WAR, EAR, etc: % mvn package
31
Choosing a Java version Configure the compiler plugin… […] […] org.apache.maven.plugins maven-compiler-plugin 1.5
32
Tweaking the packaging name By default, the project name and version number end up in the generated package –MyProject-2.1.3.war Use to set the…er…final name of the package ${project.name}
33
Filtering resources Configure resources for filtering… src/main/filters/other.properties src/main/resources true Use properties in resource files… application.name=${pom.name} application.version=${pom.version} application.contactNumber=${other.contactNumber} Filters will be applied during the “process-resources” goal Optional
34
Cleaning out all build artifacts The “clean” goal removes the target directory –Not a lifecycle goal…part of the “clean” plugin –Effectively removing all build artifacts % mvn clean Can be used along with other goals % mvn clean package
35
Automatically clean every time If you can’t or don’t want to type clean every time… –Attach the “clean” goal to the “validate” lifecycle goal: [...] maven-clean-plugin auto-clean validate clean [...]
36
Setting properties Use a block to set properties: 2.0.6 Use ${} syntax for referencing properties: org.springframework spring ${spring.version} compile
37
Finding dependencies in ibiblio Use http://mvnrepository.com to find dependencies you aren’t sure of… –Search engine for http://repo1.maven.org/maven2 –Shows all available versions –Includes element, suitable for cut-n-paste into your pom.xml
38
mvnrepository.com
39
Using other repositories Add a new repository to your build: Codehaus Codehaus Repository http://repository.codehaus.org default Other repositories you might like to know about… –http://s3.amazonaws.com/maven.springframework.org/snapshot –http://download.java.net/maven/2/ –http://snapshots.repository.codehaus.org –http://repository.codehaus.org
40
Install a JAR into the local repo If the project’s packaging is “jar”, the “install” lifecycle goal will do: % mvn install To install a 3rd party JAR (assuming that it’s not already in a repository): % mvn install:install-file -DgroupId=com.bigmoneysoftware -DartifactId=ProprietaryLibrary -Dversion=1.2.1 -Dpackaging=jar -Dfile=MyLibrary-1.2.1.jar
41
Running offline If no net access… % mvn -o goals You must have all of the project’s dependencies in the local repository
42
Kickstart a web app project Use Maven’s webapp archetype: % mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetype -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.habuma -DartifactId=MyWebApp
43
Deploy a web app to a server Use Cargo’s (http://cargo.codehaus.org) Maven plugin org.codehaus.cargo cargo-maven2-plugin 0.3 tomcat5x remote runtime localhost 8080 admin password Note: Tomcat must be running!
44
Deploy to embedded Jetty Add the Jetty plugin […] […] org.mortbay.jetty maven-jetty-plugin Start Jetty and run your app: % mvn jetty:run
45
Multi-module projects Create a top-level project with “pom” packaging 4.0.0 com.habuma 1.0-SNAPSHOT MySuperApp pom my-lib my-webapp Reference parent in child POMs com.habuma MySuperApp 1.0-SNAPSHOT […]
46
Exclude integration tests Configure SureFire plugin to skip “ITest” classes […] […] maven-surefire-plugin **/*ITest*
47
Running integration tests Create an “itest” profile that does not exclude “ITest” classes: itest org.apache.maven.plugins maven-surefire-plugin none Run integration tests with… % mvn test -Pitest
48
Getting past those pesky firewalls In settings.xml: true http proxy.habuma.com 8080 mwalls letmeout01
49
Embedding Ant Use the “antrun” plugin… […] […] org.apache.maven.plugins maven-antrun-plugin ant ant-antlr 1.6.5 Do Ant stuff Execute Ant... % mvn antrun:run
50
Attaching Ant to a lifecycle goal Place inside of an block: org.apache.maven.plugins maven-antrun-plugin […] install Do Ant stuff
51
Getting some 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
52
Generate a project website Use the “site” plugin: % mvn site Kickstart a project with a site structure in place: % mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-site -DgroupId=com.habuma -DartifactId=MyAwesomeApp
53
Give credit to project members Developer info will be included in generated site: craig Craig Walls craig@habuma.com Head honcho Developer habuma.com -5
54
Continuous Integration Includes a link to the CI system in the site Cruise Control http://ci.habuma.com/cruisecontrol dev@habuma.com
55
Issue Management Includes a link to the issue management system JIRA jira.habuma.com
56
Licenses Includes a license page on the site Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt repo
57
Mailing Lists Includes mailing list information on the generated project web site Poker Service Mailing List subscribe@habuma.com unsubscribe@habuma.com> poker-dev@habuma.com http://mail-archives.habuma.com
58
Reporting: JavaDoc Generates JavaDoc and includes in generated project web site: maven-javadoc-plugin
59
Reporting: Java XRef Includes a Java cross-refence with the generated project web site org.codehaus.mojo jxr-maven-plugin
60
Reporting: Test coverage Includes test code coverage report org.codehaus.mojo cobertura-maven-plugin
61
Reporting: Test reports Includes unit test reports org.apache.maven.plugins maven-surefire-plugin
62
Reporting: JDepend Produces a JDepend report and includes with generated project web site org.codehaus.mojo jdepend-maven-plugin
63
Additional site content Placed in src/site/… –…/apt : Almost plain text –…/fml : FAQ markup –…/xdoc : XDoc files –site.xml : Site structure
64
Creating an archetype Build a project with the following structure: Or better yet…use the archetype archetype % mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-archetype -DgroupId=com.habuma -DartifactId=MyArchetype
65
An archetype’s pom.xml 4.0.0 com.habuma MyArchetype maven-plugin 1.0
66
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 […]
67
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.
68
Final 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!
69
Additional Maven resources “Get the most out of Maven 2 site generation” http://www.javaworld.com/javaworld/jw-02-2006/jw-0227-maven.html “The Maven 2 POM Demystified” http://www.javaworld.com/javaworld/jw-05-2006/jw-0529-maven.html “Better Builds with Maven: How-to Guide for Maven 2” http://www.devzuz.com/web/guest/products/resources#BBWM “Maven: The Definitive Guide” http://www.sonatype.com/book/
70
Q & A Don’t forget to turn in evals!!! http://www.springinaction.com craig-sia@habuma.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.