首页 文章

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

提问于
浏览
3

我正在写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

    如果您的外部 jar 不在中央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添加适当的依赖项 .

    希望这可以帮助

  • 5

    首先将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

  • 0

    在pom.xml <buid><plugins>...</plugins></build> 部分中添加 maven-assembly-pluginjar-with-dependencies descriptorRef ,如下所示 . 它创建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>
    

    您也可以使用 maven-shade-plugin ,如here所述 .

相关问题