KP13
KP13

Reputation: 68

Circular dependencies in Maven and horrible structure of project

I have a problem with maven. I have a huge multi-module project with limited possibilities to change the structure of it.

So, let's say I have 3 modules: A,B,C.

For now, there aren't any problems.

Unfortunately, B uses C during the runtime (spring, ioc, ...), so someone added C as B's dependency, so we have a horrible cycle in Maven. Build finishes with failure (something like "cycle detected" in log).

I would like to keep it this way (provide somehow C dependency in B module) as I need to compile and deliver B with all needed JAR archives (including JARs from C).

Can I somehow build C and copy its JARs to B's target directory after the full compilation of B? Is there a plugin or tool which can be used by maven to do this?

If this post is not clear, I will try to describe it in more details.

Thanks in advance ;)

Upvotes: 2

Views: 3788

Answers (2)

Rob
Rob

Reputation: 6497

It sounds like there are two issues:

  1. How to tell Maven about a runtime-only dependency between artifacts.
  2. How to get Maven to leverage the dependency to pull together all the relevant dependencies.

Maven has a feature of dependency management that allows you to inform maven of such nuances using the "scope" element of the dependency element. In this case, I think you want

<scope>runtime</scope>

See the documentation. In particular:

runtime - This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

Proper utilization of the runtime scope should resolve the circular dependency issue.

Regarding the second issue, you have not supplied enough information to provide a definitive answer. However, you almost certainly want to use a maven plugin in order to leverage maven's information about the dependencies. For example, if you want to produce a single "fat" jar that contains everything, you want to look at maven-shade-plugin. Another option is maven-assembly-plugin, which is extremely flexible and can include all dependencies in the assembly. There are other plugins that excel at handling various other common circumstances. You may want to formulate a separate question if you have problems with how to use a specific plugin.

Upvotes: 1

zakaria amine
zakaria amine

Reputation: 3682

Here is how I would do it. From C's pom file:

 <profile>
    <id>compile</id>
      <dependencies>
        <dependency>
           use B here but do not use in the main dependencies section
        </dependency>
          + other dependencies
      </dependencies>
  </build>
</profile>

For example you can compile C module using mvn compile -Pcompile

From B's pom file:

 <profile>
    <id>run</id>
      <dependencies>
        <dependency>
           use C here but do not use in the main dependencies section
        </dependency>
          + other dependencies
      </dependencies>
  </build>
</profile>

you run B module using mvn yourcommandforrunning -Prun

In this way you can escape the cyclic dependencies issue.

Upvotes: 2

Related Questions