java 在运行时找不到依赖项 maven
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6621563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dependency not found at runtime maven
提问by Helfdane
I'm new to maven and somewhat new to java. Tried google and related sources, but I didn't find one which resembled my situation.
我是 maven 新手,对 java 有点陌生。尝试了 google 和相关资源,但我没有找到与我的情况相似的资源。
Right now, I have maven project X
and Y
. X
can be seen as a shared library with some utilities, Y
is a simple JFrame
with a "hello world"
printed and a call to a static method in X
.
现在,我有 Maven 项目X
和Y
. X
可以看作是一个带有一些实用程序的共享库,Y
是一个简单的JFrame
带有"hello world"
打印和调用静态方法的X
.
I do a "run as maven install
" on project X
, I get a "build successful"
. I add project X
as dependency in project Y
(using the pom-editor in Eclipse, browsing the repository and locating it). I do a "run as maven package
" on project Y
, I get a "build successful"
. Upon running project Y
either via java -jar
or inspect the produced jar, project X
is missing everywhere and I get a fancy class not found exception. Eclipse finds it and there are no compile errors in the source editor.
我maven install
在项目上做了一个“运行身份” X
,我得到了一个"build successful"
. 我在项目X
中添加项目作为依赖项Y
(在 Eclipse 中使用 pom-editor,浏览存储库并找到它)。我maven package
在项目上做了一个“运行身份” Y
,我得到了一个"build successful"
. Y
通过java -jar
或检查生成的 jar运行项目时,项目X
到处都丢失了,我得到了一个奇特的类未找到异常。Eclipse 找到它并且源代码编辑器中没有编译错误。
Why is it only working in the Eclipse editor and not as jar?
为什么它只能在 Eclipse 编辑器中工作而不是在 jar 中工作?
POM:
聚甲醛:
<dependency>
<groupId>com.company.deployment.shared</groupId>
<artifactId>com.company.deployment.shared</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
回答by Helfdane
Maven doesn't produce a combined JAR file for you. What Eclipse is doing is looking at the Maven configuration and adding all the required classes / jars to your classpath for you when it runs.
Maven 不会为您生成组合的 JAR 文件。Eclipse 正在做的是查看 Maven 配置,并在运行时将所有必需的类/jar 添加到您的类路径中。
If you want to run your program from the command-line, you will need to add all the JARs manually to your classpath.
如果要从命令行运行程序,则需要手动将所有 JAR 添加到类路径中。
Alternatively, you could run your program directly from Mavenwhich should set up all your dependencies. There are a number of options depending on what you want to do, i.e. if it's an application which is meant to be run by an end-user you could look into the one-jar Maven plugin.
或者,您可以直接从 Maven运行您的程序,这应该设置您的所有依赖项。有许多选项取决于您想要做什么,即如果它是一个由最终用户运行的应用程序,您可以查看one-jar Maven plugin。
回答by Stephen C
I recommend that you take a look at the Maven shade plugin. This produces an "uber-jar" comprising your project and all of its dependencies. It can also do other things such as setting the entry point class to make your JAR file an executable JAR.
我建议您查看Maven shade plugin。这会生成一个“uber-jar”,其中包含您的项目及其所有依赖项。它还可以执行其他操作,例如设置入口点类以使 JAR 文件成为可执行 JAR。
回答by Prashant Bhate
You may also find exec-maven-pluginhelpful
您可能还会发现exec-maven-plugin很有帮助
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
mvn exec:exec -Dexec.executable="maven" [-Dexec.workingdir="/tmp"] -Dexec.args="-X myproject:dist"
If your client can not download dependencies from maven m2 repo on the fly like behind firewall or no internet connection, then you also need to package the dependencies using maven-dependency-pluginto copy all dependencies and maven-assembly-pluginto assemble dependencies
如果您的客户端无法像防火墙后面或没有互联网连接那样从 maven m2 repo 动态下载依赖项,那么您还需要使用maven-dependency-plugin来打包依赖项以复制所有依赖项并使用maven-assembly-plugin来组装依赖项
回答by Adrian Shum
The artifact produced in project Y contains only build results in project Y only, not including its dependencies.
项目 Y 中生成的工件仅包含项目 Y 中的构建结果,不包括其依赖项。
If you want to build a JAR in Y, which u can execute directly, you can consider using assembly plugin.
如果你想在Y中构建一个可以直接执行的JAR,你可以考虑使用程序集插件。
For example, the easiest way to build a uber-jar for project Y:
例如,为项目 Y 构建 uber-jar 的最简单方法:
<project>
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-all-in-one-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
</project>
Apart from a normal artifact, an assembly which contains classes etc from dependencies will be created, which is suitable to be executed by java -jar
除了普通的工件外,还将创建一个包含依赖项中的类等的程序集,它适合由 java -jar 执行
visit http://maven.apache.org/plugins/maven-assembly-plugin/for more sophisticated usage.
访问http://maven.apache.org/plugins/maven-assembly-plugin/了解更复杂的用法。
回答by David
Phil Sacre already explained the basic problem well (there basically is just no information on where to find the X.jar embedded in your Y.jar).
Phil Sacre 已经很好地解释了基本问题(基本上没有关于在哪里可以找到嵌入在您的 Y.jar 中的 X.jar 的信息)。
Additionally you can also look at the appassembler-maven-plugin(which can e.g. generate launch scripts for your Y project that already have the right classpath set) and/or the exec-maven-plugin(which you can use to e.g. directly launch Y with the right classpath using maven).
此外,您还可以查看appassembler-maven-plugin(例如,它可以为已经设置了正确类路径的 Y 项目生成启动脚本)和/或exec-maven-plugin(您可以使用它直接启动 Y使用 maven 使用正确的类路径)。
回答by JB Nizet
It doesn't work because Maven resolves dependencies when building your project, but doesn't put all the dependencies magically in your jar. You're supposed to run your app with all its dependencies in the classpath:
它不起作用,因为 Maven 在构建您的项目时解析依赖项,但不会神奇地将所有依赖项放入您的 jar 中。您应该在类路径中运行您的应用程序及其所有依赖项:
java -classpath X.jar;Y.jar com.foo.bar.Main
Or you have to customize the maven jar plugin in order to create an executable jar, as described here. And you may also use the maven assemby plugin to copy all your Y project's dependencies to the target directory, next to the generated Y.jar.
或者你有自定义的Maven Jar插件,以创建一个可执行的JAR,如所描述这里。并且您也可以使用 maven 程序集插件将所有 Y 项目的依赖项复制到目标目录,在生成的 Y.jar 旁边。