Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the...

42
Apache Maven: Best Practices Brett Porter - [email protected] http://www.devzuz.org/blogs/bporter

Transcript of Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the...

Page 2: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Maven without the PAIN

Sometimes unpleasant

You know it’s for your own good!

Can avoid or alleviate the problems

2

Page 3: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

3

Apache Mavendeveloper since 2003

Started the Archivaproject

Co-founder ofDevZuz

Co-author ofBetter Builds with Maven

Who am I?

Page 4: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Getting Ready for Maven4

Page 5: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Why are you using Maven?Consider this from the beginning

Right tool for the job

Ensure you reap the benefits

5

Page 6: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Preparing Your Environment

Planning Ahead

6

Page 7: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Development Environment

7

Maven is best used in the bigger picture

Plan for the infrastructure you will use

<scm> <connection>... <developerConnection>... <url>...</scm>

<issueManagement> <url>... <system>...</issueManagement>

Page 8: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Automated Build ServersMaven is intended to be automated

More than continuous integration

8

Page 9: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Repository ManagementCentralise storage of artifacts

Store the artifacts you buildStore third-party artifacts you consume

9

Page 10: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Repository ServersCan start with a simple storage

Servers can help manage the artifacts

10

Page 11: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Using Repositories

11

<repository> <id>apache-snapshots</id> <name>Apache Snapshots Repository</name> <url>http://people.apache.org/repo/m2-snapshot-repository</url> <releases> <enabled>false</enabled> </releases></repository>

<repository> <id>java-net-m1</id> <name>Java.net Repository</name> <url>http://download.java.net/maven/1/</url> <layout>legacy</layout> <snapshots> <enabled>false</enabled> </snapshots></repository>

Page 12: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

<repository> <id>apache-snapshots</id> <name>Apache Snapshots Repository</name> <url>http://people.apache.org/repo/m2-snapshot-repository</url> <releases> <enabled>false</enabled> </releases></repository>

<repository> <id>java-net-m1</id> <name>Java.net Repository</name> <url>http://download.java.net/maven/1/</url> <layout>legacy</layout> <snapshots> <enabled>false</enabled> </snapshots></repository>

<repository> <id>codehaus-snapshots</id> <name>Codehaus Snapshots Repository</name> <url>http://snapshots.repository.codehaus.org/</url> <releases> <enabled>false</enabled> </releases></repository>

<repository> <id>apache-releases</id> <name>Apache Releases Repository</name> <url>http://people.apache.org/repo/m2-ibiblio-rsync-repository</url> <snapshots> <enabled>false</enabled> </snapshots></repository>

<repository> <id>codehaus-releases</id> <name>Codehaus Releases Repository</name> <url>http://repository.codehaus.org/</url> <snapshots> <enabled>false</enabled> </snapshots></repository>

Using Repositories

12

<repository> <id>java-net-m2</id> <name>Java.net Repository</name> <url>http://download.java.net/maven/2/</url> <snapshots> <enabled>false</enabled> </snapshots></repository>

X

Page 13: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Using RepositoriesMinimise the number of repositories

Only in the POM if you redistribute

Use repository manager to centralise

13

Page 14: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Locking Down

14

<mirrors> <mirror> <id>archiva-snapshots-apache</id> <url>http://reposerver/archiva/repository/snapshots/</url> <mirrorOf>apache.snapshots</mirrorOf> </mirror> <mirror> <id>archiva-snapshots-codehaus</id> <url>http://reposerver/archiva/repository/snapshots/</url> <mirrorOf>codehaus.snapshots</mirrorOf> </mirror> ... <mirror> <id>archiva-default</id> <url>http://reposerver/archiva/repository/internal/</url> <mirrorOf>*</mirrorOf> </mirror></mirrors>

Page 15: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Settings and InstallationThree levels of configuration

project (pom.xml)user (~/.m2/settings.xml)installation (<maven>/conf/settings.xml)

15

Page 16: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Creating New Projects

Keeping it Simple

16

Page 17: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

ArchetypesCreate new projects quickly

Standard project layouts

