Maven: downloading files from url

2019-01-13 09:32发布

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

标签: maven-2 get
6条回答
Viruses.
2楼-- · 2019-01-13 10:12

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>
查看更多
干净又极端
3楼-- · 2019-01-13 10:15

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>
查看更多
聊天终结者
4楼-- · 2019-01-13 10:18

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.

查看更多
Root(大扎)
5楼-- · 2019-01-13 10:19

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.
查看更多
放荡不羁爱自由
6楼-- · 2019-01-13 10:22

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>
查看更多
何必那么认真
7楼-- · 2019-01-13 10:30

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>
查看更多
登录 后发表回答