Fetching external libraries with Maven

One of the leading motives for using Maven is its capability to fetch external class libraries from the internet. Java’s own class libraries are extensive but naturally cannot cover all our potential needs. If there exists an external Java library that offers some functionality we would like to use, it is higly likely that the library can be found from Maven’s central repository that provides access to even millions of libraries. If this is the case, then we can import the library into a maven project by simply adding a corresponding dependency definition into the project file pom.xml. When we then build the project, Maven will automatically download the defined package from the internet and the project can then use its classes and interfaces in a very similar manner as how Java’s own class libraries are used.

A little further below is an example project file that contains a dependency definition: the project file now contains also a dependencies element that in turn contains dependency elements that list the dependencies of the project. The example below defines only one dependency: the jdom2 library that offers tools for processing XML data. A dependency definition specifies the groupId, artifactId and version of the library. These three values allow Maven to identify the exact library and version that should be fetched. This also is the reason why all Maven projects have to specify these three values: if our own project would be published as a library in Maven’s central repository, end users could import our library by specifying the three values groupId, artifactId and version in a dependency definition in their own Maven project files.

Maven central repository

You may wonder how we can know what dependency definition values should be used in order to import a certain library? Quite often the documentation of the library (which we perhaps found via an internet search engine etc.) describes the required Maven dependency definition. In any case we can also search the library from Maven’s central repository, which offers a search page https://search.maven.org/. The central repository e.g. lists different available library versions, and the information for a certain version also describes the corresponding Maven dependency definition.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>fi.tuni.prog3</groupId>
  <artifactId>example</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.jdom</groupId>
      <artifactId>jdom2</artifactId>
      <version>2.0.6.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>com.jolira</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <version>1.4.4</version>
        <executions>
          <execution>
            <configuration>
              <mainClass>fi.tuni.prog3.example.ExampleMain</mainClass>
              <onejarVersion>0.97</onejarVersion>
              <attachToBuild>true</attachToBuild>
            </configuration>
            <goals>
              <goal>one-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Programming demo (duration 43:49)