简体   繁体   English

Maven依赖项不会添加到Eclipse的jar中

[英]The maven dependencies are not added to the jar in eclipse

I'm coding a maven project with eclipse 2018.09 under java 11 and I've a problem with the maven jar creation. 我正在用Java 11下的eclipse 2018.09编写一个maven项目,并且maven jar创建存在问题。 When I clean package the project, it delivers me a jar but no dependencies are added into and i sometimes have warning in eclipse like: 当我清理打包项目时,它为我提供了一个jar,但没有添加任何依赖项,有时我会在eclipse中发出警告,例如:

classpath entry junit(for example) will not be exported it may result a ClassNotFoundException. classpath条目junit(例如)将不会导出,可能会导致ClassNotFoundException。

Which is in fact what's happening when i launch my jar project. 实际上,这是我启动jar项目时发生的情况。

Thanks. 谢谢。

it delivers me a jar but no dependencies are added into [it] 它为我提供了一个jar,但没有将任何依赖项添加到[it]

it is totally normal. 这是完全正常的。 By default, when Maven builds a jar, it does not add any dependencies in it, but only the .class and resources of your current project. 默认情况下,当Maven生成jar时,它不会在其中添加任何依赖关系,而只会添加当前项目的.class和资源。

When you run your programm you want it to find your dependencies otherwise you will face ClassNotFoundException. 当您运行程序时,您希望它找到您的依赖项,否则您将面临ClassNotFoundException。 So you must configure your classpath to reference the dependencies. 因此,您必须配置您的类路径以引用依赖项。

1- if you want to run you programm from your local computer with Maven, use the exec Maven plugin with the <java> goal defined in your pom like explained here: https://www.mojohaus.org/exec-maven-plugin/usage.html#Java_goal 1-如果要使用Maven在本地计算机上运行程序,请使用exec Maven插件,在您的pom中定义<java>目标,如下所示: https : //www.mojohaus.org/exec-maven-plugin /usage.html#Java_goal

alternatively you can run it from a launcher in your IDE. 或者,您可以从IDE中的启动器运行它。 The IDE will build the classpath for you and the classpath will corectly contain your dependencies. IDE将为您构建类路径,并且该类路径将完全包含您的依赖项。

2- if you want to run from the command line on any computer, you have to copy all of you dependencies in one directory (using Maven's dependency plugin mvn dependency:copy ) and run you jar like this: 2-如果要在任何计算机上从命令行运行,则必须将所有依赖项复制到一个目录中(使用Maven的依赖项插件mvn dependency:copy ),然后像这样运行jar:

java -cp myProgram.jar:dependencyDirectory/* com.blabla.MainClass

(beware the use of ';' or ':' and '/' or '\\' depending on Linux/Windows) (请注意,根据Linux / Windows使用';'或':'和'/'或'\\')

3- as an alternative you can run your jar with java -jar myprogram.jar but only if it contains a correct MANIFEST.MF where the location of all the dependencies are hardcoded. 3-或者,您可以使用java -jar myprogram.jar运行jar,但前提是它包含正确的MANIFEST.MF,其中所有依赖项的位置都经过硬编码。

My advice is to target solution 1 or 2 first. 我的建议是首先针对解决方案1或2。

PS: you can also create "fat jars" or "uber jars" containing your dependencies but I would advise you do not target this solution at first. PS:您还可以创建包含依赖项的“胖罐”或“超罐”,但我建议您首先不要针对此解决方案。

You can simply add this to your pom.xml (under the < plugins > tag): 您可以简单地将其添加到pom.xml中(在<plugins>标记下):

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Remember to change the mainclass to your entrypoint (which class the static void main(string[args]) is). 请记住将mainclass更改为您的入口点( static void main(string[args])是哪个类)。 Now when you run the command mvn clean install there will be a jar in the targets folder with name yourproject-version-SNAPSHOT-jar-with-dependencies.jar 现在,当您运行命令mvn clean install时, targets文件夹中将存在一个名为yourproject-version-SNAPSHOT-jar-with-dependencies.jar

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

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