@@ -10,98 +10,142 @@ layout: default
10
10
## Introduction
11
11
{:.no_toc}
12
12
13
- Hamcrest consists of different jars matching the different needs of
14
- applications. This document describes these distributables and the
15
- functionality contained in each of them.
13
+ This document describes the current and previous versions of the various Hamcrest
14
+ distributables, and the functionality contained in each of them.
15
+
16
+ The latest version of Hamcrest consists of a single jar file which contains base
17
+ classes and a library of useful matcher implementations. This is different from
18
+ older versions.
19
+
20
+ Older versions of Hamcrest consisted of a number of different jars matching the
21
+ different needs of applications. The change in the jar packaging requires care
22
+ when upgrading.
16
23
17
24
## Table of Contents
18
25
{:.no_toc}
19
26
20
27
* TOC
21
28
{: toc }
22
29
23
- ## The Java Hamcrest Jars
24
-
25
- ### ` hamcrest-core.jar `
26
-
27
- This is the core API to be used by third-party framework providers.
28
- This includes a foundation set of matcher implementations for common
29
- operations. This API is stable and will rarely change. You will
30
- need this library as a minimum.
31
-
32
- ### ` hamcrest-library.jar `
33
-
34
- The library of Matcher implementations which are based on the core
35
- functionality in hamcrest-core.jar.
36
-
37
- ### ` hamcrest-integration.jar `
38
-
39
- Provides integration between Hamcrest and other testing tools, such as jMock
40
- and EasyMock. Uses hamcrest-core.jar and hamcrest-library.jar.
41
-
42
- ### ` hamcrest-generator.jar `
43
-
44
- A tool to allow many Matcher implementations to be combined into
45
- a single class with static methods returning the different matchers
46
- so users don't have to remember many classes/packages to import.
47
- Generates code. This library is only used internally at compile time.
48
- It is not necessary for the use of any of the other hamcrest libraries
49
- at runtime.
50
-
51
- ### ` hamcrest-all.jar `
52
-
53
- One jar containing all classes of all the other jars.
54
-
55
-
56
- ## Adding Hamcrest to your Project
30
+ ## The Hamcrest Jar
57
31
58
- The dependencies of the jars (` hamcrest-integration ` uses ` hamcrest-library `
59
- which uses ` hamcrest-core ` ) is represented by the Maven dependency mechanism.
60
- There is no ` hamcrest-all ` library in the Maven repo prior to version 1.3. Just
61
- use hamcrest-integration which references all the other hamcrest libraries.
62
-
32
+ All the base classes and standard matcher implementations are contained in a
33
+ single jar file called ` hamcrest-2.1.jar ` .
63
34
64
- ### Gradle
35
+ ### Using Hamcrest in a Gradle Project
65
36
66
- Add ` "org.hamcrest:hamcrest-library:1.3 " ` to the dependencies section of your
37
+ Add ` "org.hamcrest:hamcrest:2.1 " ` to the dependencies section of your
67
38
` build.gradle ` , for example:
68
39
69
40
``` gradle
70
41
apply plugin: 'java'
71
42
72
43
dependencies {
73
- testImplementation 'org.hamcrest:hamcrest-library:1.3 '
44
+ testImplementation 'org.hamcrest:hamcrest:2.1 '
74
45
}
75
46
```
76
47
77
48
Note: older versions of Gradle use the ` testCompile ` configuration
78
49
instead of the ` testImplementation ` configuration.
79
50
80
- ### Maven
51
+ ### Using Hamcrest in a Maven Project
81
52
82
- Add the following dependency declaration to your ` pom.xml ` :
53
+ Add the following to the ` <dependencies> ` section in your ` pom.xml ` :
83
54
84
55
``` xml
85
56
<dependency >
86
57
<groupId >org.hamcrest</groupId >
87
- <artifactId >hamcrest-library </artifactId >
88
- <version >1.3 </version >
58
+ <artifactId >hamcrest</artifactId >
59
+ <version >2.1 </version >
89
60
<scope >test</scope >
90
61
</dependency >
91
62
```
92
63
93
- ### Download
64
+ ### Download the Hamcrest Jar
94
65
95
- You can download the jars directly from Maven Central. At a minimum, you will
96
- need ` hamcrest-core.jar ` , though most people will also use
97
- ` hamcrest-library.jar ` too. If you want to integrate Hamcrest Matchers with
98
- other test tools, such as jMock or EasyMock, download ` hamcrest-integration.jar ` , too.
99
-
100
- You can find the jars by searching Maven Central for groupId ` org.hamcrest `
66
+ You can download the jars directly from Maven Central. You can find the jars by searching Maven Central for groupId ` org.hamcrest `
101
67
using the following link:
102
68
103
69
> [ https://search.maven.org/search?q=g:org.hamcrest ] ( https://search.maven.org/search?q=g:org.hamcrest )
104
70
105
- Maven central has some extra artifacts called ` java-hamcrest ` and ` hamcrest-java ` ,
106
- with a version of ` 2.0.0.0 ` . Please do not use these, as they are an aborted effort
107
- at repackaging the different jars.
71
+ ## Previous Versions of Hamcrest
72
+
73
+ Prior to version 2.x, Hamcrest was distributed through multiple jars, described
74
+ below.
75
+
76
+ * ** ` hamcrest-core.jar ` ** : This was the core API to be used by third-party framework
77
+ providers. This includes a foundation set of matcher implementations for common
78
+ operations. This library was used as a dependency for many third-party libraries,
79
+ including JUnit 4.x. From Hamcrest version 2.x, all the classes in
80
+ ` hamcrest-core.jar ` were moved into ` hamcrest.jar ` .
81
+ * ** ` hamcrest-library.jar ` ** : The library of Matcher implementations which are based
82
+ on the core functionality in hamcrest-core.jar. From Hamcrest version 2.x, all the
83
+ classes in ` hamcrest-core.jar ` were moved into ` hamcrest.jar ` .
84
+ * ** ` hamcrest-integration.jar ` ** : Provides integration between Hamcrest and other
85
+ testing tools, such as jMock and EasyMock. It depends upon ` hamcrest-core.jar ` and
86
+ ` hamcrest-library.jar ` . There are no new releases of this library since version 1.3.
87
+ * ** ` hamcrest-generator.jar ` ** : A tool to allow many Matcher implementations to be
88
+ combined into a single class with static methods returning the different matchers
89
+ so users don't have to remember many classes/packages to import. Generates code.
90
+ This library is only used internally at compile time. It is not necessary for the
91
+ use of any of the other hamcrest libraries at runtime. There are no new releases of
92
+ this library since version 1.3.
93
+ * ** ` hamcrest-all.jar ` ** : One jar containing all classes of all the other jars.
94
+ There are no new releases of this library since version 1.3. Please use the single
95
+ ` hamcrest.jar ` instead.
96
+
97
+ ### Upgrading from Hamcrest 1.x
98
+
99
+ Care must be taken when upgrading from Hamcrest 1.3 or earlier. Due to the change in
100
+ packaging, the version conflict resolution that happens in dependency management
101
+ tools won't happen automatically. A common example is projects that depend upon
102
+ JUnit 4. JUnit 4 declares a transitive dependency upon ` hamcrest-core-1.3.jar ` .
103
+ Because ` hamcrest-core ` is not the same artifact as ` hamcrest ` , it will not be
104
+ upgraded.
105
+
106
+ To address this issue, Hamcrest 2.1 and later also publish artifacts for
107
+ ` hamcrest-core ` and ` hamcrest-library ` . Although these jars contain no classes,
108
+ they trigger the version conflict upgrade in the dependency manager, and correctly
109
+ declare transitive dependencies upon the new ` hamcrest ` packaging. Users can
110
+ directly declare a dependency upon these shim jars to force the upgrade.
111
+
112
+ #### Gradle Upgrade Example
113
+
114
+ ``` gradle
115
+ apply plugin: 'java'
116
+
117
+ dependencies {
118
+ testImplementation 'org.hamcrest:hamcrest:2.1'
119
+ testImplementation 'org.hamcrest:hamcrest-library:2.1'
120
+ testImplementation 'junit:junit:4.12'
121
+ }
122
+ ```
123
+
124
+ #### Maven Upgrade Example
125
+
126
+ ** Warning** :
127
+ Maven users should declare a dependency upon ` hamcrest-library ` ** before** other
128
+ dependencies, otherwise the older version will take precedence.
129
+
130
+ ``` xml
131
+ <dependencies >
132
+ <dependency >
133
+ <groupId >org.hamcrest</groupId >
134
+ <artifactId >hamcrest</artifactId >
135
+ <version >2.1</version >
136
+ <scope >test</scope >
137
+ </dependency >
138
+ <dependency >
139
+ <groupId >org.hamcrest</groupId >
140
+ <artifactId >hamcrest-library</artifactId >
141
+ <version >2.1</version >
142
+ <scope >test</scope >
143
+ </dependency >
144
+ <dependency >
145
+ <groupId >junit</groupId >
146
+ <artifactId >junit</artifactId >
147
+ <version >4.12</version >
148
+ <scope >test</scope >
149
+ </dependency >
150
+ </dependencies >
151
+ ```
0 commit comments