When writing Java code we always need some libraries, for example, to do unit test we need the JUnit library, to do String manipulation we need Apache Common Util libraries.
For bigger project we may need to create our own library and use it in different parts of the project.
The library Java files are packaged in JAR files and these JAR files are needed on the classpath when you compile your project code.
Different project need different version of the libraries. Keeping the project up-to-date with the correct versions of library JAR files is not an easy task.
Each external JAR may again also depend on other external JAR files etc. Downloading all these external dependency JAR files recursively and making sure that the right versions are downloaded is a huge task.
When the project grows bigger and bigger, we would need more and more external dependencies.
Maven has built-in dependency management. We can just specify in the POM file what external libraries the project depends on, and which version.
Maven would download them and puts them in your local Maven repository.
If these external libraries need other libraries, then Maven would download their dependency libraries into your local Maven repository as well.
We can specify dependencies inside the dependencies element in the POM file.
Here is an example:
<project ...> <modelVersion>4.0.0</modelVersion> <groupId>com.java2s.crawler</groupId> <artifactId>java-web-crawler</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
In the code above there are two dependency elements. Each dependency element describes an external dependency.
Each dependency is described by its groupId, artifactId and version.
We use groupId, artifactId and version to identify our own project in the top of the pom file. We also use those three fields to identify a library.
The code above needs the org.jsoup group's jsoup artifact in version 1.7.3, and the junit group's junit artifact in version 4.8.1.
When executing this POM file, the two dependencies will be downloaded from a central Maven repository and put into your local Maven repository if the dependencies are missing from your local Maven repository.
If the dependencies are already found in your local repository, Maven will not download them.
If a given dependency is not available in the central Maven repository. You can download the dependency yourself and put it into your local Maven repository.
Remember to put it into a subdirectory structure matching the groupId, artifactId and version. Replace all dots (.) with / and separate the groupId, artifactId and version with /
.
The two dependencies downloaded by the example above will be put into the following subdirectories:
MAVEN_REPOSITORY_ROOT/junit/junit/4.8.1 MAVEN_REPOSITORY_ROOT/org/jsoup/jsoup/1.7.3
A Maven external dependency is a dependency JAR file which is not in a local or remote Maven repository.
External dependencies may be located on your own local hard disk.
"external" means external to the Maven repository system.
We can configure an external dependency like this:
<dependency> <groupId>mydependency</groupId> <artifactId>mydependency</artifactId> <scope>system</scope> <version>1.0</version> <systemPath>${basedir}\war\WEB-INF\lib\mydependency.jar</systemPath> </dependency>
The groupId
and artifactId
are both set to the name of the dependency.
The scope
element value is set to system.
The systemPath
element is pointing to the location of the JAR file.
The ${basedir}
points to the directory where the POM is located.
Snapshot dependencies are dependency JAR files under development.
Instead of constantly updating the version numbers to get the latest version, you can mark a dependency JAR file as snapshot so that the Maven will always download it.
Snapshot versions are always downloaded into your local repository for every build, even if a matching snapshot version is already downloaded in your local repository.
To mark your project as a snapshot version we can append -SNAPSHOT
to the version number in the beginning of the POM where you set the groupId
and artifactId
.
Here is a version element example:
<version>1.0-SNAPSHOT</version>
The -SNAPSHOT
is appended to the version number.
When declaring the dependency
element in the pom.xml we have to
use the -SNAPSHOT
as well as follows.
<dependency> <groupId>com.jenkov</groupId> <artifactId>java-web-crawler</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
The -SNAPSHOT
appended to the version number marks a snapshot
dependency library
in Maven.
We can configure how often Maven would download snapshot dependencies in the Maven settings.xml
File.