Maven:从url下载文件

我可以从http下载一些文件,而maven的生命周期? 任何插件?

如果该文件是Maven依赖项,则可以使用具有get目标的Maven依赖项插件 。

对于任何文件,您都可以使用Antrun插件来调用Ant的Get任务 。

另一个select是maven-download-plugin ,它被精确地创build来促进这种事情。 这不是非常积极的开发和文档只提到了一个artifact目标,完全相同的事情作为dependency:get 但..如果你看的来源,你会看到,有一个WGET mojo将完成这项工作。

在任何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> 

这个插件的主要优点是caching下载和检查签名,如MD5。

请注意,这个答案已经大量更新,以反映评论中所述插件的变化。

似乎CodeHaus的wagon-maven-plugin允许通过HTTP下载文件(尽pipe这不是最初的目标)。

以下是在集成testing之前下载GlassFish zip的示例:

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

maven-antrun-plugin是一个更直接的解决scheme:

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

我想添加一些关于下载maven-plugin的内容:

  • 项目现在托pipe在GitHub上https://github.com/maven-download-plugin/maven-download-plugin
  • 它的发行版在Maven Central上可用,SNAPSHOT可在oss.sonatype.org快照库中使用
  • 与此处提到的其他build议相比,download-maven-plugin增加了以下有趣的function:caching文件(避免总是重新下载大文件)和签名validation,以确保下载得到正确的位。

如果可用,wget可以直接用于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> 

您可以使用wagon插件中的download-single目标。 下面是一个下载HTML页面的例子(请注意,URL必须被拆分成“目录”url和“文件名”)

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