26
Maven OOP 2018

Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

  • Upload
    others

  • View
    28

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

MavenOOP 2018

Page 2: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

What is Maven Open-source project management framework

Simplifies building, testing and packaging

Standard way of building projects

Sharing generated artifacts across projects

Version 1.0 released in 2004

Page 3: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Features Standardized directory structure

Declarative dependency management

Plug-ins

Uniform build abstraction

Tools support

Archetypes

Page 4: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Standardized directory structure

project root folder - pom.xml - src - main - java - test - java - target

Page 5: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Dependency identification

groupId● Organization/group identifier

artifactId● Artifact identifier● Unique among the projects using the same groupId

version

<dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.2</version></dependency>

Page 6: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Dependency identification

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>LATEST</version></dependency>

<dependency> <groupId>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>1.5.5</version> <scope>test</scope></dependency>

Page 7: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Statistics

Page 8: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Create new maven project

Page 9: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Set artifactId, groupId & version

Page 10: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Set project name & location

Page 11: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Confirm

Page 12: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Open pom.xml Enable auto-import (IDEA)

Page 13: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2
Page 14: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Add dependecy <dependencies> <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.2</version> </dependency></dependencies>

Page 15: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2
Page 16: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Create package (if missing)

In folder src/main/java

Create package<groupId>.<artifactId>

Page 17: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2
Page 18: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Create Main (if missing)

In folder src/main/java/<packagename>

Page 19: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2
Page 20: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Use imported dependency(static methods used for simplicity!)

private static CommandLineParser parser = new BasicParser();private static Options options = new Options();

public static void main(String[] args) { try { parseArgumentValue(args); } catch (ParseException e) { printParseException(e); }}

private static void parseArgumentValue(String[] args) throws ParseException { CommandLine commandLine = parseCommands(args); System.out.println("a=" + commandLine.getOptionValue("a"));}

private static CommandLine parseCommands(String[] args) throws ParseException { options.addOption("a", true, "an argument"); return parser.parse(options, args);}

private static void printParseException(ParseException e) { System.out.println(e.getMessage()); System.out.println("Usage: java -jar -a <value>");}

Page 21: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2
Page 22: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Create run configuration

Set program arguments to:-a 10

or execute jar from command linejava -jar <name>.jar -a 10

Page 23: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2
Page 24: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Run

Page 25: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2
Page 26: Maven · Features Standardized directory structure Declarative dependency management Plug-ins Uniform build abstraction ... 1.2

Further reading Varanasi, B., Belida, S.: Introducing Maven. Apress (2014)

Sonatype Company: Maven: The Definitive Guide. O'Reilly Media, Inc. (2008)