79

How do I download JAR during a build in Maven script?

Sam Saffron
  • 124,587
  • 78
  • 320
  • 501
Nadav Benedek
  • 801
  • 1
  • 7
  • 4

12 Answers12

79

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:

Sean Patrick Floyd
  • 284,665
  • 62
  • 456
  • 576
54

Or since 3.1, simply as mvn dependency:get -Dartifact=org.springframework:spring-instrument:3.2.3.RELEASE

kisna
  • 2,587
  • 1
  • 23
  • 29
  • 7
    For those like me who don't know Maven but are forced to use it: it downloads the jars in `~/.m2/repository/`. – Matthieu Sep 02 '19 at 10:24
  • It works, but I had to put "" around the package like -Dartfiact="groupId:artifactId:version". Was using powershell. – JWo Feb 13 '20 at 17:15
29

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

Venkata Raju
  • 4,396
  • 3
  • 26
  • 34
  • WARNING: maven's default download link doesn't use https, double check the url before assuming your download isn't compromised. – user3338098 Jul 19 '18 at 21:33
  • 2
    this is very time consuming to download one at a time when a single jar file depends on a dozen other jar files which themselves may depend on yet more jar files... – user3338098 Jul 19 '18 at 22:03
  • This answer has nothing to do with maven and therefore does not answer OP's question. – Alex Jul 28 '20 at 11:02
9

You can download Jar package to specific directory.

mvn dependency:get -Dartifact=org.riversun:random-forest-codegen:1.0.0 -Ddest=./
riversun
  • 618
  • 8
  • 10
  • 2
    dest 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.L Jan 08 '20 at 12:49
8

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.

Community
  • 1
  • 1
Sam Goldberg
  • 6,644
  • 7
  • 47
  • 83
8

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.

Francesco
  • 181
  • 2
  • 9
5

Normally you don't use Maven for "just downloading", but for your build process. So normally, you do the following steps:

  1. Define a new project by defining the archetype of your project and some needed properties.
  2. Define as a dependency the library you want to use.
  3. 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.

mliebelt
  • 15,115
  • 7
  • 53
  • 90
5

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/

fgysin
  • 10,754
  • 11
  • 57
  • 89
4

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.

Jose
  • 1,536
  • 4
  • 26
  • 36
  • 1
    for those who don't want to learn about maven: run the command `mvn dependency:copy-dependencies` and look in `./target/dependency/` to see all the required jars – user3338098 Jul 19 '18 at 22:07
2

This is what I do (2022 answer), go to https://mvnrepository.com/, search for your .jar and click on here: enter image description here

Ivan Carcamo
  • 288
  • 1
  • 5
1

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
0

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))
Lova Chittumuri
  • 2,427
  • 1
  • 25
  • 27