0

Problem

  • In java, I have a a "Util project" using another "Mock project" when doing unit test.
  • My problem is that the "Mock Project" is as well using the "Util project" to build some of the Mock object.
  • When i use maven to build my projects, i can't build it cause one project miss the jar from the second and reverse case for the other project.

Example

  • As you can see in the example below, it make sense that both project needs each other and each piece of code is located in the right project, what is "Mock" is in "Mock" project, what is "Util" is in "Util" project.
public class TestProjectUtil
{
   @Test myMethod()
   {
       //some code
       GeneratedEntity obj = ProjectMockUtil.generateEntity();
   }
}

public class ProjectMockUtil
{
   public static EntityObj generateEntity()
   {
       //Some code
       EntityObj obj = new EntityObj();
       MethodList names = ProjectUtil.Reflection.getMethodList(obj);

       //Some code
   }
}

Question

  • How should you deal with this type of situation. I tried to force maven to still build my project and ignore failure but as soon as one class fail to compile then the generated jar does not include any class at all, so the jar is empty.
  • As well i do not believe that a refactoring is ultimately needed in my case, the different classes are in the right projects and i do not want to duplicate my code for the sake of having the same class in both project to satisfy maven and make it work.

What might be the best approach ?

fra
  • 83
  • 1
  • 11
  • `Util Project` uses `Mock Project` and using `Util Project` means you have a circular dependency. This means you have to refactor your code to solve that issue...there is no other way to refactor which will solve the circular dependency... – khmarbaise Aug 27 '19 at 20:27
  • another way to do it is to build the first project as a JAR and WITHOUT MAVEN, in this case your jar is usable by the second project when you run maven for the first time. (it will need to reference the JAR in the POM). After that you can build the first project normally with maven, then rebuild first project in maven (change reference in the POM to use dependency from local repository). And for future build always use MAVEN as before, it will work properly. – fra Aug 28 '19 at 18:53
  • Another way to do so is to merge the 2 projects together, its not always logic to do so but in my case it could be logic to merge 2 class util together and create a separation via package name, for instance first project under dev.helper.helperjse then the second project dev.helper.helpermock in this case we don't have the issue with circular reference since within a project circular reference are accepted and normal. – fra Aug 28 '19 at 18:56
  • Another way is to change the argument of the maven compilation plugin and pass argument to force compilation error jars to be added to the jar file and to not fail on error. (this one i did not find what are the arguments yet). Happy if someone knows. – fra Aug 28 '19 at 18:59

1 Answers1

0

option 1

  • Another way to do it is to build the first project as a JAR WITHOUT MAVEN, in this case your jar is usable by the second project when you run maven for the first time. (the JAR will need to be added as dependency in the POM).
  • After that you can build the first project normally with maven, then rebuild the second project again but this time change the JAR reference in the POM to use dependency from local repository (the one you just build with first project).
  • And for future build always use MAVEN as before, it will work properly.

Option 2

  • Another way to do so is to merge the 2 projects together, its not always logic to do so but in my case, it could be logic to merge 2 class util together and create a separation via package name, for instance first project under dev.helper.helperjse, then the second project dev.helper.helpermock
  • In this case we don't have the issue with circular reference since within a project circular reference are accepted and normal.

Option 3

  • Another way is to change the argument of the maven compilation plugin and pass argument to force compilation error .class to be added to the jar file and to not fail on error. (this one i did not find what are the arguments yet, Happy if someone knows).
fra
  • 83
  • 1
  • 11