Maven:如何将我的外部jar包含到构建可执行jar中

时间:2015-11-27 22:14:02

标签: java spring maven jar

我正在编写Spring应用程序,我有两个外部jar。它在IDE中工作,但是当我用maven构建可执行jar时,它失败了,错误java.lang.NoClassDefFoundError:到我的外部jar。我用Google搜索,但我仍然不知道该怎么做。请帮我。我对pom文件的依赖是:

<dependency>
            <groupId>com.myapp.myappawsprovider</groupId>
            <artifactId>MyAppProvider</artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
            <systemPath>/Users/Projects/Java/MyApp/MyAppProvider/target/MyAppProvider Provider-1.0.0.jar</systemPath>
        </dependency>

我用男士包装来构建它。

3 个答案:

答案 0 :(得分:5)

如果您的外部罐子不在中央maven仓库中,您可以随时使用以下命令将它们添加到您当地的maven仓库

mvn install:install-file -DlocalRepositoryPath=[path of local maven repo] -DcreateChecksum=true -Dpackaging=jar -Dfile=[jar file path] -DgroupId=[xxx] -DartifactId=[yyy] -Dversion=[zzz]

然后,您可以在pom.xml中为这些外部jar添加适当的依赖项。

希望这有帮助

答案 1 :(得分:0)

首先将jar添加到maven本地存储库。 mvn install:install-file -Dfile="path of jar" -DgroupId="com.external.jar" -DartifactId="externalJar" -Dversion="version of your jar" -Dpackaging=jar在项目的pom.xml中添加依赖项 -      <dependency> <groupId>com.external.jar</groupid> <artifactId>externalJar</artifactId> <version>version of your jar</version> </dependency> Reference

答案 2 :(得分:0)

在pom.xml maven-assembly-plugin部分添加jar-with-dependencies descriptorRef <buid><plugins>...</plugins></build>,如下所示。它创建具有所有必需依赖项的uber jar。 请参阅配置部分here

<build>
   <plugins> 
       <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

您还可以使用here所述的maven-shade-plugin

相关问题