77

Can I download some files from http while maven lifecycle? any plugin?

5 Answers 5

85

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.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 It is also possible to check MD5 sum eg: <md5>3921c19528d180902939b9f4c9ac92f1</md5>
Though it might be obvious to many people but executing mvn process-resources from command line will start this download
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.
how to download multiple files
36

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> 

3 Comments

Does it cache the files in the local repository like maven-download-plugin?
The current version of the plugin is 2.0, otherwise - this works nicely.
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: mvnrepository.com/artifact/org.codehaus.mojo/wagon-maven-plugin/…
25

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> 

2 Comments

I don't know if it's the version of the plugin I used or what, but in the <configuration> tag if I used <target> nothing happened, but when I changed it to <tasks> the <get> started working. Examples here helped: maven.apache.org/guides/mini/guide-using-ant.html
Something 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.
20

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.

5 Comments

A plugin of which app exactly? Eclipse? and why a plugin instead of a standAlone?
A plugin for Maven. Click on the link about and see README with examples.
Wait, so you download maven, and then a plugin for maven that's called "maven" too?
See README. This question is about how to download a file from Maven, this is a plugin that allows to download a file with Maven...
yes, i know. I wonder if there's a tool to just download from there without all these steps.
0

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> 

6 Comments

This 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.
@AdamBurley it actually works well on windows without the need of having wget installed
@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.
@AdamBurley you are correct, however it uses an Apache library, not WGet, Please check the code in github.com/maven-download-plugin/maven-download-plugin/blob/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.