10

I have two module A and B. Actualy B is plugin to A.
B depends on A in compile time. A NOT depend on B. On A runtime I want to add B to classpath, so in A's pom.xml I add the following dependency

pom.xml

    <dependency>
        <groupId>my_group</groupId>
        <artifactId>my_Plugin</artifactId>
        <version>${project.version}</version>
        <scope>runtime</scope>
    </dependency> 

Maven process fail with cyclic dependency error

[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='A'}' and 'Vertex{label='B'}' introduces to cycle in the graph B-->A-->B -> [Help 1]

[ERROR]

Why runtime dependency impact compile time ?

maba
  • 47,113
  • 10
  • 108
  • 118
user1500951
  • 101
  • 1
  • 3
  • 1
    There's a good approach to arranging your build for this kind of project. If you refactor out the code that is shared into another module (C), project B will no longer need to depend on A at compile time - A and B can both depend on C. Then it's not a problem to have A depend on B at runtime. A good way to think of this is to consider writing a library - a bunch of sharable code - and an application, which is a collection of assembled libraries. An application project doesn't actually need to contain any code at all. – Conan Jul 04 '12 at 08:56
  • One solution is to add the cyclic dependency into a profile. That way Maven will not complain of the cyclic dependency when you build module A. – allkenang Jan 10 '19 at 01:46

1 Answers1

1

As suggested by Conan and if possible extract your common code into a separate module in order to resolve the cyclicity. Normally, in such cases one would extract common interfaces and the core classes into a separate module which is extended by both modules which cause the cyclic dependency. You would then remove the direct dependencies on the modules which were initially in a cyclic state. Sometimes this is very hard to solve, but modularizing the code helps you figure out how to refactor your code so that it is easily re-usable.

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • 1
    The code of A is not under my responsibility, so I can't separate it. – user1500951 Jul 04 '12 at 10:03
  • Well, if it's code you or your company devised, this is the way to go. If not -- based on your example and the little that you've told us, there is not other obvious solution. – carlspring Jul 04 '12 at 10:17