简体   繁体   中英

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 . 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.

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.

First I thought about compiling the custom project with all the dependencies into a shaded 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. I've got one Java package that is a dependency of the main project jar and of the custom project jar as well.

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 ?

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.

You can build shaded (or fat) jars to run them as standalone applications, but don't put them into the <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). 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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