简体   繁体   English

如何在我的Maven最终构建和安装的.JAR文件中包含依赖项二进制文件?

[英]How are dependency binaries included in my final built and installed .JAR in Maven?

1) I included a Spring Context dependency in my pom.xml project in Eclipse with Maven. 1)我在使用Maven的Eclipse的pom.xml项目中加入了Spring Context依赖项。

2) I ran the 'Install' phase on the project and it built properly, and the project was installed to my local .m2 repository 2)我在项目上运行了“安装”阶段,并正确构建了该项目,并将该项目安装到了本地.m2存储库中

3) When I unzipped my .JAR, I only saw my single class that I created custom. 3)解压缩.JAR时,只看到创建自定义的单个类。

This brings up two questions: 这带来了两个问题:

1) Are external, dependency classes only included in your final built jars if a class from it is physically instantiated within your class? 1)如果外部依赖项类仅在您的类中被物理实例化,则它们是否仅包含在最终构建的jar中?

and

2) How come, when I imported the SpringContextAnnotationConfig class into my class, and instantiated an instance of it, and installed my project, I STILL only saw my custom class when I unzipped my .JAR. 2)怎么了,当我将SpringContextAnnotationConfig类导入到我的类中,并实例化它的实例,并安装我的项目时,我仍然只能在解压缩.JAR时看到我的自定义类。 Is this unusual? 这不寻常吗? Or is the SpringContextAnnotationConfig now just written into the .class binary so that when I deploy my .jar to another JVM, it hass all its dependencies within my custom built .class binary? 还是现在只是将SpringContextAnnotationConfig写入.class二进制文件中,以便当我将.jar部署到另一个JVM时,它的所有依赖项都位于我自定义的.class二进制文件中?

Thanks 谢谢

SOLUTION : 解决方案

The problem was that I was expecting maven to do the same for a JAR output as it would for a WAR. 问题是我期望Maven对JAR输出所做的与对WAR所做的相同。 When using the webapp archetype to generate a WAR file, Maven automatically packaged the required dependency jars into the WEB-INF directory. 使用webapp原型生成WAR文件时,Maven自动将所需的依赖项jar打包到WEB-INF目录中。

I was basically trying to understand how a container on a remote, brand new server would run my classes without having the dependency binaries. 我基本上是想了解远程全新服务器上的容器如何在没有依赖二进制文件的情况下运行我的类。 I was attempting to run a maven built to produce a JAR file, which did not end up including my dependencies. 我试图运行构建为生成JAR文件的Maven,但最终并没有包括我的依赖项。 However, when I ran maven install to build a WAR file, my dependencies were all there ready for deployment. 但是,当我运行maven install生成WAR文件时,我的依赖项就可以部署了。

  1. No, they are never included (unless you use a special plugin which does that). 不,它们从不包括在内(除非您使用执行此操作的特殊插件)。

  2. See 1. 请参阅1。

If you add this artifact as a dependency to some other project, its dependencies (and their dependencies, etc.) will be automatically added (this is controllable, so you can eg exclude them or change the version). 如果您将此工件添加为对其他项目的依赖项,则会自动添加其依赖项(及其依赖项等)(这是可控制的,因此您可以例如排除它们或更改版本)。 But they are taken from pom.xml , not from the .jar itself. 但是它们来自pom.xml ,而不是.jar本身。 This also allows not to download same libraries a huge number of times. 这也允许不大量下载相同的库。

For building fat jars, see eg How can I create an executable JAR with dependencies using Maven? 有关构建胖罐子的信息,请参见例如如何使用Maven创建具有依赖项的可执行JAR? . Specifically for Spring you may want Spring Boot . 专门针对Spring,您可能需要Spring Boot

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

相关问题 为什么在最终.war中找不到我包含在.jar中的maven依赖项 - Why is my maven dependency in included .jar not found in final .war Maven:我的项目具有的依赖关系不在任何Maven存储库(系统范围)中。 它是否包含在我的最终罐子中? - Maven: My project has a dependency that is NOT in any maven repo (system scope). Does it get included in my final jar? 如何从不是用Maven构建的jar中将类作为Maven依赖项排除在外? - How to exclude a class as a maven dependency from a jar that is not built with maven? Maven 不使用较新的本地构建/安装依赖项 - Maven not using newer locally built/installed dependency Maven为包含的JAR依赖关系定义子依赖关系 - Maven define sub-dependency for included JAR dependency Maven:如何将本地jar打包到最终jar中? - Maven: How to package local jar into final jar? 如何将.jar依赖项包含到ANT目标中,以生成应用程序的最终.jar文件? - How to include a .jar dependency into the ANT target that generate my final .jar file of my application? 如何在运行 maven 可执行文件 jar 时重用我的本地依赖项? - How to reuse my local dependency while running maven executable jar? Maven 阴影仅将依赖项(3 个中的 2 个)中的某些类着色为最终 jar - Maven shade is only shading some classes from the dependency (2 of 3) into final jar 如何在Maven中添加jar依赖项? - how to add jar dependency in maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM