0

My Maven project Project1 has only one dependency which is the Utils(which is again my local Maven project). I have to distribute my project as a Standalone jar to be used in a client application. I used the following maven assembly plugin to generate the fat jar.

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>myMainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

This fat jar now contains all the dependencies of Utils project packaged. This will bring in the dependency resolution issues if the client application also uses a Maven dependency, but has a different version.

Is there a way such that I can package the Utils without its dependencies and let Maven download the Utils dependencies at the client location?

My Project1 dependencies:

<dependencies>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>Utils</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies> 

My Utils dependencies:

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-parameter-names</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.9.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${version.log4j}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${version.log4j}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${version.log4j}</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
    </dependency>
 </dependencies>
user2761431
  • 925
  • 2
  • 11
  • 26
  • that is not good idea, rather build dependencies in separate /ext/lib and your main jar separately. hence you can deploy dependencies once and your project jar file whenever its required – vels4j Sep 13 '19 at 05:07
  • You should always package the dependencies with your application instead of making it download from else where unless it is really dependent of different latest version of 3'rd party libraries – Sohan Sep 13 '19 at 06:20

2 Answers2

0

Usually, you use fat jars (jar-with-dependencies) for standalone applications, not for jars that are meant as dependencies.

Otherwise you risk exactly the problems you described. The best way to offer a dependency is to put it in a Maven Repository that is available for your users (either one you maintain on your own, or something like MavenCentral). Then the users can just declare your <repository>, put the <dependency> in their project and all transitive resolution is done by Maven.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
-1

The process once you imported the Maven project into an another computer is that the Maven executable will download the dependencies for the project on the internet again if that dependency has not been downloaded yet on that computer? How? Maven will look at the C:\Users\<user>\.m2 (Windows) or ~\.m2 (Linux) directory if that JAR already exists in that directory. If not yet existing, then Maven will download each dependency that is not yet there.