0

I got this inherited project using Maven/m2e as the build automation tool. All nice & cool except that this project, checked out of SVN as is, is broken... meaning it fails to build, with several duplicate class errors:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.1:compile (default-compile) on project myproj: Compilation failure: Compilation failure:
[ERROR] \Users\Daniel\workspace\myproj\target\generated-sources\cxf\org\package1\services\ClassA.java:[36,7] duplicate class: org.package1.services.ClassA

Now, it's true that ClassA exists in the build environment 3 times:

c:/Users/Daniel/workspace/myproj/src/main/java/org/package1/services/ClassA.java
c:/Users/Daniel/workspace/myproj/src/main/java/org/package1/www/services/ClassA.java
c:/Users/Daniel/workspace/myproj/target/generated-sources/cxf/org/package1/services/ClassA.java

But they belong to different packages:

  • package org.package1.services;
  • package org.package1.www.services;

So, why would the compiler complain about a duplicate class?

(Or is it Maven that's complaining?)

I am not familiar with the design considerations of the original author, so any idea how to resolve these duplicates would be much appreciated.

Withheld
  • 4,603
  • 10
  • 45
  • 76

3 Answers3

5

You've got three classes, in two packages. Therefore two of the classes are in the same package. These two files:

.../myproj/src/main/java/org/package1/services/ClassA.java
.../myproj/target/generated-sources/cxf/org/package1/services/ClassA.java

... are both contributing org.package1.services.ClassA (which is the fully-qualified class name the compiler is complaining about, note). It's not clear which one you should be using, based on the information you've given.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

The fact that the class has the same simple name in different packages does not matter; a class is considered by it's full named which also contains the package name. In Java you can actually have two classes by the same full name in the same JVM as long as they are loaded by two different class loaders (this is not your case).

Now you have two classes that have the same name org.package1.services.ClassA one written by you and one generated; if you need them both and you do not want them renamed you need to use a different classloader for one of them (kind of complicated so it would be simple if you renamed your generated class).

Random42
  • 8,989
  • 6
  • 55
  • 86
1

It is not ClassA which is duplicated but org.package1.services.ClassA which is duplicated.

You need to change that the package eorg.package1.services; doesn't appear at the start of the other files.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130