2

I have 2 projects, lets say project A and project B. Project A has a Maven dependency on Project B. Both projects contain the class Test in the same package.

Now when I import,

import com.my.package.Test;

From another class in Project A, I think (at runtime) it uses the Test class from project B rather than the class contained in the same project.

Does anyone know if this is how it is supposed to work?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
The Cat
  • 2,375
  • 6
  • 25
  • 37
  • I think the classloader decides which to load. You can't control it. – Zutty Mar 12 '13 at 10:50
  • That's exactly why we add a unique prefix to the packages (like `com.company.`). So this problem could only happen with packages in your own projects. Here you have to avoid this circumstance. – Kai Mar 12 '13 at 10:51
  • Okay I just wondered if there was a way to resolve the dependency from within the same project before the Maven dependency. I guess not :)I should just rename the package – The Cat Mar 12 '13 at 10:57

2 Answers2

4

You are not supposed to have classes with the same FQN-s (fully qualified names, for example com.foo.MyTest). Having duplicate classes will only lead to these sort of problems. Remove the class from one of the two artifacts. Then use the maven-jar-plugin to create a test-jar artifact so you can share you class between the two projects.

You can also have a look at this SO answer.

Community
  • 1
  • 1
carlspring
  • 31,231
  • 29
  • 115
  • 197
0

first classloader wins. Classloader order in maven is the same as dependency order, but you shall nbot have classes with same FQN in separate projects.

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35