Maven: downloading files from url

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


Solution 1:

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.

Solution 2:

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>

Solution 3:

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>

Solution 4:

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.

Solution 5:

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>