Can I download some files from http while maven lifecycle? any plugin?
5 Answers
If the file is a Maven dependency, you could use the Maven Dependency Plugin which has a get
goal.
For any file, you could use the Antrun plugin to call Ant's Get task.
Another option would be the maven-download-plugin, it has been precisely created to facilitate this kind of things. It's not very actively developed and the documentation only mentions an artifact
goal that does exactly the same thing as dependency:get
but.. If you look at the sources, you'll see that is has a WGet mojo that will do the job.
Use it like this in any POM:
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<!-- the wget goal actually binds itself to this phase by default -->
<phase>process-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>http://url/to/some/file</url>
<outputFileName>foo.bar</outputFileName>
<!-- default target location, just to demonstrate the parameter -->
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Key benefits of this plugin are caching of the download and checking against a signature, such as MD5.
Note that this answer has been heavily updated to reflect changes in the plugin as noted in the comments.

- 60,927
- 15
- 95
- 117

- 562,542
- 136
- 1,062
- 1,124
-
4+1 It is also possible to check MD5 sum eg:
3921c19528d180902939b9f4c9ac92f1 – Michal Zmuda Mar 17 '14 at 19:57 -
3Though it might be obvious to many people but executing `mvn process-resources` from command line will start this download – Madhav May 10 '17 at 21:26
-
I have some issues with this one, I want that when I ran mvn clean-install, the downloaded files will be included in the jar As I tried this answer, whenever I download (the file is not available) the file will be downloaded however not included in the jar. – Aaron Dec 07 '17 at 06:54
-
1how to download multiple files – kozla13 Apr 04 '18 at 15:44
Seems like wagon-maven-plugin from CodeHaus allows to download files over HTTP (though this is not is original goal).
Here is an example downloading GlassFish zip before integration tests:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>download-glassfish</id>
<phase>pre-integration-test</phase>
<goals>
<goal>download-single</goal>
</goals>
<configuration>
<url>http://download.java.net</url>
<fromFile>glassfish/3.1/release/glassfish-3.1.zip</fromFile>
<toDir>${project.build.directory}/glassfish</toDir>
</configuration>
</execution>
</executions>
</plugin>
-
2Does it cache the files in the local repository like maven-download-plugin? – Zoltán Jan 13 '14 at 12:45
-
-
As a maven committer, I can recommend this answer (and the ant answer). Mojohaus plugins are maintained by mostly the same people that maintain maven core. This goal is not OS dependent. But the version is outdated, 2.0.2 is the current version: https://mvnrepository.com/artifact/org.codehaus.mojo/wagon-maven-plugin/2.0.2 – Benjamin Marwell Mar 29 '23 at 08:42
The maven-antrun-plugin is a more direct solution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>download-files</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- download file -->
<get src="http://url/to/some/file"
dest="${project.build.directory}/downloads/"
verbose="false"
usetimestamp="true"/>
</target>
</configuration>
</execution>
</executions>
</plugin>

- 713
- 9
- 9
-
2I don't know if it's the version of the plugin I used or what, but in the
tag if I used – Hardy Apr 15 '16 at 22:02nothing happened, but when I changed it to the started working. Examples here helped: https://maven.apache.org/guides/mini/guide-using-ant.html -
2Something really weird is happening. When the download is complete, I was expecting a file inside the downloads directory. But the result is that "downloads" is created as a file with the same extension of the downloaded file. And when I open this "downloads" file, you found one unique file named "downloads" too. – Dherik Nov 28 '17 at 16:14
I'd like to add a few thing about the download-maven-plugin:
- Project is now hosted on GitHub https://github.com/maven-download-plugin/maven-download-plugin
- Its releases are available on Maven Central, and the SNAPSHOTs are available on the oss.sonatype.org snapshot repository
- Compared to other suggestions mentioned here, the download-maven-plugin adds the following interesting feature: caching of files (to avoid always redownloading big files) and signature verification to make sure download got the right bits.

- 3,506
- 1
- 21
- 33
-
A plugin of which app exactly? Eclipse? and why a plugin instead of a standAlone? – android developer Oct 01 '14 at 08:41
-
1A plugin for Maven. Click on the link about and see README with examples. – Mickael Oct 01 '14 at 09:34
-
Wait, so you download maven, and then a plugin for maven that's called "maven" too? – android developer Oct 01 '14 at 09:58
-
1See README. This question is about how to download a file from Maven, this is a plugin that allows to download a file with Maven... – Mickael Oct 01 '14 at 10:17
-
yes, i know. I wonder if there's a tool to just download from there without all these steps. – android developer Oct 01 '14 at 13:29
If available, wget can be used directly with exec-maven-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>wget</executable>
<arguments>
<argument>http://example.com/file.zip</argument>
<argument>destination.zip</argument>
</arguments>
</configuration>
</plugin>

- 20,536
- 18
- 103
- 149
-
4This is only possible if wget itself is available on the machine where Maven is running. For example if running on Windows, wget will not be available. – Adam Burley Nov 01 '16 at 18:33
-
@AdamBurley it actually works well on windows without the need of having wget installed – Ruslan López Feb 19 '20 at 09:42
-
@RuslanLópez how can it possibly work without wget being installed? all exec-maven-plugin does is executes a command on the local system. if the program is not found, the command can't execute. – Adam Burley Feb 23 '20 at 22:23
-
@AdamBurley you are correct, however it uses an Apache library, not WGet, Please check the code in https://github.com/maven-download-plugin/maven-download-plugin/blob/master/src/main/java/com/googlecode/download/maven/plugin/internal/HttpFileRequester.java – Ruslan López Feb 24 '20 at 01:43
-
And here you can find my working commit: https://github.com/maven-download-plugin/maven-download-plugin/issues/154 – Ruslan López Feb 24 '20 at 01:43
-
That is for `maven-download-plugin`. This answer is talking about `exec-maven-plugin`. They are two totally different plugins. – Adam Burley Feb 27 '20 at 13:14