简体   繁体   English

Maven-如何使用共同的依赖项管理多个jar

[英]Maven - how to manage multiple jar with dependencies in common

I've got a big project which is composed of several smaller projects, each one with a pom.xml . 我有一个大项目,它由几个较小的项目组成,每个项目都有一个pom.xml Some projects are dependencies of others. 一些项目是其他项目的依赖项。 The way I made it work is in producing a few independent shaded jar and war files thanks to maven. 我的工作方式是通过maven生成一些独立的有阴影的jarwar文件。

The problem I'm encountering is that I've got a new custom project that is dependent on classes from the main project. 我遇到的问题是我有一个新的自定义项目,该项目依赖于主项目中的类。 The matter is that this project contains a custom class and, therefore, the resulting jar file is added afterward to the app. 问题在于该项目包含一个自定义类,因此,生成的jar文件随后添加到应用程序中。

First I thought about compiling the custom project with all the dependencies into a shaded jar . 首先,我考虑将具有所有依赖项的自定义项目编译到一个有阴影的jar However, when running the app, I got a casting error: 但是,在运行应用程序时,出现强制转换错误:

java.lang.ClassCastException: com.some.class cannot be cast to com.another.class

I guess the problem comes from the fact that some dependencies are double. 我猜问题出在某些依赖是双重的。 The com.another.class exists in two shaded jar and therefore it is not the same in the custom project jar and the main project one. com.another.class存在于两个有阴影的jar ,因此在自定义项目jar和主项目中是不同的。 I've got one Java package that is a dependency of the main project jar and of the custom project jar as well. 我有一个Java包,它是主项目jar和自定义项目jar的依赖项。

Does somebody have an idea of how to make it work? 有人对如何使其工作有想法吗? Is there a way to don't include the dependencies in the custom project jar and make it clear that it needs to seek the dependencies into another jar ? 有没有一种方法可以不将依赖项包含在自定义项目jar ,并明确表明需要将依赖项查找到另一个jar

I hope I've been able to make myself understood. 我希望我能够使自己理解。

Thanks! 谢谢!

The main thing: Shaded jars are not meant to be dependencies of other jars. 最主要的是:带阴影的jar并不意味着是其他jar的依赖项。

You can build shaded (or fat) jars to run them as standalone applications, but don't put them into the <dependencies> . 您可以构建有阴影(或胖)的jar使其作为独立的应用程序运行,但不要将其放入<dependencies>

If you need classes in several projects, put them into a project or module and compile them as usual jar (not a shaded one). 如果需要在多个项目中使用类,请将它们放入项目或模块中,然后像平常的jar一样编译它们(而不是阴影对象)。 Then you can use it as dependency. 然后,您可以将其用作依赖项。

You can exclude sub-dependencies in a dependency: 您可以在依赖项中排除子依赖项:

<dependency>
  <groupId>my.group</groupId>
  <artifactId>my-artifact</artifactId>
  <version>1.2.3</version>
  <exclusions>
    <exclusion>
      <artifactId>xercesImpl</artifactId>
      <groupId>xerces</groupId>
    </exclusion>
    <exclusion>
      <artifactId>xmlParserAPIs</artifactId>
      <groupId>xerces</groupId>
    </exclusion>
  </exclusions>
</dependency>

Another way is to select which artifacts go into your shaded jar. 另一种方法是选择将哪些工件放入您的着色罐中。 So you can include a group and except some specific artifacts. 因此,除了一些特定的工件外,您可以包括一个组。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <createSourcesJar>true</createSourcesJar>
        <minimizeJar>false</minimizeJar>
        <artifactSet>
            <includes>
                <include>org.vaadin.*:*</include>
                <include>com.vaadin:*</include>
                <include>com.vaadin.external.atmosphere:*</include>
                <include>com.vaadin.external.slf4j:*</include>
                <include>com.github.*:*</include>
                <include>fi.jasoft:dragdroplayouts</include>
            </includes>
            <excludes>
                <exclude>com.vaadin:vaadin-client-compiler</exclude>
                <exclude>com.vaadin:vaadin-client</exclude>
                <exclude>com.vaadin:vaadin-theme-compiler</exclude>
            </excludes>
        </artifactSet>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

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

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