Basic Maven<br />Chris Roeder<br />Kevin Livingston<br />April 2011<br />5/5/11<br />1<br />Chris Roeder, Kevin Livingston...
Maven Big Picture:<br />Builds: Maven will compile your code and assemble jars. (similar to ant’s function)<br />Dependenc...
Contents<br />Introduction<br />Dependency Manangement<br />Builds and pom files<br />Modules<br />Repositories<br />Relea...
Introduction 1<br />Dependency Manager:Given a description of dependent jars, Maven will fetch them for you<br />Build  To...
Introduction 2<br />Maven is organized around projects that produce a single jar.(or sets of projects that each produce a ...
Introduction: Convention over Configuration<br />A big Maven philosophy is that life is easier with clear conventions:<br ...
Dependency Management<br />Maven is declarative (not imperative):<br />Specify the jars used, where they come from, and wh...
Dependency Management: Adding a new jar <br />Google “maven junit” to find the dependency for junit.<br />Add this to your...
Dependency Management:Finding the Jar<br />Most jars are in “The” repository, a public central repository for community us...
5/5/11<br />Chris Roeder, Kevin Livingston<br />10<br />
5/5/11<br />Chris Roeder, Kevin Livingston<br />11<br />
Build Tool<br />By default, Maven:<br /> knows where to find the code and resources. <br />You told it where and what the ...
Example POM<br /><?xml version="1.0" encoding="UTF-8"?><br /><project	xmlns="http://maven.apache.org/POM/4.0.0"<br />xmlns...
Example POM (cont)<br /><project><br />…<dependencies>		<dependency>			<groupId>log4j</groupId>			<artifactId>log4j</artif...
Example POM: Repository Stanza<br /><repositories>  <repository><id>apache incudbaor</id><name>apache-incubator</name><url...
Build Tool: unconventional<br />If you must work in a directory structure that doesn’t follow the convention…<br />…just t...
Modules:Families of Projects<br />You can have a “super project” that binds a number of sub-projects together. <br />A dir...
Parent Pom<br />A parent pom may be declared for each module that specifies common resources, dependencies, properties, et...
Properties<br />Not called “variable” because they aren’t<br />Declare them:<br />	<properties><br />			<clojure.version>1...
Repositories<br />Maven uses three levels of repositories:<br />Reactor: not really a repository. It’s part of a particula...
Artifactory<br />Artifactory is the Maven repository implementation used here:<br />http://amc-bakeoff.ucdenver.pvt:8081<b...
Snapshots<br />Maven doesn’t deal strictly with perfectly prepared releases.<br />Snapshots are a way of sharing a work-in...
Snapshots 2<br />A snapshot’s version says “SNAPSHOT”:<br />	<version>0.0.2-SNAPSHOT</version><br />A repository has to be...
Plugins<br />Maven uses plugins to add functionality.<br />They are retreived like other artifacts: from repositories.<br ...
Example Plugin Stanza<br /><plugin><groupId>org.apache.maven.plugins</groupId>	<artifactId>maven-compiler-plugin</artifact...
Mojo:Maven CommandsGoals<br />Maven is implemented in a number of plugins.<br />Each plugin has one or more goals (command...
Maven Phases<br />A phase can have zero or more goals associated with it.<br />A lifecycle is a series of phases leading u...
Runtime Tool<br />A plugin, exec, allows you to call java from Maven so you can use the classpath assembly features to run...
Runtime 2: The getResourceAsStream Soap Box<br />Not all projects remain naïve of the file system.<br />Some expect to be ...
Review: Maven Vocabulary<br />Project<br />Pom – a file that tells maven about a particular project.<br />Artifact – a jar...
Review: Standard Directory Layout (with UIMA additions)<br />http://maven.apache.org/guides/introduction/introduction-to-t...
Advanced Topics(not covered here)<br />Archetypes are a way of building a template for maven templates (poms).<br />Maven ...
Caution<br />The learning curve can be steep.<br />Be patient: you’ll write less SCM code.<br />Follow Convention.<br />Do...
Thank You<br />Hunter Lab Software Engineers for keeping the bar high:<br />Bill Baumgartner<br />Yuriy Malenkiy<br />ISI’...
Upcoming SlideShare
Loading in …5
×

Maven

2,294 views

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,294
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
85
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Maven

  1. 1. Basic Maven<br />Chris Roeder<br />Kevin Livingston<br />April 2011<br />5/5/11<br />1<br />Chris Roeder, Kevin Livingston<br />
  2. 2. Maven Big Picture:<br />Builds: Maven will compile your code and assemble jars. (similar to ant’s function)<br />Dependency Management: Maven will fetch other jars your project uses and assemble a CLASSPATH for the compiler. (similar to ivy’s function)<br />Integrate with Eclipse: by way of plugins like m2.<br />The goal of this talk is to get you started, not to make an expert of you. <br />5/5/11<br />Chris Roeder, Kevin Livingston<br />2<br />
  3. 3. Contents<br />Introduction<br />Dependency Manangement<br />Builds and pom files<br />Modules<br />Repositories<br />Releases and Snapshots<br />Mojo: Plugins, Goals, Phases<br />Runtime<br />Review, Advanced Topics, Caution<br />Thanks<br />5/5/11<br />Chris Roeder, Kevin Livingston<br />3<br />
  4. 4. Introduction 1<br />Dependency Manager:Given a description of dependent jars, Maven will fetch them for you<br />Build Tool: Convention over ConfigurationIf your project follows a standard layout, things just work (with no ant files)<br />Running: Maven will assemble as classpath for you at runtime. Consider what this means for deployment.<br />5/5/11<br />4<br />Chris Roeder, Kevin Livingston<br />
  5. 5. Introduction 2<br />Maven is organized around projects that produce a single jar.(or sets of projects that each produce a jar)<br />Each project has a pom.xml file, a “pom”, that configures it.<br />The pom describes the project and its dependencies. Each with 3 “coordinates”:<br />Group ID (edu.ucdenver.ccp)<br />Artifact ID (common)<br />Version (1.0)<br />5/5/11<br />5<br />Chris Roeder, Kevin Livingston<br />
  6. 6. Introduction: Convention over Configuration<br />A big Maven philosophy is that life is easier with clear conventions:<br />You know what to expect<br />The tool knows what to expect, so…<br />…it can run with little configuration.<br />Made popular by Ruby on Rails, and others.<br />Conventions (defaults) are declared in a (mostly) invisible “super pom”<br />5/5/11<br />6<br />Chris Roeder, Kevin Livingston<br />
  7. 7. Dependency Management<br />Maven is declarative (not imperative):<br />Specify the jars used, where they come from, and what release, and Maven will manage the files without explicit coding.<br />Maven manages the files.<br />You don’t need a “lib” directory<br />…even in eclipse. No modifying the project’s classpath.<br />5/5/11<br />7<br />Chris Roeder, Kevin Livingston<br />
  8. 8. Dependency Management: Adding a new jar <br />Google “maven junit” to find the dependency for junit.<br />Add this to your pom.xml (maven file): <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version></dependency><br />Even if the jar filename has no version, Maven keeps track.<br />Pom search engines include: http://mvnrepository.com/<br />5/5/11<br />8<br />Chris Roeder, Kevin Livingston<br />
  9. 9. Dependency Management:Finding the Jar<br />Most jars are in “The” repository, a public central repository for community use.<br />Sometimes the are not, so tell Maven about other repositories:<br /><repository><br /> <name>Clojure main release repository</name><id>clojure-releases</id><br /> <url>http://build.clojure.org/releases</url><br /></repository><br />You can usually find this in the same place as the dependency you Googled.<br />5/5/11<br />9<br />Chris Roeder, Kevin Livingston<br />
  10. 10. 5/5/11<br />Chris Roeder, Kevin Livingston<br />10<br />
  11. 11. 5/5/11<br />Chris Roeder, Kevin Livingston<br />11<br />
  12. 12. Build Tool<br />By default, Maven:<br /> knows where to find the code and resources. <br />You told it where and what the dependent jars are.<br />Convention tells it where the new jar should go and what it should be named.<br />Runs the “install” goal, making a jar available to more maven process. (more about goals coming)<br />5/5/11<br />12<br />Chris Roeder, Kevin Livingston<br />
  13. 13. Example POM<br /><?xml version="1.0" encoding="UTF-8"?><br /><project xmlns="http://maven.apache.org/POM/4.0.0"<br />xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://maven.apache.org/POM/4.0.0<br /> http://maven.apache.org/maven-v4_0_0.xsd"><br /><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><modelVersion>4.0.0</modelVersion><groupId>edu.ucdenver.ccp</groupId><artifactId>common</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version> <name>${project.artifactId}</name> <description>ccp common code</description><br />5/5/11<br />13<br />Chris Roeder, Kevin Livingston<br />Project “Coordinates”<br />
  14. 14. Example POM (cont)<br /><project><br />…<dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency><br /> </dependencies></project><br />5/5/11<br />14<br />Chris Roeder, Kevin Livingston<br />
  15. 15. Example POM: Repository Stanza<br /><repositories> <repository><id>apache incudbaor</id><name>apache-incubator</name><url>http://people.apache.org/repo/m2-incubating-repository/</url> </repository> <repository><id>cleartk-googlecode</id> <name>ClearTK Google Code repository</name> <url>http://cleartk.googlecode.com/svn/repo</url> <snapshots><enabled>true</enabled> </snapshots> </repository></repositories><br />5/5/11<br />15<br />Chris Roeder, Kevin Livingston<br />
  16. 16. Build Tool: unconventional<br />If you must work in a directory structure that doesn’t follow the convention…<br />…just tell it where to find things:<br /> <build> <sourceDirectory>src</sourceDirectory> <resources> <resource> <directory>resources</directory> </resource> </resources></build><br />Maven rhetoric “highly discourages” this, but it doesn’t seem to be an issue.<br />5/5/11<br />16<br />Chris Roeder, Kevin Livingston<br />
  17. 17. Modules:Families of Projects<br />You can have a “super project” that binds a number of sub-projects together. <br />A directory and a pom. The directory can sit besides the others. The pom goes in it.<?xml version="1.0" encoding="UTF-8"?><project> <modelVersion>4.0.0</modelVersion> <groupId>SciKnowMine</groupId> <artifactId>SciKnowMine</artifactId> <version>0.0.2-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>../SciKnowMineCore</module> <module>../SciKnowMineCitationStore</module> <module>../SciKnowMineTriage</module> <module>../SciKnowMinePreProcessing</module> </modules></project><br />Allows you to run them all from this one directory.<br />5/5/11<br />17<br />Chris Roeder, Kevin Livingston<br />
  18. 18. Parent Pom<br />A parent pom may be declared for each module that specifies common resources, dependencies, properties, etc.<br />The parent looks normal.<br />Children declare it as a parent:<br /> <parent> <groupId>edu.ucdenver.ccp</groupId> <artifactId>kr</artifactId> <version>1.0-SNAPSHOT</version></parent><br />Can be combined with main module from previous slide.<br />5/5/11<br />Chris Roeder, Kevin Livingston<br />18<br />
  19. 19. Properties<br />Not called “variable” because they aren’t<br />Declare them:<br /> <properties><br /> <clojure.version>1.2.0</clojure.version> <ver.jena>2.6.3</ver.jena> <ver.arq>2.8.5</ver.arq> <ver.sesame>2.3.2</ver.sesame></properties><br />Use them:<br /><dependency> <groupId>org.clojure</groupId> <artifactId>clojure</artifactId> <version>${clojure.version}</version></dependency><br />5/5/11<br />Chris Roeder, Kevin Livingston<br />19<br />
  20. 20. Repositories<br />Maven uses three levels of repositories:<br />Reactor: not really a repository. It’s part of a particular run.<br />Install: your personal repository: ~/.m2/repository<br />Deploy: something like a web server of jars<br />They are used sequentially: if an artifact (jar) is not available in the Reactor, it looks in your ~/.m2. If not there, it searches for deployments in repositories. <br />Really, there are two kinds of Deployment repositories:<br />You company’s local, private one, that caches.<br />Globally available repositories where things are publicly available.<br />The local repository also hosts “in-house” code.<br />5/5/11<br />20<br />Chris Roeder, Kevin Livingston<br />
  21. 21. Artifactory<br />Artifactory is the Maven repository implementation used here:<br />http://amc-bakeoff.ucdenver.pvt:8081<br />It’s used to cache artifacts (jars) from other repositories to reduce network load and load on those community repositories.<br />It also hosts locally developed artifacts.<br />5/5/11<br />21<br />Chris Roeder, Kevin Livingston<br />
  22. 22. Snapshots<br />Maven doesn’t deal strictly with perfectly prepared releases.<br />Snapshots are a way of sharing a work-in-progress.<br />If you specify a dependency as a snapshot, you get the latest and greatest: the most recently published.<br />The Maven repositories deal with keeping track of them and which is the most recent.<br />You can fix on a particular snapshot by referring to it using it’s timestamp.<br />5/5/11<br />22<br />Chris Roeder, Kevin Livingston<br />
  23. 23. Snapshots 2<br />A snapshot’s version says “SNAPSHOT”:<br /> <version>0.0.2-SNAPSHOT</version><br />A repository has to be marked as one that holds snapshots:<br /><repository> <id>clojure-snapshots</id> <url>http://build.clojure.org/snapshots</url> <snapshots><enabled>true</enabled> </snapshots> </repository><br />Dependencies too:<br /><dependency> <groupId>swank-clojure</groupId> <artifactId>swank-clojure</artifactId><version>1.3.0-SNAPSHOT</version></dependency><br />5/5/11<br />Chris Roeder, Kevin Livingston<br />23<br />
  24. 24. Plugins<br />Maven uses plugins to add functionality.<br />They are retreived like other artifacts: from repositories.<br />They are configured in stanzas called “plugin”<br />Their commands start with the plugin name:<br />Exec:java, for example is the exec plugin’s java goal<br />Plugin goals are called “Mojo” in Maven<br />5/5/11<br />24<br />Chris Roeder, Kevin Livingston<br />
  25. 25. Example Plugin Stanza<br /><plugin><groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration><source>1.6</source><target>1.6</target> </configuration></plugin><br />5/5/11<br />25<br />Chris Roeder, Kevin Livingston<br />
  26. 26. Mojo:Maven CommandsGoals<br />Maven is implemented in a number of plugins.<br />Each plugin has one or more goals (commands)<br />giving your install some kind of “mojo”<br />Goals are executed by naming them and their plugin:<br /> <plugin>:<goal><br />mvnarchetype:generate<br />Maven groups goals can be associated with “lifecycle phases”, and run simply by naming a phase<br />mvninstall<br />5/5/11<br />26<br />Chris Roeder, Kevin Livingston<br />
  27. 27. Maven Phases<br />A phase can have zero or more goals associated with it.<br />A lifecycle is a series of phases leading up to a package type.<br />Phases are ordered, and require successful completion of prior phases.<br />The default lifecycle is for jars, and consists of phases that run the following goals:<br />5/5/11<br />Chris Roeder, Kevin Livingston<br />27<br />
  28. 28. Runtime Tool<br />A plugin, exec, allows you to call java from Maven so you can use the classpath assembly features to run.<br />Consider what possibilities this creates:<br />Deliver a running project with just one file: the pom.<br />5/5/11<br />28<br />Chris Roeder, Kevin Livingston<br />
  29. 29. Runtime 2: The getResourceAsStream Soap Box<br />Not all projects remain naïve of the file system.<br />Some expect to be able to open files using either absolute or relative filepaths and the File class. <br />These files can’t be buried in a jar file.<br />Use maven-assembly-pack to build a deployment assemply.<br />Unpack with maven-dependency-unpack.<br />5/5/11<br />Chris Roeder, Kevin Livingston<br />29<br />
  30. 30. Review: Maven Vocabulary<br />Project<br />Pom – a file that tells maven about a particular project.<br />Artifact – a jar, war, pom or other thing required by a project<br />Dependency – something your project needs from another project.<br />Coordinates – the comination of attributes that identify an artifact<br />Repository – a place for artifacts.<br />Lifecycle – a series of steps to produce an artifact.<br />Phase – one of the steps in a Lifecycle.<br />Plugin (Mojo) – added capability<br />Install – putting an artifact into your local ~/.m2/repository<br />Deploy – putting an artifact into a repository<br />Reactor – a process that analyzes and resolves dependencies and runs the build. It is the innermost (if temporary) container of artifacts.<br />5/5/11<br />Chris Roeder, Kevin Livingston<br />30<br />
  31. 31. Review: Standard Directory Layout (with UIMA additions)<br />http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html<br />src<br />main<br />Java<br />edu.isi.bmkeg…<br />Resources<br />desc/ae<br />desc/cpe<br />desc/cr<br />typeSystem<br />models<br />test<br />java<br />resources<br />target<br />Product-0.1-SNAPSHOT.jar (created by build run)<br />5/5/11<br />Chris Roeder, Kevin Livingston<br />31<br />
  32. 32. Advanced Topics(not covered here)<br />Archetypes are a way of building a template for maven templates (poms).<br />Maven does a lot in terms of Dependency Management: What if you have 2 jars that each depend on a third jar, but different versions? Maven picks the most recent, but this assumes backward compatibility.<br />Scope: items within maven can be scoped to a phase, so for example, jUnit doesn’t have to ship with the production jar.<br />SCM integration: How does this integrate with Subversion?<br />Eclipse Integration: m2 eclipse plugin<br />Site Utilities: a project info web site can be built that includes author and test info. among others.<br />5/5/11<br />Chris Roeder, Kevin Livingston<br />32<br />
  33. 33. Caution<br />The learning curve can be steep.<br />Be patient: you’ll write less SCM code.<br />Follow Convention.<br />Don’t be afraid to violate Convention.<br />Get a second opinion.<br />Read the errors bottom-up.<br />Run “mvn clean” and “rm ~/.m2/repository” when things get real weird.<br />http://www.sonatype.com/books/mvnex-book/reference/public-book.html<br />5/5/11<br />33<br />Chris Roeder, Kevin Livingston<br />
  34. 34. Thank You<br />Hunter Lab Software Engineers for keeping the bar high:<br />Bill Baumgartner<br />Yuriy Malenkiy<br />ISI’s SciKnowMine project for using tools that practically require Maven:<br />Cartic Ramakrishnan<br />Gully Burns<br />5/5/11<br />34<br />Chris Roeder, Kevin Livingston<br />

×