简体   繁体   中英

Maven: How to extract (not unpack) dependent artifacts to a separate folder from local repository

Scenario: I need to prepare a usable and concise local repository for another developer who can't access maven public repository via network. But the local repository on my computer is about 20G which contains a large part of artifacts that is not relevant to the project.

AFAIK I can use mvn dependency:resolve to download only the dependencies to a new local repository location but the network traffic is limited and I don't want to waste time waiting for the download to complete. I googled a while and I tried dependency:copy-dependencies but it only copies the jar file, not including the pom.xml files and other meta-files in the artifact.

I know this work-around is kind of against the maven spirit, but sometimes we all need to work offline.

If you have a list of all the jars, you can extract them, temporarily mv .m2, install them as per https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html , give your colleague your local .m2 and rename your old .m2 back.

Although rarely, but sometimes you will have 3rd party JARs that you need to put in your local repository for use in your builds, since they don't exist in any public repository like Maven Central. The JARs must be placed in the local repository in the correct place in order for it to be correctly picked up by Apache Maven. To make this easier, and less error prone, we have provide a goal in the maven-install-plugin which should make this relatively painless. To install a JAR in the local repository use the following command:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

If there's a pom-file as well, you can install it with the following command:

mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>

With version 2.5 of the maven-install-plugin it gets even better. If the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default. In that case, all you need to do is:

mvn install:install-file -Dfile=<path-to-file>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM