Maven

34
Basic Maven Chris Roeder Kevin Livingston April 2011 07/03/2022 1 Chris Roeder, Kevin Livingston

Transcript of Maven

Page 1: Maven

04/12/2023 Chris Roeder, Kevin Livingston 1

Basic Maven

Chris RoederKevin Livingston

April 2011

Page 2: Maven

04/12/2023 Chris Roeder, Kevin Livingston 2

Maven Big Picture:

• Builds: Maven will compile your code and assemble jars. (similar to ant’s function)

• Dependency Management: Maven will fetch other jars your project uses and assemble a CLASSPATH for the compiler. (similar to ivy’s function)

• Integrate with Eclipse: by way of plugins like m2.

• The goal of this talk is to get you started, not to make an expert of you.

Page 3: Maven

04/12/2023 Chris Roeder, Kevin Livingston 3

Contents

• Introduction• Dependency Manangement• Builds and pom files• Modules• Repositories• Releases and Snapshots• Mojo: Plugins, Goals, Phases• Runtime• Review, Advanced Topics, Caution• Thanks

Page 4: Maven

04/12/2023 Chris Roeder, Kevin Livingston 4

Introduction 1

• Dependency Manager:Given a description of dependent jars, Maven will fetch them for you

• Build Tool: Convention over ConfigurationIf your project follows a standard layout, things just work (with no ant files)

• Running: Maven will assemble as classpath for you at runtime. Consider what this means for deployment.

Page 5: Maven

04/12/2023 Chris Roeder, Kevin Livingston 5

Introduction 2

• Maven is organized around projects that produce a single jar.(or sets of projects that each produce a jar)

• Each project has a pom.xml file, a “pom”, that configures it.

• The pom describes the project and its dependencies. Each with 3 “coordinates”:– Group ID (edu.ucdenver.ccp)– Artifact ID (common)– Version (1.0)

Page 6: Maven

04/12/2023 Chris Roeder, Kevin Livingston 6

Introduction: Convention over Configuration

• A big Maven philosophy is that life is easier with clear conventions:– You know what to expect– The tool knows what to expect, so…– …it can run with little configuration.

• Made popular by Ruby on Rails, and others.• Conventions (defaults) are declared in a

(mostly) invisible “super pom”

Page 7: Maven

04/12/2023 Chris Roeder, Kevin Livingston 7

Dependency Management

• Maven is declarative (not imperative):– Specify the jars used, where they come from, and

what release, and Maven will manage the files without explicit coding.

• Maven manages the files.– You don’t need a “lib” directory– …even in eclipse. No modifying the project’s

classpath.

Page 8: Maven

04/12/2023 Chris Roeder, Kevin Livingston 8

Dependency Management: Adding a new jar

• Google “maven junit” to find the dependency for junit.

• Add this to your pom.xml (maven file): <dependency>

<groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.2</version>

</dependency>

• Even if the jar filename has no version, Maven keeps track.

• Pom search engines include: http://mvnrepository.com/

Page 9: Maven

04/12/2023 Chris Roeder, Kevin Livingston 9

Dependency Management:Finding the Jar

• Most jars are in “The” repository, a public central repository for community use.

• Sometimes the are not, so tell Maven about other repositories:

<repository><name>Clojure main release repository</name><id>clojure-releases</id>

<url>http://build.clojure.org/releases</url></repository>

• You can usually find this in the same place as the dependency you Googled.

Page 10: Maven

04/12/2023 Chris Roeder, Kevin Livingston 10

Page 11: Maven

04/12/2023 Chris Roeder, Kevin Livingston 11

Page 12: Maven

04/12/2023 Chris Roeder, Kevin Livingston 12

Build Tool

• By default, Maven:– knows where to find the code and resources. – You told it where and what the dependent jars

are.– Convention tells it where the new jar should go

and what it should be named.– Runs the “install” goal, making a jar available to

more maven process. (more about goals coming)

Page 13: Maven

04/12/2023 Chris Roeder, Kevin Livingston 13

Example POM<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<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>

Project “Coordinates”

Page 14: Maven

04/12/2023 Chris Roeder, Kevin Livingston 14

Example POM (cont)<project>…

<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>

</dependencies></project>

Page 15: Maven

04/12/2023 Chris Roeder, Kevin Livingston 15

Example POM: Repository Stanza

<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>

Page 16: Maven

04/12/2023 Chris Roeder, Kevin Livingston 16

Build Tool: unconventional• If you must work in a directory structure that doesn’t follow the

convention…• …just tell it where to find things:

<build><sourceDirectory>src</sourceDirectory><resources>

<resource><directory>resources</directory>

</resource></resources>

</build>

• Maven rhetoric “highly discourages” this, but it doesn’t seem to be an issue.

Page 17: Maven

04/12/2023 Chris Roeder, Kevin Livingston 17

Modules:Families of Projects

• You can have a “super project” that binds a number of sub-projects together. • 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>

• Allows you to run them all from this one directory.

Page 18: Maven

04/12/2023 Chris Roeder, Kevin Livingston 18

Parent Pom

• A parent pom may be declared for each module that specifies common resources, dependencies, properties, etc.

• The parent looks normal.• Children declare it as a parent:

<parent><groupId>edu.ucdenver.ccp</groupId><artifactId>kr</artifactId><version>1.0-SNAPSHOT</version>

</parent>

• Can be combined with main module from previous slide.

Page 19: Maven

04/12/2023 Chris Roeder, Kevin Livingston 19

Properties• Not called “variable” because they aren’t• Declare them:

<properties><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>

• Use them:

<dependency><groupId>org.clojure</groupId><artifactId>clojure</artifactId><version>${clojure.version}</version></dependency>

Page 20: Maven

04/12/2023 Chris Roeder, Kevin Livingston 20

Repositories

• Maven uses three levels of repositories:– Reactor: not really a repository. It’s part of a particular run.– Install: your personal repository: ~/.m2/repository– Deploy: something like a web server of jars

• 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.

• Really, there are two kinds of Deployment repositories:– You company’s local, private one, that caches.– Globally available repositories where things are publicly available.

• The local repository also hosts “in-house” code.

Page 21: Maven

04/12/2023 Chris Roeder, Kevin Livingston 21

Artifactory

• Artifactory is the Maven repository implementation used here:– http://amc-bakeoff.ucdenver.pvt:8081

• It’s used to cache artifacts (jars) from other repositories to reduce network load and load on those community repositories.

• It also hosts locally developed artifacts.

Page 22: Maven

04/12/2023 Chris Roeder, Kevin Livingston 22

Snapshots

• Maven doesn’t deal strictly with perfectly prepared releases.

• Snapshots are a way of sharing a work-in-progress.• If you specify a dependency as a snapshot, you get

the latest and greatest: the most recently published.• The Maven repositories deal with keeping track of

them and which is the most recent.• You can fix on a particular snapshot by referring to it

using it’s timestamp.

Page 23: Maven

04/12/2023 Chris Roeder, Kevin Livingston 23

Snapshots 2• A snapshot’s version says “SNAPSHOT”:

<version>0.0.2-SNAPSHOT</version>

• A repository has to be marked as one that holds snapshots:<repository>

<id>clojure-snapshots</id><url>http://build.clojure.org/snapshots</url><snapshots><enabled>true</enabled></snapshots>

</repository>

• Dependencies too:

<dependency><groupId>swank-clojure</groupId><artifactId>swank-clojure</artifactId><version>1.3.0-SNAPSHOT</version>

</dependency>

Page 24: Maven

04/12/2023 Chris Roeder, Kevin Livingston 24

Plugins

• Maven uses plugins to add functionality.• They are retreived like other artifacts: from

repositories.• They are configured in stanzas called “plugin”• Their commands start with the plugin name:– Exec:java, for example is the exec plugin’s java goal

