开发者

Maven: download artifact and its deps to a specific directory

开发者 https://www.devze.com 2023-02-14 03:39 出处:网络
I must be missing something.I have searched and searched, and played and tinkered, and I still can\'t figure out how to do the following with Maven:

I must be missing something. I have searched and searched, and played and tinkered, and I still can't figure out how to do the following with Maven:

I would like to download an artifact and all its dependencies (and transitive dependencies), from our internal Nexus server, into a user-specified location. The idea here is 开发者_如何学Cto allow the person who is deploying the solution into production a way that they can easily get all the jar files they need in one place.

There is dependency:get, and this is close-but-no-cigar. With dependency:get, all the artifacts are downloaded into the local mvn repository, under directories according to each artifact's groupId and artifactId. This is NOT what I want, because then you have to trudge around all those directories to get at the jars. I want all the files downloaded to one directory so that they are in one place.

Then there is dependency:copy-dependencies. This again does almost what I want; it copies all of an artifact's deps into target/dependency. The two problems with this are 1) You need to have a pom.xml; you can't specify arbitrary coordinates like you can with dependency:get, and 2) dependency:copy-dependencies does't copy the main artifact itself into target/dependencies.

There must be a better way to do this, but I can't figure out where else to look for a solution. To summarize, I want to be able to given someone a set of maven coordinates (groupId:artifactId:version) and our internal Nexus URL, and have them download everything with one command into a directory of their choosing.


You can combine the use of dependency:copy and dependency:copy-dependencies to achieve your goal.

The idea is simple:

  1. Use dependency:copy to fetch the pom.xml of your starting artifact.
  2. Use dependency:copy-dependencies feeding it the pom.xml you fetched in the previous step to fetch all of the starting artifact's dependencies.
  3. Use dependency:copy again to fetch the starting artifact itself.
#!/bin/sh

if [ "$#" -ne 2 ]; then
    echo "Usage: $(basename $0) <artifact> <directory>" > /dev/stderr
    exit 1
fi

ARTIFACT="$1"
OUTPUT="$2"

mkdir -p "$OUTPUT"

TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT

mvn dependency:copy              "-DoutputDirectory=${TMP}"    "-Dartifact=${ARTIFACT}:pom"

POM=$(find "$TMP" -type f)

mvn dependency:copy              "-DoutputDirectory=${OUTPUT}" "-Dartifact=${ARTIFACT}"
mvn dependency:copy-dependencies "-DoutputDirectory=${OUTPUT}" -f "$POM" -DincludeScope=runtime

Save as download-jars and use like this:

download-jars org.apache.hadoop:hadoop-aws:2.7.3 /tmp/jars


Use the maven assembly plugin to package an additional "jar with dependencies" into a ZIP file that includes everything.

http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <!-- TODO: a jarjar format would be better -->
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Then the user can just request <type>zip</type>, in addition to the regular 'maven coordinates' to get a zip file with all the dependencies.


If you "want to given someone a set of maven coordinates" it would be the best to put them in a special pom.xml (you have to write them down anywhere). This pom is not the pom of your "main artifact" but has the "main artifact" as dependency. The packaging type can be pom as this project will not create any artifcat itself.

Then use the dependency:copy-dependencies solution that you already evaluated and you will get all the dependencies you need. IMHO a elegant and simple solution. I do not know any better.

0

精彩评论

暂无评论...
验证码 换一张
取 消