How do I download JAR during a build in Maven script?
16 Answers
Maven does not work like that. Here's the closest you'll get to my knowledge:
mvn dependency:get -DremoteRepositories=http://repo1.maven.org/maven2/ \
-DgroupId=junit -DartifactId=junit -Dversion=4.8.2 \
-Dtransitive=false
Note that all parameters except transitive are required.
Also note that Maven will download the jar to your local repository, and there's no sensible way (that I know of) to copy it to a local directory.
Reference:
-
@chrispolzer it does in Maven Dependency Plugin 2.8, while outputting a warning message regarding deprecation. It no longer works in current versions (3.1.1). Commented Jul 20, 2018 at 0:12
-
-
Or since 3.1, simply as:
mvn dependency:get -Dartifact=org.springframework:spring-instrument:3.2.3.RELEASE
This will place the JAR in the following directory:
~/.m2/repository/org/springframework/spring-instrument/3.2.3.RELEASE/
-
18For those like me who don't know Maven but are forced to use it: it downloads the jars in
~/.m2/repository/<artifact...>
.– MatthieuCommented Sep 2, 2019 at 10:24 -
3It works, but I had to put "" around the package like -Dartfiact="groupId:artifactId:version". Was using powershell.– JWoCommented Feb 13, 2020 at 17:15
Note: This answer is for downloading the jars directly from maven without any scripts
[That is how Google directed me here]
Assuming mvn
dependency is like this:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.4.7</version>
</dependency>
Goto http://search.maven.org and search for g:"com.zaxxer" AND a:"HikariCP" AND v:"2.4.7"
(simply searching for HikariCP
also works. You may need to select the appropriate GroupId
and Version
from the results)
In the Search Results -> Download
column, you should see jar javadoc.jar sources.jar
available for direct download
You can use:
mvn dependency:copy \
-Dartifact=<group>:<artifact-name>:<version> \
-DoutputDirectory=/tmp/my_custom_dir
(Replace <values>
with the ones of your case)
That's the full documentation of the goal: https://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html
Note: the other "dependecy:get" way of doing this has been deprecated.
This is what I do (2022 answer), go to https://mvnrepository.com/, search for your .jar and click on here:
-
1IMHO this does not answer the original question, because the question was to use Maven to download the JARs, not how to download it manually. Commented Jan 22, 2023 at 8:39
-
3
-
does not work for axis 1.6.1 mvnrepository.com/artifact/org.apache.axis/axis/1.6.1 - there are no files, just ref which cannot be opened Commented May 2 at 16:58
You can download Jar package to specific directory.
mvn dependency:get -Dartifact=org.riversun:random-forest-codegen:1.0.0 -Ddest=./
-
2dest is deprecated. This can be done in two commands: mvn dependency:get -Dartifact=org.riversun:random-forest-codegen:1.0.0:0.8.5:jar:nodeps mvn dependency:copy -Dartifact=org.riversun:random-forest-codegen:1.0.0:jar:nodeps -DoutputDirectory=.– Y.LCommented Jan 8, 2020 at 12:49
See How to use Maven pom to download jar files only. This worked really nicely for me.
My use case was that I wanted to download some of the dependency jars to deploy to a QA server, and was doing it manually (outside of the Maven build). I'm not sure exactly what your use case is.
Normally you don't use Maven for "just downloading", but for your build process. So normally, you do the following steps:
- Define a new project by defining the archetype of your project and some needed properties.
- Define as a dependency the library you want to use.
- Run Maven with
mvn compile
As a side effect, you will have downloaded the library to your local Maven repository. There are a lot of plugins to do something with dependencies, so have e.g. a look at the Maven Dependency plugin.
-
Define "normally" in a jenkins pipeline step where you just need to download and deploy a jar :) Commented Aug 13, 2018 at 12:27
If you just want to download a JAR once from a maven mirror I suggest you could just do this manually:
For Maven 1:
http://mirrors.ibiblio.org/pub/mirrors/maven/
For Maven 2:
http://mirrors.ibiblio.org/pub/mirrors/maven2/
These are the repositories (a mirror anyway) that maven will get its JARs from - you can easily access them in the webbrowser of your choice and download the JARs etc. Just browse through the hierarchy (it looks like any Java packag hierarchy) until you find the artefact, then pick the right version and you're good.
For example version 3.6.6.Final of hibernate-core from group org.hibernate you'd find here:
http://mirrors.ibiblio.org/pub/mirrors/maven2/org/hibernate/hibernate-core/3.6.6.Final/
-
4+1 or use mvnrepository.com , it has a search interface and provides direct links to the artifacts. Commented Aug 18, 2011 at 16:33
-
The ibiblio links above were empty directories when I tried them. Sean's link to mvnrepository.com worked well. Thanks! Commented Apr 28, 2016 at 22:27
Updated 2022 answer for command line - building on Ivan Carcamo's answer:
wget [the link that Ivan Carcamo points to in his screenshot]
Edit: This may download the thin jar for some repositories, so you may need to find a different link for the fat jar
You can setup a pom.xml to define your dependencies (the jars you want to copy). Then use the dependency:copy-dependencies goal to copy the jars to the desired location.
All the jars are available directly in the maven central repository. You don't have to use "maven" if all you want is the jar.
https://repo.maven.apache.org/maven2/
If the pom dependency is
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
You can download it from the corresponding directory structure by replacing the dots with forward slashses on the group id, artifact id, and version. I would navigate to the final folder to get the exact link of the jar.
https://repo.maven.apache.org/maven2/org/apache/poi/poi/5.2.2/poi-5.2.2.jar
-
Thanks @Richie. Just to add further here that poi-5.2.2 is artifactId-version and not groupid's poi-version. Commented Jun 24 at 9:58
It's possible to download a JAR from a Gitlab Maven private repository. The URL is appearing when running some Maven command so it's a bit hacky but it's working for me.
Like this:
wget --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/packages/maven/${MAVEN_PACKAGE_NAME}/${MAVEN_VERSION}/${JAR_FILE}"
Where,
- GITLAB_PRIVATE_TOKEN is a Gitlab Token with right "api" (atm the others are not enough)
- GITLAB_PROJECT_ID e.g. 1462237
- MAVEN_PACKAGE_NAME e.g. com/bar/foo
- MAVEN_VERSION e.g. 0.0.1
- JAR_FILE e.g. foo-0.0.1.jar
This works for me, but it downloads only the latest version:
#! /bin/bash
set -eu
if [[ -z ${1:-} || -z ${2:-} ]]; then
printf 'Usage: %s group artifact\n' "$(basename "$0")" >&2
exit 1
fi
g="$1"
a="$2"
url='https://search.maven.org'
v="$(curl -s "$url/solrsearch/select?q=g:$g+AND+a:$a&core=gav&rows=20&wt=json" |
jq -r '.response.docs[0].v')"
curl -s -L -o "$a-$v.jar" "$url/remotecontent?filepath=${g//.//}/$a/$v/$a-$v.jar"
You need bash
, curl
and jq
.
Example:
./maven-download net.sf.saxon Saxon-HE
Downloads Saxon-HE-11.4.jar
today (2022-12-23).
Use the below code snip
result = subprocess.check_output('mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get \
-DgroupId=%s \
-DartifactId=%s \
-Dversion=%s \
-Dtransitive=false \
-DremoteRepositories={repos_string} \
-Dpackaging=jar \
-DoutputDirectory=%s' % (group_id,
artifact_id,
version_name,
des_path), shell=True)
logger.info("success download jar: %s" % each_version)
except Exception as e:
logger.error("Error in download jar : %s" % str(e))
You can use this plugin:
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<phase>generate-test-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<uri>http://example.com/foo.zip</uri>
<unpack>true</unpack>
<maxLockWaitTime>100</maxLockWaitTime>
<outputDirectory>${project.build.directory}/foo</outputDirectory>
<md5>5b1f9712b75439446f2fe3d4e1339448</md5>
<checkSignature>true</checkSignature>
</configuration>
</execution>
</executions>
</plugin>