简体   繁体   English

为什么在最终.war中找不到我包含在.jar中的maven依赖项

[英]Why is my maven dependency in included .jar not found in final .war

I have a .jar we have developed in-house. 我有一个我们内部开发的.jar。 It is built using maven. 它是使用Maven构建的。 It has a dependency, commons-codec, that is provided from the maven central repository. 它具有从Maven中央存储库提供的依赖项commons编解码器。

.jar .pom dependency ("myjar"): .jar .pom依赖项(“ myjar”):

<packaging>jar</packaging>
...
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>         
</dependency>
...

.war .pom dependency: .war .pom依赖项:

<packaging>war</packaging>
...
<dependency>
    <groupId>com.myco.mydiv</groupId>
    <artifactId>myjar</artifactId>
    <version>1.5.54</version> 
</dependency>
....

I can build and test this jar without issue. 我可以毫无问题地构建和测试此jar。 When the jar is included as a dependency in a .war, it builds without problem, but at run time, the external dependency for commons-codec produces a class def not found exception. 如果将jar作为依赖项包含在.war中,则可以毫无问题地进行构建,但是在运行时,commons-codec的外部依赖项会生成未找到的类def异常。

I tried changing the scope for commons-codec in the .jar .pom to 'compile' but this did not help. 我尝试将.jar .pom中commons编解码器的范围更改为“编译”,但这无济于事。

I can fix this by adding the commons-codec dependency to the .war .pom, but this is not the right way to fix it (I think) as it requires all projects using the .jar to know about this dependency and include it likewise. 我可以通过在.war .pom中添加commons-codec依赖关系来解决此问题,但这不是正确的解决方法(我认为),因为它要求所有使用.jar的项目都必须了解此依赖关系,并同样包含它。 I could also include the external .jar in the WEB-INF/lib to solve this, but it also seems like the incorrect approach. 我也可以在WEB-INF / lib中包含外部.jar来解决此问题,但这似乎也是错误的方法。

What is the best way to handle this? 处理此问题的最佳方法是什么? Why is the common-codec dependency not visible at run time for the .war? 为什么.war在运行时看不到通用编解码器依赖性?

This conversation seems to touch on a similar issue: https://github.com/ReactiveX/RxNetty/issues/292 这次对话似乎涉及到类似的问题: https : //github.com/ReactiveX/RxNetty/issues/292

And so I wonder if there is a right way to handle this with nested dependencies. 因此,我想知道是否存在使用嵌套依赖项处理此问题的正确方法。

But I just noticed this answer, which seems to show the right way to do this: https://stackoverflow.com/a/98743/2266428 但我只是注意到了这个答案,这似乎显示了执行此操作的正确方法: https : //stackoverflow.com/a/98743/2266428

jar-with-dependencies via the maven-assembly-plugin seems to solve the problem but at the expense of a very large .jar (6 MB in my case). 通过maven-assembly-plugin进行jar依赖似乎可以解决该问题,但要以很大的.jar(在我的情况下为6 MB)为代价。

After trying multiple approaches, it turns out the Shade plugin was exactly what was needed. 在尝试了多种方法之后,事实证明Shade插件正是需要的。 The below added plugin configuration to my .war .pom, exactly as shown, added the dependency to the jar and made it available to the .war at runtime. 如下所示,将插件配置添加到我的.war .pom中,完全如图所示,将依赖项添加到jar中,并使其在运行时可供.war使用。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <minimizeJar>true</minimizeJar>
        </configuration>
      </execution>
    </executions>
  </plugin>

Unzipping the initial .jar shows that the following path and classes are now included in the build: 解压缩初始.jar会显示以下路径和类现已包含在构建中:

org > apache > commmons > codec > *.class

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

相关问题 如何在我的Maven最终构建和安装的.JAR文件中包含依赖项二进制文件? - How are dependency binaries included in my final built and installed .JAR in Maven? 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? 为什么我的 JAR 的 Maven 依赖项没有包含在我的 WAR 中? - Why aren't the Maven dependencies of my JAR being included in my WAR? 为什么我的jar文件和我的.war文件不能在我的项目构建中找到? Maven的 - Why can't my jar files and my .war file be found within my project build? Maven 为什么 maven 无法将我的依赖项添加到 jar? - Why maven cannot add my dependency to jar? 如何判断哪个maven依赖项负责包含我的WAR文件中包含的jar? - How can I tell which maven dependency is responsible for including jars included in my WAR file? Maven 依赖项。jar 文件未找到 - Maven dependency .jar file not found Maven为包含的JAR依赖关系定义子依赖关系 - Maven define sub-dependency for included JAR dependency 运行时依赖包含在战争中 - Runtime dependency included in war 如何找出Maven包含罐子的原因? - How to find out why a jar was included by Maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM