Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: downloading files from url

Tags:

maven-2

get

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

like image 896
Jin Kwon Avatar asked Apr 30 '10 01:04

Jin Kwon


People also ask

Why is Maven downloading from Central?

Whenever you run build job, maven first try to find dependency from local repository. If it is not there, then, by default, maven will trigger the download from this central repository location. To override this default location, you can can make changes to your settings.

What is Maven download plugin?

Download Plugin for Maven This is a plugin meant to help maven user to download different files on different protocol in part of maven build. For the first implementation, there will only be a goal that will help downloading a maven artifact from the command line.

Does Maven download dependencies automatically?

When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build.

Does Maven download jar files?

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.


2 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.

like image 155
Pascal Thivent Avatar answered Sep 18 '22 16:09

Pascal Thivent


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> 
like image 43
mmuller Avatar answered Sep 19 '22 16:09

mmuller