Include organisational POM

Facilitates consistency

17

Page 18: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Writing the POM

18

Write the build like you write code

Utilise conventions

Use multiple modules

Page 19: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

InheritanceMulti-module inheritance<parent> <groupId>org.apache.maven</groupId> <artifactId>maven</artifactId> <version>2.1-SNAPSHOT</version></parent><artifactId>maven-core</artifactId>

Organisational POM hierarchy<parent> <groupId>org.apache</groupId> <artifactId>apache</artifactId> <version>3</version></parent><groupId>org.apache.maven</groupId><artifactId>maven</artifactId><version>2.1-SNAPSHOT</version>

19

Page 20: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

DependenciesSpecify only what you need

Specify scope

Use dependencyManagement to:coerce Maven to use a particular versionenforce consistency within a project

20

Page 21: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Build PipelineDepends on your team

Use profiles for controlling complexity

Applies to testing as well

Key is to keep the build fast

21

Page 22: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

22

ScriptingMaven is declarative by design

Integrate scripting if necessary

Consider writing plugins

Page 23: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Documenting as You Go

The D Word

23

Page 24: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

DocumentationDocument the build

Developer documentation

separate from product documentationkeep it together with reports

24

Page 25: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Sites and ReportsA whole other topic of best practices!

But apply the same principles

set up what you’ll actually useset up enforcement checks, not just reports

25

Page 26: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Playing with Others

Portability

26

Page 27: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

The Goal

27

When a new developer builds the project

it works first go... and it keeps working

Page 28: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

28

maven.test.skipis evil

Page 29: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Hard CodingDon’t hard code paths

Don’t hard code databases

Don’t hard code properties

Don’t do it in the tests either

29

Page 30: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

ProfilesVery useful - but don’t abuse them

Document them all

Avoid depending on the environment

30

Page 31: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Portable Artifacts

31

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:database" /> <property name="username" value="sa" /> <property name="password" value="" /></bean> Xvalue="org.hsqldb.jdbcDriver"

value="jdbc:hsqldb:database"

Page 32: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Portable ArtifactsArtifacts in repository must be unique

Either by classifier, or being portable

Recommend externalising configurationdatabasetarget environmentproperties

32

Page 33: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

}${Resource Filtering

Use with great care!

Centralisation, not substitution✓ google.analytics.code=UA-1234567-1X database.password=reallysecretpword

Useful for once-off alterations

Consider externalising configuration

33

Page 34: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Shared ResourcesDon’t duplicate resources across projects

ZIP and put in the repository

Use the dependency plugin to retrieve

34

Page 35: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Ensuring Reproducible Builds

Reproducibility

35

Page 36: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Reproducibility

36

Important for releases

More important for source releases

Build must be isolated from change

Portability is a pre-requisite

Page 37: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

The Enforcer

37

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0-alpha-4</version> <executions> <execution> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requirePluginVersions> <banLatest>true</banLatest> <banRelease>true</banRelease> </requirePluginVersions> </rules> </configuration> ...

Page 38: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

The Enforcer

38

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0-alpha-4</version> <executions> <execution> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requirePluginVersions> <banLatest>true</banLatest> <banRelease>true</banRelease> </requirePluginVersions> </rules> </configuration> ...

<rules> <requirePluginVersions> <banLatest>true</banLatest> <banRelease>true</banRelease> </requirePluginVersions></rules>

Page 39: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

The EnforcerHelp ensure build will be reproducible

Based on rulesforce specific plugin versionsban snapshotsglobal exclusionsforce Maven/Java/OS versioncan write your own

39

Page 40: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Release Early, Often and Right

Releasing

40

Page 41: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

ReleasesSet the project version to a -SNAPSHOT

Make them early and often

Use Maven tools to automate

41

Page 42: Apache Maven: Best Practices - WordPress.com · 3 Apache Maven developer since 2003 Started the Archiva project Co-founder of DevZuz Co-author of Better Builds with Maven Who am I?

Brett Porter - [email protected]

Better Builds with Maven, blog athttp://www.devzuz.org/

Questions?