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

时间:2017-09-13 10:09:29

标签: java maven build maven-3

我有一个如下所示的多模块maven设置,

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

module2生成一个可执行jar及其依赖项复制到lib/目录。 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>

module1引用module2作为依赖项时,问题就变得明显了。在module1&#39; s pom.xml包含,

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

当我尝试运行module1时,它告诉我它找不到module2的任何依赖项,因为它需要lib/存储库中.m2目录下的所有jar。这是由于上面定义的classpathPrefix

例如,对于缺少的依赖项,我会收到以下错误,

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)

除了创造一个胖子/超级罐子之外还有吗?

1 个答案:

答案 0 :(得分:0)

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