4

maven-dependency-plugin:copy copies a single jar. I need to copy the jar and the pom to the appropriately named path in accordance with repository structure.

Use case: My project has a local repository, where I keep non-public dependencies. The dependencies are projects that I build on my machine and which get installed to the local machine's repository. After I copy the artifacts to the project's local repository, I can build the project on any machine which doesn't have the sources of the other projects. Using a project local repository is easy. You just need a folder with a particular directory structure and include this in the POM:

<repositories>
    <repository>
        <id>in-project</id>
        <name>In Project Repo</name>
        <url>file://${project.basedir}/libs</url>
    </repository>
</repositories>

However, copying stuff into the local repo is an extra step I'd like to avoid. maven-dependency-plugin:copy almost does what I need.

<build>                
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-private-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>...</groupId>
                                <artifactId>...</artifactId>
                            </artifactItem>
                        </artifactItems>
                        <outputDirectory>${project.basedir}/libs</outputDirectory>
                        <overWriteReleases>true</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>            
    </plugins>
</build>

But it just copies the dependency's jar, without the pom and without the necessary directory structure.

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
  • What would you like to achieve? – khmarbaise Jun 04 '14 at 06:17
  • You are talking about 'local repository for your project' sound you should start using a repository manager will make your life easier. – khmarbaise Jun 04 '14 at 06:30
  • Maybe you are looking for `mvn deploy` command. – Aleksandr M Jun 04 '14 at 09:41
  • 3
    @khmarbaise a repository manager is nice, but not what i need. the repo manager assumes all build machines are in the same network. bundling libs with the source is more flexible in some respects. – Aleksandr Dubinsky Jun 04 '14 at 12:23
  • @aleksandrm mvn deploy can do it, but it's essentially a push. I 'd have to list every folder of every project that uses the artifact being built. i want the dependent projects to pull it instead. – Aleksandr Dubinsky Jun 04 '14 at 12:26
  • @AleksandrDubinsky: Correct me if I'm wrong. There is a project A which you want to build and copy it jar and pom into another project B lib folder? If so why do you need pom there? – Aleksandr M Jun 04 '14 at 12:33
  • 1
    Take a look at the [appassembler-maven-plugin](http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/create-repository-mojo.html) and the `create-repository` goal. – khmarbaise Jun 04 '14 at 13:19
  • @AleksandrM Project B is Maven-based, and it needs the pom to resolve the dependency. No? – Aleksandr Dubinsky Jun 04 '14 at 13:36
  • @AleksandrDubinsky: OK project B is maven project also. But still, copying a jar is a push too essentially. And where from project B will pull its dependencies if you say it should work from any machine? From some local folder inside it? Then you have to copy to multiple folders if you have more projects. No? – Aleksandr M Jun 04 '14 at 17:46
  • @AleksandrM I suppose the maven plugin should update the project's local repository only if it detects that a newer artifact exists in some other accessible repository. In effect, it'll only update on my dev machine. – Aleksandr Dubinsky Jun 04 '14 at 18:49
  • 1
    @AleksandrM A common problem I face is: An open-source dependency has a bug. I checkout its source, fix the bug, modify its version, build, and install to machine's local repo (and, of course, publish the issue to the project's devs). I copy the fixed lib into my project's local repo and commit to my VCS. Everything is nice and neat. I don't want to publish the patched dependency to some internet-accessible maven repo. – Aleksandr Dubinsky Jun 04 '14 at 18:59
  • @AleksandrDubinsky: 1. If you have multiple projects that depend on that patched project you need to copy it to each of them and commit to your VCS (yuck...) 2. Is your VCS accessible from internet? How that different from maven repo? Conclusion: you need a company maven repo (e.g. Artifactory). – Aleksandr M Jun 04 '14 at 21:19
  • 2
    @AleksandrM I like having a self-contained folder that can build my project, regardless of whether a private Maven repo is up. I may even like to have all the dependencies live in the project's dir, so no network connection is ever required. There's an elegeance to that. Granted, a Maven server makes sense the larger the organization and the more of these shared projects there are. – Aleksandr Dubinsky Jun 05 '14 at 15:22

2 Answers2

4

Use the maven-dependency-plugin:copy-dependencies goal instead. If useRepositoryLayout and copyPom properties are set to true, it will create such a local repository.

The goal can be called from the command line as:

mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.2:copy-dependencies -Dmdep.useRepositoryLayout=true -Dmdep.copyPom=true -DoutputDirectory=./repository-exported
Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
jens p
  • 56
  • 3
1

I have written a proof-of-concept about that. You can try it out:

https://github.com/Jintin/syndle

For example, if you like to download "group:name:version" at "jcenter.bintray.com" into your local Maven repository, you can execute the following command:

syndle clone -p group:name:version -s https://jcenter.bintray.com
Blauharley
  • 4,186
  • 6
  • 28
  • 47
Jintin
  • 1,426
  • 13
  • 22