How can I download a specific Maven artifact in one command line?

前端 未结 11 794
太阳男子
太阳男子 2020-11-28 01:05

I can install an artifact by install:install-file, but how can I download an artifact?

For example:

mvn download:download-file -Dgroup         


        
相关标签:
11条回答
  • 2020-11-28 01:25

    one liner to download latest maven artifact without mvn:

    curl -O -J -L  "https://repository.sonatype.org/service/local/artifact/maven/content?r=central-proxy&g=io.staticcdn.sdk&a=staticcdn-sdk-standalone-optimizer&e=zip&v=LATEST"
    
    0 讨论(0)
  • 2020-11-28 01:29

    Here's an example to get ASM-7 using Maven 3.6:

    mvn dependency:get -DremoteRepositories=maven.apache.org -Dartifact=org.ow2.asm:7.0:sources:jar
    

    Or you can download the jar from here: https://search.maven.org/search?q=g:org.ow2.asm%20AND%20a:asm and then

    mvn install:install-file -DgroupId=org.ow2.asm -DartifactId=asm -Dversion=7.0 -Dclassifier=sources -Dpackaging=jar -Dfile=/path/to/asm-7.0.jar
    
    0 讨论(0)
  • 2020-11-28 01:31

    To copy artifact in specified location use copy instead of get.

    mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.2:copy \
      -DrepoUrl=someRepositoryUrl \
      -Dartifact="com.acme:foo:RELEASE:jar" -Dmdep.stripVersion -DoutputDirectory=/tmp/
    
    0 讨论(0)
  • 2020-11-28 01:32

    You could use the maven dependency plugin which has a nice dependency:get goal since version 2.1. No need for a pom, everything happens on the command line.

    To make sure to find the dependency:get goal, you need to explicitly tell maven to use the version 2.1, i.e. you need to use the fully qualified name of the plugin, including the version:

    mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
        -DrepoUrl=url \
        -Dartifact=groupId:artifactId:version
    

    UPDATE: With older versions of Maven (prior to 2.1), it is possible to run dependency:get normally (without using the fully qualified name and version) by forcing your copy of maven to use a given version of a plugin.

    This can be done as follows:

    1. Add the following line within the <settings> element of your ~/.m2/settings.xml file:

    <usePluginRegistry>true</usePluginRegistry>
    

    2. Add the file ~/.m2/plugin-registry.xml with the following contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <pluginRegistry xsi:schemaLocation="http://maven.apache.org/PLUGIN_REGISTRY/1.0.0 http://maven.apache.org/xsd/plugin-registry-1.0.0.xsd"
    xmlns="http://maven.apache.org/PLUGIN_REGISTRY/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <useVersion>2.1</useVersion>
          <rejectedVersions/>
        </plugin>
      </plugins>
    </pluginRegistry>
    

    But this doesn't seem to work anymore with maven 2.1/2.2. Actually, according to the Introduction to the Plugin Registry, features of the plugin-registry.xml have been redesigned (for portability) and the plugin registry is currently in a semi-dormant state within Maven 2. So I think we have to use the long name for now (when using the plugin without a pom, which is the idea behind dependency:get).

    0 讨论(0)
  • 2020-11-28 01:32

    The command:

    mvn install:install-file 
    

    Typically installs the artifact in your local repository, so you shouldn't need to download it. However, if you want to share your artifact with others, you will need to deploy the artifact to a central repository see the deploy plugin for more details.

    Additionally adding a dependency to your POM will automatically fetch any third-party artifacts you need when you build your project. I.e. This will download the artifact from the central repository.

    0 讨论(0)
提交回复
热议问题