maven compilation error: duplicate classes

2020-02-05 06:34发布

In my maven2 project I have a directory ${basedir}/autogen that contains some autogenerated source code files produced by wsdl2java.

When running mvn compile I get an compilation error, because of duplicate classes, that lives in ${basedir}/autogen. This is true. But what is the compilation phase doing in ${basedir}/autogen? I have not told maven to add this directory as a source directory. And there seems to be no way of telling maven to ignore the directory.

9条回答
劳资没心,怎么记你
2楼-- · 2020-02-05 06:58

I had a similar problem with JPA model generator. It occurred on this dependency:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
    <version>2.1.1</version>
</dependency>

I wrongly added the scope=provided and that resulted in:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.1:compile (default-compile) on project mocker: Compilation failure: Compilation failure:
[ERROR] \Projects\entity\MockVehicle_.java:[10,7] duplicate class: entity.MockVehicle_
查看更多
▲ chillily
3楼-- · 2020-02-05 06:58

I my case helped this:

<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>
查看更多
爷的心禁止访问
4楼-- · 2020-02-05 06:59

I've seen this a few times. In almost all cases, it is due to the generated classes being added to the main src tree then checked into version control.

查看更多
我只想做你的唯一
5楼-- · 2020-02-05 07:06

I had the same problem when using the maven-processor-plugin and found that the solution was to configure the maven-compiler plugin as follows:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>

-proc:none means that compilation takes place without annotation processing and therefore no duplicate classes (which are typically generated in the generate-sources phase)

I hope that helps.

查看更多
姐就是有狂的资本
6楼-- · 2020-02-05 07:06

In my case, it worked when I changed source directory.

New POM looks like,

<build>
        <sourceDirectory>src</sourceDirectory>

Pointing just a src folder with sourceDirectory tag.

Earlier it was

 <build>
    <sourceDirectory>.</sourceDirectory>

Note that earlier it was working in IntellIJ, but not on cmd. Now it works on both.

查看更多
戒情不戒烟
7楼-- · 2020-02-05 07:06

I had the exact same issue. In my case the problem was that I called maven with -f=./pom.xml. I have no idea why this leads to a different result (would be nice if someone can explain) but maybe good to know if someone else has the same issue.

查看更多
登录 后发表回答