Upload
kevinmum1
View
228
Download
0
Embed Size (px)
Citation preview
8/6/2019 Java Power Tools
1/109
John Ferguson SmartPrinciple ConsultantWakaleo Consulting
Email: [email protected]: http://www.wakaleo.com
Twitter: wakaleo
1
http://www.wakaleo.com/http://www.wakaleo.com/mailto:[email protected]:[email protected]8/6/2019 Java Power Tools
2/109
Agile2009 - Making Agile Real
Learn about the latest evolutions in Java development tools.In particular, learn how to improve and automate yourdevelopment process using tools like Maven, Hudson, and
many others.
2
8/6/2019 Java Power Tools
3/109
Agile2009 - Making Agile Real
John Ferguson Smart
Consultant, Trainer, Mentor, Author,...
Works with Enterprise Java, Web Development,
and Open Source technologiesAuthor of Java Power Tools (OReilly)
Writes articles for sites like JavaWorld, DevX and
Java.net, and blogs on Java.net
Frequent speaker at conferences and Java UserGroups
Likes to write about himself in the third person
3
8/6/2019 Java Power Tools
4/109
Agile2009 - Making Agile Real
What we will cover today:
Industrializing your build process
Organizing your internal artifacts
Improving your release management strategy
Automate the build process
Better testing practices
Monitoring code coverage and code quality metrics
What we will cover today:
4
8/6/2019 Java Power Tools
5/109
Agile2009 - Making Agile Real
HERE is Edward Bear, coming downstairs now, bump,bump, bump, on the back of his head, behind
Christopher Robin. It is, as far as he knows, the only
way of coming downstairs, but sometimes he feels
that there really is another way, if only he could stop
bumping for a moment and think of it.
-- A. A. Milne
5
8/6/2019 Java Power Tools
6/109
Agile2009 - Making Agile Real
Industrializing your build process
Why bother?
Each team and each project using different conventions?
Build scripts ad-hoc and difficult to understand and maintain?Little reuse of components between teams and projects?
Code quality metrics and reporting are not done systematically, or not
at all?
High learning curve and maintenance costs?What can you do?
Set up a development infrastructure: e.g. Maven 2, Nexus, Hudson...
6
8/6/2019 Java Power Tools
7/109
Agile2009 - Making Agile Real
Build Automation - the big picture
maven
JUnitCI build server
7
8/6/2019 Java Power Tools
8/109
Agile2009 - Making Agile Real
Maven as a standardization tool
More than just your average build scripting tool!
A build tool
A dependency management toolA documentation generation tool
A metrics/code quality reporting tool
A project management tool
And more
8
8/6/2019 Java Power Tools
9/109
Agile2009 - Making Agile Real
So what can Maven do for me?
Standardize your build and deployment process
A standard, but extensible, build lifecycle
A standard directory structureClear and clean dependency management
Good technical documentation and reporting
9
8/6/2019 Java Power Tools
10/109
Agile2009 - Making Agile Real
Maven 2 Highlights
A Standard Life Cycle
A standardized approach to building your software
Familiar commands across all projects
No re-inventing the wheel
10
8/6/2019 Java Power Tools
11/109
Agile2009 - Making Agile Real
Maven 2 Highlights
A Standard Directory Structure
Familiar structure across all projects
Lower learning curveConvention Over Configuration
Less low-level scripting
11
8/6/2019 Java Power Tools
12/109
Agile2009 - Making Agile Real
Maven 2 Highlights
A Standard Way of Identifying Artifacts
Each Maven artifact has a unique identifier, or co-
ordinates
12
8/6/2019 Java Power Tools
13/109
Agile2009 - Making Agile Real
Maven 2 Highlights
Dependency Management
13
8/6/2019 Java Power Tools
14/109
Agile2009 - Making Agile Real
Maven 2 Highlights
Traditional Dependency Management
Each project has its own set of JAR files
Unnecessary duplicationHard to keep track of versions
Errors due to incompatible JAR files
Overloads the source code repository
lib
14
8/6/2019 Java Power Tools
15/109
Agile2009 - Making Agile Real
Maven 2 Highlights
Declarative Dependency Management
Artifact versions are stored on a central server
Each project declares what libraries and versions it needsAll the required dependencies are automatically downloaded
pom.xml
15
8/6/2019 Java Power Tools
16/109
Agile2009 - Making Agile Real
Maven 2 Highlights
Declaring Dependencies in Maven
Declared in the build script itself
Dependencies with version numbersDifferent types of dependencies
Compile, test, provided,...
16
8/6/2019 Java Power Tools
17/109
Agile2009 - Making Agile Real
Managing Maven Dependencies
Its easier with Eclipse...m2eclipse
Adding new dependencies
Visualizing dependenciesHandling dependency conflict
17
8/6/2019 Java Power Tools
18/109
Agile2009 - Making Agile Real
Managing Maven Dependencies
Viewing Maven project dependencies
18
8/6/2019 Java Power Tools
19/109
Agile2009 - Making Agile Real
Managing Maven Dependencies
Adding new dependencies...
19
8/6/2019 Java Power Tools
20/109
Agile2009 - Making Agile Real
Managing Maven Dependencies
Visualizing the Dependency Hierarchy
20
?
20
8/6/2019 Java Power Tools
21/109
Agile2009 - Making Agile Real
Managing Maven Dependencies
Visualizing the Dependency Graph
21
8/6/2019 Java Power Tools
22/109
Agile2009 - Making Agile Real
Managing Maven Dependencies
Visualizing conflicts in the Dependency Graph
22
8/6/2019 Java Power Tools
23/109
Agile2009 - Making Agile Real
Managing Maven Dependencies
Excluding Dependencies
23
23
8/6/2019 Java Power Tools
24/109
Agile2009 - Making Agile Real
DEMO
A walk through a simple Maven 2 project
24
8/6/2019 Java Power Tools
25/109
Agile2009 - Making Agile Real
Organizing your artifacts
Internal releases are hard to co-ordinate
How do I share my API with other teams?
Where is the latest version of that API?What version am I using, anyway?
What can you do?
Use an Enterprise Maven Repository
25
8/6/2019 Java Power Tools
26/109
Agile2009 - Making Agile Real
Organizing your artifacts
Maven Repositories
JAR files are downloaded to a local cache
26
8/6/2019 Java Power Tools
27/109
Agile2009 - Making Agile Real
Organizing your artifacts
Maven Repositories
Public web sites containing JARs for many open source projects
Maven automatically downloads the JARs you need onto your localmachine
You can publish internal APIs on your own repository
27
8/6/2019 Java Power Tools
28/109
Agile2009 - Making Agile Real
Organizing your artifacts
Maven Repositories
JAR files are downloaded as required into a local cache
local repository
Public Maven Repository
28
8/6/2019 Java Power Tools
29/109
Agile2009 - Making Agile Real
Organizing your artifacts
Maven Repositories
There are several public Maven repositories
You can also share internal JARs in an Enterprise Maven Repository
local repository
Public Maven Repository
Codehaus Maven RepositoryEnterprise Maven Repository
29
8/6/2019 Java Power Tools
30/109
Agile2009 - Making Agile Real
Organizing your artifacts
Maven Repositories
The Enterprise Maven Repository can also act as a proxy/cache to
the public repositories
local repository
Public Maven Repository
Codehaus Maven RepositoryEnterprise Maven Repository
30
8/6/2019 Java Power Tools
31/109
Agile2009 - Making Agile Real
Organizing your artifacts
Key Best Practices
Deploy your internal releases to a local Enterprise repository
Store proprietary JAR files you need here as wellDistinguish between snapshot and release versions
31
8/6/2019 Java Power Tools
32/109
Agile2009 - Making Agile Real
Organizing your artifacts
Using the Nexus repository manager
Easy to set up
Easy to administerProxy/cache
32
8/6/2019 Java Power Tools
33/109
Agile2009 - Making Agile Real
DEMO
An Enterprise Repository
33
8/6/2019 Java Power Tools
34/109
Agile2009 - Making Agile Real
Working with snapshots and releases
Snapshots
Work in progress
A new time-stamped version deployed with each deployment
$mvn deploy
34
8/6/2019 Java Power Tools
35/109
Agile2009 - Making Agile Real
Working with snapshots and releases
Releases
Stable, tested release
The deployed artifact is unique
$mvn deploy
35
8/6/2019 Java Power Tools
36/109
Agile2009 - Making Agile Real
Working with snapshots and releases
Setting it all up in Maven
To deploy a snapshot version, you need:
A Snapshot repositoryA SNAPSHOT version number
User authentication for the repository
A Snapshot configuration for the repository
36
http://wanaka:8081/nexus/content/repositories/snapshotshttp://wanaka:8081/nexus/content/repositories/snapshotshttp://wanaka:8081/nexus/content/repositories/snapshots8/6/2019 Java Power Tools
37/109
Agile2009 - Making Agile Real
Working with snapshots and releases
Automating snapshot deployments
Use the maven-release-plugin to automate SCM book-keeping
mvn:preparemvn:perform
mvn:rollback
Nexus
tax-calculator-core-1.0.1
37
8/6/2019 Java Power Tools
38/109
Agile2009 - Making Agile Real
DEMO
Releases, Snapshots and Repositories
38
8/6/2019 Java Power Tools
39/109
Agile2009 - Making Agile Real
Continuous Integration - whats the issue?
Traditional development cycles are bad for your health:
Integration is long and difficult
Poor visibility on development progressFunctional tests are done too late
Raised issues are harder to fix
The client gets a sub-optimal product
39
8/6/2019 Java Power Tools
40/109
Agile2009 - Making Agile Real
Continuous Integration - whats involved?
40
8/6/2019 Java Power Tools
41/109
Agile2009 - Making Agile Real
Continuous Integration - why bother?
Smoother integration process
Automatic regression testing
Regular working releasesEarlier functional testing
Faster and easier bug fixes
Better visibility
41
8/6/2019 Java Power Tools
42/109
Agile2009 - Making Agile Real
Continuous Integration - what you need
42
8/6/2019 Java Power Tools
43/109
Agile2009 - Making Agile Real
Continuous Integration - what can it do?
More than just your average build scheduler!
Raise (and monitor) integration issues - fast!
Automatically publish Maven artifactsMonitor your build process
Monitor and report on code quality and code coverage
43
8/6/2019 Java Power Tools
44/109
Agile2009 - Making Agile Real
Continuous Integration - raising issues fast!
Use an appropriate notification strategy, e.g.
The committer is notified for all build results
Team members are notified for any failed buildsTeam leader gets special notification after 5 successive build failures
44
8/6/2019 Java Power Tools
45/109
Agile2009 - Making Agile Real
Continuous Integration - raising issues fast!
Use an appropriate (fast) notification mechanism
Instant Messaging, SMS etc. for fast notification
Email as a secondary channelRSS for consulting build history
45
8/6/2019 Java Power Tools
46/109
Agile2009 - Making Agile Real
Continuous Integration - raising issues fast!
Use a good build plan strategy
Fast builds first, e.g.
Unit tests before integration testsIntegration tests before metrics
Use manual builds where appropriate
Release builds
Deployments...
46
8/6/2019 Java Power Tools
47/109
Agile2009 - Making Agile Real
Looking for a good O/S Continuous Integration tool?
Try Hudson!
Easy to set up and configure
Good build and code quality metricsLots of plugins
47
8/6/2019 Java Power Tools
48/109
Agile2009 - Making Agile Real
DEMO
Automating the builds
48
8/6/2019 Java Power Tools
49/109
Agile2009 - Making Agile Real
Improving your testing game
Innovative testing techniques
Automating unit tests
Separate unit tests and integration testsMonitor test duration
Keep tabs on test coverage
49
8/6/2019 Java Power Tools
50/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Unit testing
A cornerstone of modern software development
Unit tests can help you:Ensure that code behaves as expected
Make your code more flexible and easier to maintain
Detect regressions
Document your code
50
8/6/2019 Java Power Tools
51/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Innovative unit testing
More readable tests - Hamcrest asserts
More efficient tests - Parameterized testsCleaner tests - using Groovy
More accurate tests - Behavior Driven Development (BDD)
Functional and web tests
Use your imagination!
51
8/6/2019 Java Power Tools
52/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Hamcrest asserts
More expressive and readable test assertions
Easier to understand
Less chance of test errors
52
8/6/2019 Java Power Tools
53/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Hamcrest asserts
Traditional JUnit 3.x asserts are hard to read:
Parameter order is counter-intuitive for English-speakers
x=10 is written
The statements don't read well for English-speakers
Assert that are equal 10 and xDefault error messages are sometimes limited:
53
8/6/2019 Java Power Tools
54/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Hamcrest asserts
JUnit 4.4 introduces the assertThat statement
Rather than writing:
You can write
54
8/6/2019 Java Power Tools
55/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Parameterized tests
Run several sets of test data against the same test case
Help reduce the number of unit tests to write
Encourage developers to test more thoroughly
55
8/6/2019 Java Power Tools
56/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Parameterized tests
Example: Calculating income tax
56
8/6/2019 Java Power Tools
57/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Parameterized tests
What you need:
Some test data
A test class with matching fields
And some tests
And an annotation
57
8/6/2019 Java Power Tools
58/109
Agile2009 - Making Agile Real
Use mocks and stubs
Mockito - lightweight mocking
58
8/6/2019 Java Power Tools
59/109
Agile2009 - Making Agile Real
DEMO
Better Testing - Parameterized Tests and Hamcrest Asserts
59
8/6/2019 Java Power Tools
60/109
8/6/2019 Java Power Tools
61/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Some examples...
61
8/6/2019 Java Power Tools
62/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Some examples...
62
8/6/2019 Java Power Tools
63/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Behaviour-Driven Develpment
Its not about writing tests
TDD and BDD is about writing better code:
Maintainable
Flexible
Reliable
Simple
63
8/6/2019 Java Power Tools
64/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Behavior-Driven Develpment
BDD uses words like should to describe the desired behavior
Should transfer money from account A to account B
Should deploy landing gear before touching ground
...
64
8/6/2019 Java Power Tools
65/109
8/6/2019 Java Power Tools
66/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Easyb stories:
Use a narrative approach
Describe a precise requirement
Can be understood by a stakeholder
Usually made up of a set of scenarios
Use an easy-to-understand structure:
Given [a context]...When [something happens]...
Then [something else happens]...
66
8/6/2019 Java Power Tools
67/109
8/6/2019 Java Power Tools
68/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Easyb reporting
Simple but readable...
68
8/6/2019 Java Power Tools
69/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Continuous Testing
Run your tests whenever you save your code changes
Test failures are treated like compiler errors!
Two main tools:
Infinitest
JUnitMax
69
DEMO
8/6/2019 Java Power Tools
70/109
Agile2009 - Making Agile Real
DEMO
Better Testing - Testing with Groovy and EasyB
70
8/6/2019 Java Power Tools
71/109
Agile2009 - Making Agile Real
Use innovative testing techniques
Integration Test strategies
Deploy to an embedded Jetty server for local integration tests
Run local integration tests with a web testing tool
Selenium
HtmlUnit
JWebUnit
Canoo WebTest
Deploy automatically to a publicly-visible integration server
71
8/6/2019 Java Power Tools
72/109
Agile2009 - Making Agile Real
Web Testing Tools
Two strategies to developer web testing:
Test before deployment
Use Jetty
Run web tests locally from within the normal build
Test after deployment
Deploy to a test server
Run web tests against a remote server in a dedicated build job
You probably need both...
72
8/6/2019 Java Power Tools
73/109
Agile2009 - Making Agile Real
Examples of Web Testing Tools
Selenium
Runs tests through a real browser
Accurate rendition of Javascript and AJAX behavior
High-level readable API
More complicated to set up
Slow
73
8/6/2019 Java Power Tools
74/109
Agile2009 - Making Agile Real
Examples of Web Testing Tools
Selenium
74
http://localhost:8080/http://localhost:8080/8/6/2019 Java Power Tools
75/109
Agile2009 - Making Agile Real
Examples of Web Testing Tools
HTMLUnit
Simulates a browser
Lower level API
Easier to set up
Fast
75
8/6/2019 Java Power Tools
76/109
Agile2009 - Making Agile Real
Examples of Web Testing Tools
HTMLUnit
76
http://localhost:8080/ebank-webhttp://localhost:8080/ebank-web8/6/2019 Java Power Tools
77/109
Agile2009 - Making Agile Real
Examples of Web Testing Tools
JWebUnit
Simulates the browser (build on HTMLUnit)
Higher and more readable API
Easy to set up
Fast
77
8/6/2019 Java Power Tools
78/109
Agile2009 - Making Agile Real
Examples of Web Testing Tools
JWebUnit
78
DEMO
http://localhost:9090/ebank-webhttp://localhost:9090/ebank-web8/6/2019 Java Power Tools
79/109
Agile2009 - Making Agile Real
DEMO
Better Testing - Web Testing
79
8/6/2019 Java Power Tools
80/109
Agile2009 - Making Agile Real
Why use code quality metrics
Better quality code
Code is easier to maintain
Detect potential bugsTrain new staff
80
8/6/2019 Java Power Tools
81/109
Agile2009 - Making Agile Real
Why automate code quality metrics
Global picture of code quality
Statistical code quality trends
More value-added manual reviews
81
8/6/2019 Java Power Tools
82/109
Agile2009 - Making Agile Real
How automate code quality metrics
Integrate code quality metrics into your build scripts
Run a special build reserved for code metrics
TIP: Code quality metrics are easier with Maven!
82
8/6/2019 Java Power Tools
83/109
Agile2009 - Making Agile Real
Types of code quality metricsCoding Standards
Naming conventions, Javadoc comments, layout,...
Tool: Checkstyle
Best PracticesGood programming habits, potential bugs,...
Tools: Checkstyle, PMD, Findbugs, Crap4j
Bug Detection
Broken code, dangerous code, bugs,...Tool: FindBugs
Code Coverage
How much code is executed by your tests
Tool: Cobertura, Emma, Clover,Crap4j...
83
8/6/2019 Java Power Tools
84/109
Agile2009 - Making Agile Real
Coding standards - how far do you go?
Customize for your organization!
Configure rules to match your practices
Remove rules that dont apply
Add non-default rules where useful
84
8/6/2019 Java Power Tools
85/109
Agile2009 - Making Agile Real
Coding standards - how far do you go?
Example - Checkstyle javadoc conventions
85
8/6/2019 Java Power Tools
86/109
Agile2009 - Making Agile Real
Team code reviews
Review code as a group
Long and slow if done manually
Benefits greatly from the use of tools
86
8/6/2019 Java Power Tools
87/109
Agile2009 - Making Agile Real
Automating code quality metrics with Hudson
Using the Violations plugin
Reports on Checkstyle, PMD, Findbugs, and others
Uses data generated with Maven or Ant
87
8/6/2019 Java Power Tools
88/109
Agile2009 - Making Agile Real
Automating code quality metrics with Hudson
Configuring the Violations plugin
88
8/6/2019 Java Power Tools
89/109
Agile2009 - Making Agile Real
Automating code quality metrics with Hudson
Displaying the Violations reports
89
8/6/2019 Java Power Tools
90/109
Agile2009 - Making Agile Real
Automating code quality metrics with Hudson
Displaying the Violations reports
90
8/6/2019 Java Power Tools
91/109
Agile2009 - Making Agile Real
Automating code quality metrics with Hudson
Displaying the Violations details
91
8/6/2019 Java Power Tools
92/109
Agile2009 - Making Agile Real
92
8/6/2019 Java Power Tools
93/109
Agile2009 - Making Agile Real
Code Coverage
What can code coverage metrics tell you:
Indicates what code is being executed by your unit tests.
Can help isolate untested code
Does not guarantee that the tests that are run are of high quality
Can be an indicator of whether tests are being written at all
93
8/6/2019 Java Power Tools
94/109
Agile2009 - Making Agile Real
Code Coverage
Using the Hudson code coverage plugins
Generate data using Ant or Maven
Report coverage metrics in Hudson
94
8/6/2019 Java Power Tools
95/109
Agile2009 - Making Agile Real
Code Coverage
Using the Hudson code coverage plugins
Cobertura coverage reports
95
8/6/2019 Java Power Tools
96/109
Agile2009 - Making Agile Real
Code Coverage
Using the ECLEclipse plugin
96
8/6/2019 Java Power Tools
97/109
Agile2009 - Making Agile Real
Code Quality Management
Check out Sonar!
Centralized code quality management
Generate code quality metrics in an automated build using Maven
Store code quality metrics in a database
Code quality metrics can be consulted on a web site
97
8/6/2019 Java Power Tools
98/109
Agile2009 - Making Agile Real
Code Quality Management
Sonar architecture
98
8/6/2019 Java Power Tools
99/109
Agile2009 - Making Agile Real
Code Quality Management
Sonar centralizes many code quality metrics
99
8/6/2019 Java Power Tools
100/109
Agile2009 - Making Agile Real
Code Quality Management
You can drill down to view the details for each type of issue
100
8/6/2019 Java Power Tools
101/109
Agile2009 - Making Agile Real
Code Quality Management
You can also view historical data
101
8/6/2019 Java Power Tools
102/109
Agile2009 - Making Agile Real
Code Quality Management
You can also view historical data
102
8/6/2019 Java Power Tools
103/109
Agile2009 - Making Agile Real
More advanced code quality metrics
How long do your tests run
Overly-long tests slow down your build process
They may also indicate a performance issue
103
8/6/2019 Java Power Tools
104/109
Agile2009 - Making Agile Real
More advanced code quality metricsAre your tests failing repeatedly
May indicate a difficult technical issue
Could result in unreliable quick-fix solutions
104
DEMO
8/6/2019 Java Power Tools
105/109
Agile2009 - Making Agile Real
DEMO
Automated Code Quality
105
8/6/2019 Java Power Tools
106/109
Agile2009 - Making Agile Real
Automate your deployment processReproducible process
Save time
Audit trace
ApplicationServer
Enterprise
Repository
106
8/6/2019 Java Power Tools
107/109
Agile2009 - Making Agile Real
Automating the deployment process with CargoStart, stop and install application servers
Deploy to different application servers
Run a stand-alone instance
Or deploy to an existing server
107
8/6/2019 Java Power Tools
108/109
Agile2009 - Making Agile Real
There are many ways to improve a developmentprocess
Improve your build process and dependency management
Install a CI server and publish your APIs automatically
Use code quality metrics to isolate bottlenecks and fix
problems
Automate as much as possible!
108
8/6/2019 Java Power Tools
109/109