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

前端 未结 11 793
太阳男子
太阳男子 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:14

    One could use dependency:copy (http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html) which takes a list of artifacts defined in the plugin configuration section and copies them to a specified location, renaming them or stripping the version if desired. This goal can resolve the artifacts from remote repositories if they don't exist in either the local repository or the reactor.

    Not all the properties of the plugin could be used in maven CLI. The properties which have "User Property:" property defined could be specified. In the below example I am downloading junit to my temp folder and stripping the vesion from the jar file.

    mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=junit:junit:4.11 -DoutputDirectory=/tmp -Dmdep.stripVersion=true
    

    where artifact=junit:junit:4.11 is the maven coordinates. And you specify artifcat as groupId:artifactId:version[:packaging[:classifier]]

    (Thanks to Pascal Thivent for providing his https://stackoverflow.com/a/18632876/2509415 in the first place. I am adding another answer)

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

    LATEST is deprecated, try with range [,)

    ./mvnw org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \  
    -DremoteRepositories=repoId::default::https://nexus/repository/maven-releases/ \
    "-Dartifact=com.acme:foo:[,)"
    
    0 讨论(0)
  • 2020-11-28 01:23

    Regarding how to get the artifact binary, Pascal Thivent's answer is it, but to also get the artifact sources jar, we can use:

    mvn dependency:get -Dartifact=groupId:artifactId:version:jar:sources
    

    e.g.

    mvn dependency:get -Dartifact=junit:junit:4.12:jar:sources
    

    This works because the artifact parameter actually consists of groupId:artifactId:version[:packaging][:classifier]. Just the packaging and classifier are optional.

    With jar as packaging and sources as classifier, the maven dependency plugin understands we're asking for the sources jar, not the artifact jar.

    Unfortunately for now sources jar files cannot be downloaded transitively, which does make sense, but ideally I do believe it can also respect the option downloadSources just like the maven eclipse plugin does.

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

    With the latest version (2.8) of the Maven Dependency Plugin, downloading an artifact from the Maven Central Repository is as simple as:

    mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]
    

    where groupId:artifactId:version, etc. are the Maven coordinates

    An example, tested with Maven 2.0.9, Maven 2.2.1, and Maven 3.0.4:

    mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.hibernate:hibernate-entitymanager:3.4.0.GA:jar:sources
    

    (Thanks to Pascal Thivent for providing his wonderful answer in the first place. I am adding another answer, because it wouldn't fit in a comment and it would be too extensive for an edit.)

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

    The usage from the official documentation:

    https://maven.apache.org/plugins/maven-dependency-plugin/usage.html#dependency:get
    

    For my case, see the answer below:

    mvn dependency:get -Dartifact=$2:$3:$4:$5 -DremoteRepositories=$1 -Dtransitive=false
    mvn dependency:copy -Dartifact=$2:$3:$4:$5 -DremoteRepositories=$1 -Dtransitive=false -DoutputDirectory=$6
    
    #mvn dependency:get -Dartifact=com.huya.mtp:hynswup:1.0.88-SNAPSHOT:jar -DremoteRepositories=http://nexus.google.com:8081/repository/maven-snapshots/ -Dtransitive=false
    #mvn dependency:copy -Dartifact=com.huya.mtp:hynswup:1.0.88-SNAPSHOT:jar -DremoteRepositories=http://nexus.google.com:8081/repository/maven-snapshots/ -Dtransitive=false -DoutputDirectory=.
    

    Use the command mvn dependency:get to download the specific artifact and use the command mvn dependency:copy to copy the downloaded artifact to the destination directory -DoutputDirectory.

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

    Here's what worked for me to download the latest version of an artifact called "component.jar" with Maven 3.1.1 in the end (other suggestions did not, mostly due to maven version changes I believe)

    This actually downloads the file and copies it into the local working directory

    From bash:

    mvn dependency:get \
        -DrepoUrl=http://.../ \
            -Dartifact=com.foo.something:component:LATEST:jar \
            -Dtransitive=false \
            -Ddest=component.jar \
    
    0 讨论(0)
提交回复
热议问题