简体   繁体   中英

How to copy dependencies of a multi module project?

I have a project that depends on a different project-module in my workspace. I'm trying to copy all dependencies (including the module) to a lib folder for creating an executable jar that has no packaged all jars inside itself.

But maven-dependency-plugin keeps complaining that it could not copy the module classes to the target folder of my project. What might be wrong?

my.groupt my-module 1.0

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
       <execution>
          <id>copy-dependencies</id>
          <phase>package</phase>
          <goals>
             <goal>copy-dependencies</goal>
          </goals>
          <configuration>
             <outputDirectory>${project.build.directory}/lib</outputDirectory>
          </configuration>
       </execution>
    </executions>
 </plugin>

Result:

Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.1:copy-dependencies (copy-dependencies) on project my-project: Error copying artifact from C:\\workspace\\my-module\\target\\classes to C:\\workspace\\my-project\\target\\lib\\classes: (Access denied)

The reason for your failure is workspace resolution in eclipse. Eclipse m2e injects itself into the artifact resolution of maven.

So when your my-project tries to get all dependencies, the artifact resolver returns not the jar file (which does not yet exist), but the classes folder of your dependent project.

If you try to build your project using "run as -> maven install", it should work.

So this is a scenario that you cannot comfortably resolve in workspace. Either turn of workspace resolution for my-project, disable the dependency plugin inside your eclipse (lifecycle bindings) or use a different plugin like assembly to copy your depdencies (which is cleaner, btw.). Note however, that the latter will also only work if maven is called manually.

You are probably looking for the Maven Shade plugin.

It is much more flexible but be warned - any module built through Maven Shade makes automatic tracing of dependencies very difficult. Try not to make your shaded result the primary outcome of the build process.

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