• Plugin goals are called “Mojo” in Maven

Page 25: Maven

04/12/2023 Chris Roeder, Kevin Livingston 25

Example Plugin Stanza<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>

Page 26: Maven

04/12/2023 Chris Roeder, Kevin Livingston 26

Mojo:Maven Commands Goals

• Maven is implemented in a number of plugins.• Each plugin has one or more goals (commands)– giving your install some kind of “mojo”

• Goals are executed by naming them and their plugin:<plugin>:<goal>mvn archetype:generate

• Maven groups goals can be associated with “lifecycle phases”, and run simply by naming a phase

mvn install

Page 27: Maven

04/12/2023 Chris Roeder, Kevin Livingston 27

Maven Phases

• A phase can have zero or more goals associated with it.• A lifecycle is a series of phases

leading up to a package type.• Phases are ordered, and require

successful completion of prior phases.• The default lifecycle is for jars,

and consists of phases that run the following goals:

Page 28: Maven

04/12/2023 Chris Roeder, Kevin Livingston 28

Runtime Tool

• A plugin, exec, allows you to call java from Maven so you can use the classpath assembly features to run.

• Consider what possibilities this creates:– Deliver a running project with just one file:

the pom.

Page 29: Maven

04/12/2023 Chris Roeder, Kevin Livingston 29

Runtime 2: The getResourceAsStream Soap Box

• Not all projects remain naïve of the file system.• Some expect to be able to open files using

either absolute or relative filepaths and the File class. – These files can’t be buried in a jar file.

• Use maven-assembly-pack to build a deployment assemply.

• Unpack with maven-dependency-unpack.

Page 30: Maven

04/12/2023 Chris Roeder, Kevin Livingston 30

Review: Maven Vocabulary• Project• Pom – a file that tells maven about a particular project.• Artifact – a jar, war, pom or other thing required by a project

• Dependency – something your project needs from another project.• Coordinates – the comination of attributes that identify an artifact

• Repository – a place for artifacts.• Lifecycle – a series of steps to produce an artifact.• Phase – one of the steps in a Lifecycle.• Plugin (Mojo) – added capability• Install – putting an artifact into your local ~/.m2/repository

• Deploy – putting an artifact into a repository

• Reactor – a process that analyzes and resolves dependencies and runs the build. It is the innermost (if temporary) container of artifacts.

Page 31: Maven

04/12/2023 Chris Roeder, Kevin Livingston 31

Review: Standard Directory Layout (with UIMA additions)

• http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

• src– main

• Java– edu.isi.bmkeg…

• Resources– desc/ae– desc/cpe– desc/cr– typeSystem– models

– test• java• resources

• target– Product-0.1-SNAPSHOT.jar (created by build run)

Page 32: Maven

04/12/2023 Chris Roeder, Kevin Livingston 32

Advanced Topics(not covered here)

• Archetypes are a way of building a template for maven templates (poms).

• 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.

• Scope: items within maven can be scoped to a phase, so for example, jUnit doesn’t have to ship with the production jar.

• SCM integration: How does this integrate with Subversion?• Eclipse Integration: m2 eclipse plugin• Site Utilities: a project info web site can be built that includes

author and test info. among others.

Page 33: Maven

04/12/2023 Chris Roeder, Kevin Livingston 33

Caution

• The learning curve can be steep.• Be patient: you’ll write less SCM code.• Follow Convention.• Don’t be afraid to violate Convention.• Get a second opinion.• Read the errors bottom-up.• Run “mvn clean” and “rm ~/.m2/repository” when

things get real weird.• http://www.sonatype.com/books/mvnex-book/

reference/public-book.html

Page 34: Maven

04/12/2023 Chris Roeder, Kevin Livingston 34

Thank You

• Hunter Lab Software Engineers for keeping the bar high:– Bill Baumgartner– Yuriy Malenkiy

• ISI’s SciKnowMine project for using tools that practically require Maven:– Cartic Ramakrishnan– Gully Burns