0

I have 3 maven projects say P1, P2 and P3. I have added dependency of P1 into P2 and P2 into P3. How to create standalone jar of P3? I am using Eclipse and all three Maven projects are in same workspace.

2 Answers2

0

You build project P3, and configure Maven to build a "fat jar" that contains all dependencies.

The most direct way is to build a jar-with-dependencies through the Maven assembly plugin:

See https://stackoverflow.com/a/574650/927493 for details.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • I am new to Maven. Can you please provide step by step guide? – user3770039 Jun 01 '18 at 09:28
  • Have a look at the linked answer which explains what you need to put into your pom.xml. If you have specific questions, feel free to ask. – J Fabian Meier Jun 01 '18 at 09:30
  • Then I need to go through required Maven docs as the answer is well suited for those who already know Maven. Thanks. – user3770039 Jun 01 '18 at 09:45
  • You know the pom.xml and the standard build commands (like `mvn install`), right? Then just copy and paste the _second_ xml snippet into the `` section of your pom.xml. Now every `mvn install` will create an additional jar that contains all the dependencies. – J Fabian Meier Jun 01 '18 at 09:48
  • I followed https://stackoverflow.com/questions/35217128/is-it-possible-to-build-a-java-project-only-once-using-eclipse-and-share/35359756#35359756 My P2 pom is com.test p1 0.0.1-SNAPSHOT and p3 pom is like - com.test p2 0.0.1-SNAPSHOT .I cleaned all 3 using Maven clean and run Maven build on P3 using package assembly:single. – user3770039 Jun 01 '18 at 09:58
  • Result is Failed to execute goal on project p3: Could not resolve dependencies for project – user3770039 Jun 01 '18 at 09:59
  • I do not have main class in P3 project. What should I do? – user3770039 Jun 01 '18 at 10:01
  • What do you want to do with a standalone jar if you do not have a main class? – J Fabian Meier Jun 01 '18 at 10:05
  • The jar will act as chunk of APIs which can be used in other projects – user3770039 Jun 01 '18 at 10:09
  • My advice: Don't do this. Maven has transitive dependency resolution, there is not need to bundle Apis into larger units. – J Fabian Meier Jun 01 '18 at 10:42
  • Thanks for advice. What you suggest? Should I use multiple Eclipse projects and add dependent jars in build path? – user3770039 Jun 01 '18 at 12:00
  • If a project has a Maven dependency on P3, it _automatically_ also draws P2 and P1 because these are transitive dependencies. Just make sure that the pom.xml of all projects are correct. – J Fabian Meier Jun 01 '18 at 12:04
  • P3 & P1 does not have any Maven dependency. P2 has Maven dependency. I included P1 into P2 and P2 into P3. I want to create P3 jar that automatically underneath includes P1 and P2. – user3770039 Jun 01 '18 at 12:09
  • You said in your question " I have added dependency of P1 into P2 and P2 into P3". This means that the pom.xml of P3 has a dependency on P2 and the pom.xml of P2 has a dependency on P1. What am I missing? – J Fabian Meier Jun 01 '18 at 12:11
  • P1 pom - 4.0.0 com.test p1 0.0.1-SNAPSHOT jar . – user3770039 Jun 01 '18 at 12:12
  • P2 pom - removed header section 4.0.0 com.test p2 0.0.1-SNAPSHOT jar org.seleniumhq.selenium selenium-java ${selenium.version} com.test p1 0.0.1-SNAPSHOT – user3770039 Jun 01 '18 at 12:13
  • P3 pom - 4.0.0 com.test p3 0.0.1-SNAPSHOT jar com.test p2 0.0.1-SNAPSHOT – user3770039 Jun 01 '18 at 12:15
  • Exactly. So every project that declares a dependency on P3 will get P2 and P1 as well. No need to bundle them. – J Fabian Meier Jun 01 '18 at 12:17
  • But I need to create standalone jar of P3 which will have references of P1 and P2 internally. This way I can use this jar for other projects. – user3770039 Jun 01 '18 at 12:18
  • And I still do not know why. Do these other projects _not_ use Maven? – J Fabian Meier Jun 01 '18 at 12:21
  • Yeah, could be possible. Other projects may not use Maven. – user3770039 Jun 01 '18 at 12:23
  • 1
    Ok, so if you really have a project that neither uses Maven nor Gradle (which is rare these days), then this might be an option. But you are actually the first person I ever heard of that wants to build such an "api fat jar". Please think twice before you do it. I am out. – J Fabian Meier Jun 01 '18 at 12:27
  • Thanks for your time JF. Really appreciate. – user3770039 Jun 01 '18 at 12:33
0

I recommend using the maven-shade-plugin instead of the assembly plugin, as the latest assembly plugin (3.1.0) is very slow and spends over a minute packaging some of our jars. Switching to shade plugin resolved the issue. The plugin has a rich set of configuration options, for example to create an executable jar.

Here's a basic example without any custom configuration:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
gjoranv
  • 4,376
  • 3
  • 21
  • 37
  • I added this but still failed to execute. Could not resolve dependencies of other projects. – user3770039 Jun 01 '18 at 10:26
  • 1
    Did your project build before? And again: Please don't try to construct a huge api jar. Nobody does it, and if you are a Maven newbie you probably do not understand the consequences. – J Fabian Meier Jun 01 '18 at 11:41