简体   繁体   English

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

[英]Maven: How to include my external jar into build executable jar

I am writing Spring app and I have two external jars there.我正在编写 Spring 应用程序,那里有两个外部 jar。 It works in IDE, but when I building the executable jar with maven it fails with error java.lang.NoClassDefFoundError: to my external jars.它在 IDE 中工作,但是当我使用 maven 构建可执行 jar 时,它失败并出现错误 java.lang.NoClassDefFoundError: to my external jars。 How can I solve this?我该如何解决这个问题? My dependency in pom file is:我在 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>

I build it just with men package.我只用 men 包构建它。

If your external jars are not present in the central maven repo, you can always add them to your local maven repo with below command如果您的外部 jars 不存在于中央 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]

You can then add proper dependencies for these external jars in your pom.xml.然后,您可以在 pom.xml 中为这些外部 jar 添加适当的依赖项。

Hope this helps希望这可以帮助

Add the jar to maven local repository first.首先将jar添加到maven本地存储库。 mvn install:install-file -Dfile="path of jar" -DgroupId="com.external.jar" -DartifactId="externalJar" -Dversion="version of your jar" -Dpackaging=jar In pom.xml of your project add the dependency - <dependency> <groupId>com.external.jar</groupid> <artifactId>externalJar</artifactId> <version>version of your jar</version> </dependency> Reference 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> 参考

Add the maven-assembly-plugin with jar-with-dependencies descriptorRef as specified below in your pom.xml <buid><plugins>...</plugins></build> section.在 pom.xml <buid><plugins>...</plugins></build>部分中添加带有jar-with-dependencies descriptorRefmaven-assembly-plugin It creates the uber jar with all required dependencies.它创建具有所有必需依赖项的uber jar See the configuration section 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>

You can also use the maven-shade-plugin as described here .您还可以使用此处描述的maven-shade-plugin

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

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