Apache Maven: Best Practices - Brett Porter

1 downloads 36 Views 3MB Size Report
3. Apache Maven developer since 2003. Started the Archiva project. Co-founder of. DevZuz. Co-author of. Better Builds with Maven. Who am I?
Apache Maven: Best Practices Brett Porter - [email protected] http://www.devzuz.org/blogs/bporter

Maven without the PAIN

Sometimes unpleasant You know it’s for your own good! Can avoid or alleviate the problems 2

Who am I? Apache Maven developer since 2003

Started the Archiva project

Co-author of Better Builds with Maven

Co-founder of DevZuz 3

Getting Ready for Maven 4

Why are you using Maven? Consider this from the beginning Right tool for the job Ensure you reap the benefits

5

Planning Ahead Preparing Your Environment 6

Development Environment Maven is best used in the bigger picture Plan for the infrastructure you will use ... ... ...

... ...

7

Automated Build Servers Maven is intended to be automated More than continuous integration

8

Repository Management Centralise storage of artifacts Store the artifacts you build Store third-party artifacts you consume

9

Repository Servers Can start with a simple storage Servers can help manage the artifacts

10

Using Repositories apache-snapshots Apache Snapshots Repository http://people.apache.org/repo/m2-snapshot-repository false java-net-m1 Java.net Repository http://download.java.net/maven/1/ legacy false

11

Using Repositories

X

apache-releases Apache Releases Repository apache-snapshots http://people.apache.org/repo/m2-ibiblio-rsyncApache Snapshots Repository repository http://people.apache.org/repo/m2-snapshot-repository codehaus-snapshots Codehaus Snapshots Repository false false http://snapshots.repository.codehaus.org/ codehaus-releases false Codehaus Releases Repository http://repository.codehaus.org/ java-net-m1 Java.net Repository false java-net-m2 http://download.java.net/maven/1/ Java.net Repository legacy http://download.java.net/maven/2/ false false 12

Using Repositories Minimise the number of repositories Only in the POM if you redistribute Use repository manager to centralise

13

Locking Down archiva-snapshots-apache http://reposerver/archiva/repository/snapshots/ apache.snapshots archiva-snapshots-codehaus http://reposerver/archiva/repository/snapshots/ codehaus.snapshots ... archiva-default http://reposerver/archiva/repository/internal/ *

14

Settings and Installation Three levels of configuration project (pom.xml) user (~/.m2/settings.xml) installation (/conf/settings.xml)

15

Keeping it Simple Creating New Projects 16

Archetypes Create new projects quickly Standard project layouts Include organisational POM Facilitates consistency

17

Writing the POM Write the build like you write code Utilise conventions Use multiple modules

18

Inheritance Multi-module inheritance org.apache.maven maven 2.1-SNAPSHOT maven-core

Organisational POM hierarchy org.apache apache 3 org.apache.maven maven 2.1-SNAPSHOT 19

Dependencies Specify only what you need Specify scope Use dependencyManagement to: coerce Maven to use a particular version enforce consistency within a project

20

Build Pipeline Depends on your team Use profiles for controlling complexity Applies to testing as well Key is to keep the build fast

21

Scripting Maven is declarative by design Integrate scripting if necessary Consider writing plugins

22

The D Word Documenting as You Go 23

Documentation Document the build Developer documentation separate from product documentation keep it together with reports

24

Sites and Reports A whole other topic of best practices! But apply the same principles set up what you’ll actually use set up enforcement checks, not just reports

25

Portability Playing with Others 26

The Goal When a new developer builds the project it works first go ... and it keeps working

27

maven.test.skip is evil

28

Hard Coding Don’t hard code paths Don’t hard code databases Don’t hard code properties Don’t do it in the tests either

29

Profiles Very useful - but don’t abuse them Document them all Avoid depending on the environment

30

Portable Artifacts

X



value="org.hsqldb.jdbcDriver" value="jdbc:hsqldb:database"

31

Portable Artifacts Artifacts in repository must be unique Either by classifier, or being portable Recommend externalising configuration database target environment properties 32

Resource Filtering

${ } Use with great care!

Centralisation, not substitution

✓ google.analytics.code=UA-1234567-1 X database.password=reallysecretpword

Useful for once-off alterations

Consider externalising configuration

33

Shared Resources Don’t duplicate resources across projects ZIP and put in the repository Use the dependency plugin to retrieve

34

Reproducibility Ensuring Reproducible Builds 35

Reproducibility Important for releases More important for source releases Build must be isolated from change Portability is a pre-requisite

36

The Enforcer org.apache.maven.plugins maven-enforcer-plugin 1.0-alpha-4 enforce true true ... 37

The Enforcer org.apache.maven.plugins maven-enforcer-plugin 1.0-alpha-4 enforce true true ...

true true

38

The Enforcer Help ensure build will be reproducible Based on rules force specific plugin versions ban snapshots global exclusions force Maven/Java/OS version can write your own 39

Releasing Release Early, Often and Right 40

Releases Set the project version to a -SNAPSHOT Make them early and often Use Maven tools to automate

41

Questions? Brett Porter - [email protected] Better Builds with Maven, blog at http://www.devzuz.org/

Suggest Documents