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.

4

3 回答 3

5

你有三个类,在两个包中。因此,其中两个类同一个包中。这两个文件:

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

...都是有贡献org.package1.services.ClassA的(这是编译器抱怨的完全限定的类名,请注意)。根据您提供的信息,尚不清楚您应该使用哪一个。

于 2012-12-06T13:55:54.537 回答
3

类在不同包中具有相同的简单名称这一事实并不重要;一个类由它的全名来考虑,全名也包含包名。在 Java 中,您实际上可以在同一个 JVM 中拥有两个具有相同名的类,只要它们由两个不同的类加载器加载(这不是您的情况)。

现在您有两个具有相同名称的类,org.package1.services.ClassA一个是您编写的,一个是生成的;如果您同时需要它们并且不希望它们重命名,则需要为其中一个使用不同的类加载器(有点复杂,因此如果重命名生成的类会很简单)。

于 2012-12-06T14:00:58.183 回答
1

不是ClassA哪个被复制,而是org.package1.services.ClassA哪个被复制。

您需要更改它package eorg.package1.services;不会出现在其他文件的开头。

于 2012-12-06T13:56:38.353 回答