0

I have a sample.jar created from a Maven project with all the dependencies (fat jar using maven assembly plugin) it requires. I use this jar in a client's application by using mvn install:install-file and including the dependency in the client application's pom.xml. This works.

But is there a way such that I do not have to build the sample.jar as a fat jar?

Instead let the client application's pom resolve the dependencies required by sample.jar as well by reading the sample.jar's pom.xml, if all of the dependencies of sample.jar are available from Maven central repo?

UPDATE:

My maven assembly plugin.

        <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>
user2761431
  • 925
  • 2
  • 11
  • 26
  • To clarify your question, is JAR being installed through `mvn install:install-file` or just using `mvn install` from the source? Depending on this, the solution might be different. Is deploying your thin jar to an artifactory a viable option? – rph Sep 12 '19 at 11:12
  • Through `mvn install:install-file` – user2761431 Sep 13 '19 at 04:12
  • Thanks for clarifying @user2761431. Using that command to install your sample-thin.jar, you should be good to go if you include the option `-useFile=pom.xml`, referencing the pom.xml of your sample-thin.jar. Under the .m2 repository where the jar gets installed, it will also generate a `.pom` file that includes all the dependencies. When you include the thin JAR in the client application, Maven should now be able to resolve the transitive dependencies, making the Fat JAR no longer required. Please checkout [my answer and comments below](https://stackoverflow.com/a/57904069/1220802). – rph Sep 13 '19 at 09:45

3 Answers3

0

In your repository of choice (Central, a proprietary Artifactory etc.), you need to upload both the pom and the (thin) jar.

When you declare a dependency in the client project, Maven will automatically look for both. It will use the pom to determine the transitive dependencies and will compile against the jar.

Michael
  • 41,989
  • 11
  • 82
  • 128
0

There are a few things to note here.

  • You didn't specify how exactly you generate the fat jar but normally maven-assembly-plugin will create an additional file next to the thin (regular) jar file it creates. You can check if that is the case by looking at your project's target folder.
  • Unless otherwise configured, maven-assembly-plugin would also make any artifact it creates a project artifact. When you run mvn install all project artifacts will be installed in the local Maven repo. That means you probably already have both the thin and the fat jar in your local Maven repo.
  • Assuming the above statements hold true in your particular case and your client project has a standard dependency record (with no special types, qualifiers, ...) it is most likely that it already uses the thin jar. You can check if that is the case by running mvn dependency:tree in your client project.
Milen Dyankov
  • 2,972
  • 14
  • 25
-1

Please check this answer. Worth adding that either generatePom or pomFile options in the mvn install command are crucial for Maven to be able to resolve transitive dependencies if you use the thin version of your jar. Alternatively, you may upload your thin jar into an artifactory, if possible, that should also do it.

Useful references:

rph
  • 2,104
  • 2
  • 13
  • 24
  • You shouldn't have to generate a pom if you already have one. OP's is already a Maven project. See the first line of your 2nd link "*There are times when you do not have a POM for a 3rd party artifact*" – Michael Sep 12 '19 at 09:53
  • Please note that three options were provided: 1. generatePom (not necessary in this case), 2. pomFile, 3. artifactory deploy. Let's say the thinsample.jar is installed using `mvn install:install-file -Dfile=thinsample.jar -DgroupId=com.example -DartifactId=thinsample -Dpackaging=jar -Dversion=1.0`. The generated `.pom` won't include any dependency information. If that was the case, pomFile option needs to be present referencing the original pom.xml. Sometimes, uploading to an artifactory is not a viable option, that's why we have all these choices. – rph Sep 12 '19 at 10:35