maven best practices
TRANSCRIPT
-
8/11/2019 Maven Best Practices
1/42
Apache Maven:Best Practices
Brett Porter - [email protected]
http://www.devzuz.org/blogs/bporter
http://www.devzuz.org/blogs/bporterhttp://www.devzuz.org/blogs/bporterhttp://www.devzuz.org/blogs/bporterhttp://www.devzuz.org/blogs/bportermailto:[email protected]:[email protected] -
8/11/2019 Maven Best Practices
2/42
Maven without the PAIN
Sometimes unpleasant
You know its for your own good!
Can avoid or alleviate the problems
2
-
8/11/2019 Maven Best Practices
3/42
3
Apache Maven
developer since 2003
Started the Archiva
project
Co-founder of
DevZuz
Co-author of
Better Builds with Maven
Who am I?
-
8/11/2019 Maven Best Practices
4/42
Getting Ready for Maven
4
-
8/11/2019 Maven Best Practices
5/42
Why are you using Maven?
Consider this from the beginning
Right tool for the job
Ensure you reap the benefits
5
-
8/11/2019 Maven Best Practices
6/42
Preparing Your EnvironmentPlanning Ahead
6
-
8/11/2019 Maven Best Practices
7/42
Development Environment
7
Maven is best used in the bigger picture
Plan for the infrastructure you will use
...
...
...
...
...
-
8/11/2019 Maven Best Practices
8/42
Automated Build Servers
Maven is intended to be automated
More than continuous integration
8
-
8/11/2019 Maven Best Practices
9/42
Repository Management
Centralise storage of artifacts
Store the artifacts you build
Store third-party artifacts you consume
9
-
8/11/2019 Maven Best Practices
10/42
Repository Servers
Can start with a simple storage
Servers can help manage the artifacts
10
-
8/11/2019 Maven Best Practices
11/42
Using Repositories
11
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
-
8/11/2019 Maven Best Practices
12/42
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
codehaus-snapshots Codehaus Snapshots Repository
http://snapshots.repository.codehaus.org/
false
apache-releases Apache Releases Repository
http://people.apache.org/repo/m2-ibiblio-rsync-repository
false
codehaus-releases
Codehaus Releases Repository
http://repository.codehaus.org/
false
Using Repositories
12
java-net-m2
Java.net Repository http://download.java.net/maven/2/
false
X
-
8/11/2019 Maven Best Practices
13/42
Using Repositories
Minimise the number of repositories
Only in the POM if you redistribute
Use repository manager to centralise
13
-
8/11/2019 Maven Best Practices
14/42
Locking Down
14
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/ *
http://localhost:7777/archiva/repository/internal/http://localhost:7777/archiva/repository/internal/http://localhost:7777/archiva/repository/snapshots/http://localhost:7777/archiva/repository/snapshots/http://localhost:7777/archiva/repository/internal/http://localhost:7777/archiva/repository/internal/http://localhost:7777/archiva/repository/snapshots/http://localhost:7777/archiva/repository/snapshots/http://localhost:7777/archiva/repository/snapshots/http://localhost:7777/archiva/repository/snapshots/ -
8/11/2019 Maven Best Practices
15/42
Settings and Installation
Three levels of configuration
project (pom.xml)
user (~/.m2/settings.xml)installation (/conf/settings.xml)
15
-
8/11/2019 Maven Best Practices
16/42
Creating New ProjectsKeeping it Simple16
-
8/11/2019 Maven Best Practices
17/42
Archetypes
Create new projects quickly
Standard project layouts
Include organisational POM
Facilitates consistency
17
-
8/11/2019 Maven Best Practices
18/42
Writing the POM
18
Write the build like you write code
Utilise conventions
Use multiple modules
-
8/11/2019 Maven Best Practices
19/42
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
-
8/11/2019 Maven Best Practices
20/42
Dependencies
Specify only what you need
Specify scope
Use dependencyManagementto:
coerce Maven to use a particular version
enforce consistency within a project
20
-
8/11/2019 Maven Best Practices
21/42
Build Pipeline
Depends on your team
Use profiles for controlling complexity
Applies to testing as well
Key is to keep the build fast
21
-
8/11/2019 Maven Best Practices
22/42
22
Scripting
Maven is declarative by design
Integrate scripting if necessary
Consider writing plugins
-
8/11/2019 Maven Best Practices
23/42
Documenting as You GoThe D Word23
-
8/11/2019 Maven Best Practices
24/42
Documentation
Document the build
Developer documentation
separate from product documentation
keep it together with reports
24
-
8/11/2019 Maven Best Practices
25/42
Sites and Reports
A whole other topic of best practices!
But apply the same principles
set up what youll actually use
set up enforcement checks, not just reports
25
-
8/11/2019 Maven Best Practices
26/42
Playing with OthersPortability26
-
8/11/2019 Maven Best Practices
27/42
The Goal
27
When a new developer builds the project
it works first go
... and it keeps working
-
8/11/2019 Maven Best Practices
28/42
28
maven.test.skipis evil
-
8/11/2019 Maven Best Practices
29/42
Hard Coding
Dont hard code paths
Dont hard code databases
Dont hard code properties
Dont do it in the testseither
29
-
8/11/2019 Maven Best Practices
30/42
Profiles
Very useful - but dont abuse them
Document them all
Avoid depending on the environment
30
-
8/11/2019 Maven Best Practices
31/42
Portable Artifacts
31
Xvalue="org.hsqldb.jdbcDriver"value="jdbc:hsqldb:database"
-
8/11/2019 Maven Best Practices
32/42
Portable Artifacts
Artifacts in repository must be unique
Either by classifier, or being portable
Recommend externalising configuration
database
target environmentproperties
32
-
8/11/2019 Maven Best Practices
33/42
}${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
-
8/11/2019 Maven Best Practices
34/42
Shared Resources
Dont duplicate resources across projects
ZIP and put in the repository
Use the dependency plugin to retrieve
34
-
8/11/2019 Maven Best Practices
35/42
Ensuring Reproducible BuildsReproducibility35
-
8/11/2019 Maven Best Practices
36/42
Reproducibility
36
Important for releases
More important for source releases
Build must be isolated from change
Portability is a pre-requisite
-
8/11/2019 Maven Best Practices
37/42
The Enforcer
37
org.apache.maven.plugins maven-enforcer-plugin
1.0-alpha-4
enforce
true
true
...
-
8/11/2019 Maven Best Practices
38/42
The Enforcer
38
org.apache.maven.plugins maven-enforcer-plugin
1.0-alpha-4
enforce
true
true
...
true
true
-
8/11/2019 Maven Best Practices
39/42
The Enforcer
Help ensure build will be reproducible
Based on rules
force specific plugin versionsban snapshots
global exclusions
force Maven/Java/OS versioncan write your own
39
-
8/11/2019 Maven Best Practices
40/42
Release Early, Often and RightReleasing40
-
8/11/2019 Maven Best Practices
41/42
Releases
Set the project version to a -SNAPSHOT
Make them early and often
Use Maven tools to automate
41
-
8/11/2019 Maven Best Practices
42/42
Brett Porter - [email protected]
Better Builds with Maven, blog at
http://www.devzuz.org/
Questions?
http://www.devzuz.org/http://www.devzuz.org/http://www.devzuz.org/mailto:[email protected]:[email protected]