Maven: downloading files from url

Maven 2Get

Maven 2 Problem Overview


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

Maven 2 Solutions


Solution 1 - Maven 2

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

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

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

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>

Solution 6 - Maven 2

You can use the download-single goal in the wagon plugin. Here is an example to download an HTML page (notice that the URL have to be split in a "directory" url and a "file name")

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>wagon-maven-plugin</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals><goal>download-single</goal></goals>
      <configuration>
        <url>http://www.mojohaus.org/wagon-maven-plugin</url>
        <fromFile>download-single-mojo.html</fromFile>
        <toFile>[my dir]/mojo-help.html</toFile>
      </configuration>
    </execution>
  </executions>
</plugin>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJin KwonView Question on Stackoverflow
Solution 1 - Maven 2Pascal ThiventView Answer on Stackoverflow
Solution 2 - Maven 2mmullerView Answer on Stackoverflow
Solution 3 - Maven 2aaronView Answer on Stackoverflow
Solution 4 - Maven 2MickaelView Answer on Stackoverflow
Solution 5 - Maven 2fraczView Answer on Stackoverflow
Solution 6 - Maven 2César GarcíaView Answer on Stackoverflow