【发布时间】:2011-02-14 01:40:18
【问题描述】:
我可以在 maven 生命周期中从 http 下载一些文件吗?有什么插件吗?
【问题讨论】:
我可以在 maven 生命周期中从 http 下载一些文件吗?有什么插件吗?
【问题讨论】:
如果文件是 Maven 依赖项,您可以使用 Maven Dependency Plugin,它的目标是 get。
对于任何文件,你可以使用Antrun插件调用Ant的Get task。
另一个选项是maven-download-plugin,它的创建正是为了方便这种事情。它的开发不是很积极,文档只提到了一个 artifact 目标,它与 dependency:get 完全相同但是..如果你查看源代码,你会发现它有一个WGet 能胜任这项工作的魔力。
在任何 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>
此插件的主要优点是缓存下载并检查签名,例如 MD5。
请注意,此答案已经过大量更新,以反映插件中的更改,如 cmets 中所述。
【讨论】:
mvn process-resources 将开始下载
似乎来自 CodeHaus 的 wagon-maven-plugin 允许通过 HTTP 下载文件(尽管这不是最初的目标)。
这是在集成测试之前下载 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 是更直接的解决方案:
<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>
【讨论】:
我想补充几点关于 download-maven-plugin 的内容:
【讨论】:
如果可用,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>
【讨论】: