简体   繁体   English

如何从多模块Maven项目添加依赖项

[英]How to add a dependency from a multi-module maven project

Ive got a multi-module maven setup like the following, 我有一个如下所示的多模块Maven设置,

+- pom.xml
+- module1/
   +- pom.xml
+- module2/
   +- pom.xml

module2 produces an executable jar along with its dependencies copied to lib/ directory. module2生成一个可执行jar,并将其依赖项复制到lib/目录。 The pom.xml for this contains, 为此的pom.xml包含,

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/module2/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <includeScope>runtime</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.test.JksUtility</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>.</Class-Path>
            </manifestEntries>
        </archive>
        <outputDirectory>${project.build.directory}/module2</outputDirectory>
    </configuration>
</plugin>

The problem becomes apparent when module1 references module2 as a dependency. module1引用module2作为依赖项时,该问题变得很明显。 In module1 's pom.xml contains, module1pom.xml中,

<dependency>
    <groupId>com.test</groupId>
    <artifactId>jksutil</artifactId>
    <version>${jksutil.version}</version>
</dependency>

When I try to run module1 it tells me that it cannot find any dependencies of module2 as it expects all the jars under lib/ directory in .m2 repository. 当我尝试运行module1它告诉我它找不到module2任何依赖项,因为它期望.m2存储库中lib/目录下的所有jar。 This is due to classpathPrefix defined above. 这是由于上面定义的classpathPrefix

For instance I would get the following error for a missing dependency, 例如,对于缺少的依赖项,我将收到以下错误消息,

java.io.FileNotFoundException: /Users/test/.m2/repository/com/test/jksutil/1.2.1-SNAPSHOT/lib/argparse4j-0.7.0.jar (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:219)
    at java.util.zip.ZipFile.<init>(ZipFile.java:149)
    at java.util.jar.JarFile.<init>(JarFile.java:166)
    at java.util.jar.JarFile.<init>(JarFile.java:130)
    at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:60)
    at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49)
    at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:338)
    at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:288)
    at org.apache.jasper.servlet.TldScanner.scanJars(TldScanner.java:262)
    at org.apache.jasper.servlet.TldScanner.scan(TldScanner.java:104)
    at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:101)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Is there anyway around this other than creating a fat/uber jar ? 除了创建一个胖/胖的罐子以外,还有其他解决办法吗?

请尝试使用Maven Shade Plugin来构建可执行jar https://maven.apache.org/plugins/maven-shade-plugin/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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