Skip to content

Instantly share code, notes, and snippets.

@robin-a-meade
Last active February 23, 2022 19:51
Show Gist options
  • Save robin-a-meade/91cb5eedf9c0dc853eb38e755bd01200 to your computer and use it in GitHub Desktop.
Save robin-a-meade/91cb5eedf9c0dc853eb38e755bd01200 to your computer and use it in GitHub Desktop.
Using maven as a downloader

Using maven as a downloader

I needed to download Oracle JDBC jar files for use with my database query tool. Oracle JDBC drivers have been available in the public official Maven repository since September 15, 2019. They extended it to include all supported releases in February, 2020. (link1 link2 link3 link4).

It is possible to automate the download of these jars using maven. You can download the latest version, or a specific version.

See the example download-ojdbc.xml file below. It downloads the latest Oracle jdbc jar files and copies them to the appropriate directory (~/.dbvis/jdbc, in my case).

It utilizes the copy goal of the Apache Maven Dependency Plugin. Consult the documentation for the various parameters you may use.

Note that I bound the execution of the copy goal to the validate phase, and I specified the validate phase as the default phase using the defaultGoal element. (The defaultGoal element takes either a goal or a phase).

This let's me execute it simply as:

mvn -f download-ojdbc.xml

instead of

mvn -f download-ojdbc.xml dependency:copy@theExecutionId

The later would have required adding id element too, like this:

   <id>theExecutionId</id>

Partial output:

[INFO] com.oracle.database.jdbc:ojdbc8:LATEST:jar already exists in /home/robin/.dbvis/jdbc
[INFO] com.oracle.database.nls:orai18n:LATEST:jar already exists in /home/robin/.dbvis/jdbc
[INFO] com.oracle.database.xml:xmlparserv2:LATEST:jar already exists in /home/robin/.dbvis/jdbc
[INFO] com.oracle.database.xml:xdb:LATEST:jar already exists in /home/robin/.dbvis/jdbc
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.863 s
[INFO] Finished at: 2020-12-22T13:20:19-10:00
[INFO] ------------------------------------------------------------------------

A nice thing about this is that it is idempotent. As you see in the above output, it detected that the latest jar files were already present and didn't overwrite them. (This behavior can be changed with parameters of the copy goal.)

I also like how maven embeds the version in the filename, e.g., ojdbc8-19.8.0.0.jar. That wouldn't be the case if I downloaded the drivers directly from https://www.oracle.com/database/technologies/jdbc-upc-downloads.html.

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>1</version>
<build>
<defaultGoal>validate</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>LATEST</version>
<type>jar</type>
<overWrite>false</overWrite>
</artifactItem>
<artifactItem>
<groupId>com.oracle.database.nls</groupId>
<artifactId>orai18n</artifactId>
<version>LATEST</version>
<type>jar</type>
<overWrite>false</overWrite>
</artifactItem>
<artifactItem>
<groupId>com.oracle.database.xml</groupId>
<artifactId>xmlparserv2</artifactId>
<version>LATEST</version>
<type>jar</type>
<overWrite>false</overWrite>
</artifactItem>
<artifactItem>
<groupId>com.oracle.database.xml</groupId>
<artifactId>xdb</artifactId>
<version>LATEST</version>
<type>jar</type>
<overWrite>false</overWrite>
</artifactItem>
</artifactItems>
<!--
The default output directory is ${project.basedir}/target/dependency
You can override this the following configuration parameter
-->
<outputDirectory>${HOME}/.dbvis/jdbc</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

For simple needs, executing the dependency:copy goal on the command line is sufficient.

Pros:

  • No need to reference a pom.

Cons:

  • It can handle only one artifact at a time. If you have several artifacts to download, invoking maven multiple times instead of just once will be slow.
[robin@laptop] $ mvn dependency:copy \
        -Dartifact=com.oracle.database.jdbc:ojdbc8:LATEST \
        -Dtransitive=false \
        -DoutputDirectory=.

TODO: Write a general purpose shell script

Similar to https://gist.github.com/TakahikoKawasaki/8664115

  • but based on dependency:copy instead of dependency:get
  • and downloads to the current working directory, instead of just the cache (~/.m2/repository)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment