gvdm
gvdm

Reputation: 3166

Maven way to include a runtime dependency when packaging

I have two Maven projects, A and B, where A depends on B at compile time, but at runtime B needs some classes of A.

What I did is:

A's pom.xml

<dependency>
  <groupId>B</groupId>
  <artifactId>B</artifactId>
  <version>${B.version}</version>
</dependency>

B's pom.xml

<dependency>
  <groupId>A</groupId>
  <artifactId>A</artifactId>
  <version>${A.version}</version>
  <scope>runtime</scope>
</dependency>

When letting Jenkins compile the projects it fails to compile each other as a downstream project because it finds the circular dependency and avoid the infinite build loop.

So, what I thought is a way to add the A's runtime dependency only when packaging B (when Jenkins executes mvn package) so that Jenkins does not find the circular dependencies in the pom.xml files and configures the downstream compilation.

Is there any way to accomplish this with an existing Maven plugin or other way?

Thank you

Upvotes: 0

Views: 2015

Answers (2)

Andrea Scarpino
Andrea Scarpino

Reputation: 801

I wrote this Maven plugin for the same reason.

It adds any listed JAR artifact to the WAR file where this plugin is used. JAR dependencies are resolved and added to the WAR file if not artifact with the same version is found.

Upvotes: 3

Karol Dowbecki
Karol Dowbecki

Reputation: 44962

The important thing is to define dependencies only inside the <dependency> POM section because that's the only configuration used by the Maven reactor.

If you fiddle with custom plugins to introduce your own dependency management ideas you will most likely break the reactor. Even if your custom approach works with regular mvn clean install it will usually explode when -T4 or similar option is used to enable multi threaded builds. There is simply no way to explicitly define the module build order in POM as it's governed by the reactor.

The usual way of sharing code between modules is to create a new module C which is depended on by both A and B.

Upvotes: 2

Related Questions