简体   繁体   English

Maven在运行时添加依赖项

[英]Maven add dependencies on runtime

I am trying to compile and run this java project using maven. 我正在尝试使用maven编译并运行此Java项目。 For this, Ive had to use a 3rd party Jar library, not found in the maven repository. 为此,Ive必须使用在maven存储库中找不到的第3方Jar库。 I've been trying to tell maven to use this library on runtime (since it works perfectly fine at compile time, as I stated it on the POM file with no issues). 我一直试图告诉Maven在运行时使用该库(因为它在编译时工作得很好,正如我在POM文件上所说的那样,没有问题)。 I've tried several things such as using the Add-Jars plugin , adding it on the POM file as an external dependency: 我已经尝试了几种方法,例如使用Add-Jars插件 ,将其作为外部依赖项添加到POM文件中:

        <!--medpost-->
    <dependency>
        <groupId>lib.medpost</groupId>
        <artifactId>medpost</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/medpost/medpost/1.0/medpost.jar</systemPath>
    </dependency>

Among other things such as trying to indicate it as a command line argument, such as the first optional argument as shown in this API . 除其他外,例如试图将其指示为命令行参数,例如此API中显示的第一个可选参数。 It still throws a ClassNotFoundException. 它仍然抛出ClassNotFoundException。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

First you need to install the custom JAR into your local Maven repository: 首先,您需要将自定义JAR安装到本地Maven存储库中:

mvn install:install-file -Dfile=medpost.jar 
    -DgroupId=lib.medpost -DartifactId=medpost -Dversion=1.0

Then just put the following standard <dependency> into your POM file: 然后只需将以下标准<dependency>放入您的POM文件中:

<dependency>
    <groupId>lib.medpost</groupId>
    <artifactId>medpost</artifactId>
    <version>1.0</version>
</dependency>

By default, the medpost.jar will be available for both compilation and runtime, so you don't need to specify a scope unless you have a special need other than what your OP stated. 默认情况下, medpost.jar将可用于编译运行时,因此除非您的OP声明有特殊需要,否则您无需指定作用域。

By the way, the likely reason you weren't having a problem during compilation is that your IDE had the JAR on its classpath. 顺便说一句,编译期间没有问题的可能原因是您的IDE的类路径中包含JAR。 But when you actually tried running it, the dependency was not available. 但是,当实际尝试运行它时,依赖项不可用。

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

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