在我的 maven2 项目中,我有一个目录${basedir}/autogen
,其中包含一些由wsdl2java
.
运行时mvn compile
,由于存在重复的类,我得到一个编译错误${basedir}/autogen
。这是真实的。但是编译阶段在做什么${basedir}/autogen
呢?我没有告诉 maven 将此目录添加为源目录。而且似乎没有办法告诉 maven 忽略该目录。
在我的 maven2 项目中,我有一个目录${basedir}/autogen
,其中包含一些由wsdl2java
.
运行时mvn compile
,由于存在重复的类,我得到一个编译错误${basedir}/autogen
。这是真实的。但是编译阶段在做什么${basedir}/autogen
呢?我没有告诉 maven 将此目录添加为源目录。而且似乎没有办法告诉 maven 忽略该目录。
我在使用maven-processor-plugin的时候遇到了同样的问题,发现解决方法是配置maven-compiler插件如下:
<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 表示编译发生时没有注释处理,因此没有重复的类(通常在 generate-sources 阶段生成)
我希望这会有所帮助。
我已经看过几次了。在几乎所有情况下,这是由于生成的类被添加到主 src 树然后检查到版本控制中。
就我而言,它在我更改源目录时起作用。
新的 POM 看起来像,
<build>
<sourceDirectory>src</sourceDirectory>
仅指向带有 sourceDirectory 标记的 src 文件夹。
早些时候是
<build>
<sourceDirectory>.</sourceDirectory>
请注意,之前它在 IntellIJ 中工作,但不在 cmd 上。现在它适用于两者。
我有完全相同的问题。就我而言,问题是我用-f=./pom.xml
. 我不知道为什么这会导致不同的结果(如果有人可以解释会很好)但可能很高兴知道其他人是否有同样的问题。
JPA 模型生成器也有类似的问题。它发生在这种依赖关系上:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
<version>2.1.1</version>
</dependency>
我错误地添加了 scope=provided 并导致:
[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_
我通过从我的 pom.xml 中删除 generateAsync 来解决它,GWT 插件看起来像
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwtVersion}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
<!-- <goal>i18n</goal> -->
</goals>
</execution>
</executions>
很难更改默认的 maven 行为,我认为最好使用它 - 您可以使用 maven wsdl2java-maven-plugin生成这些文件
这里的所有答案都没有帮助。这可能是正确的答案:
另一位 StackOverflow 用户写道:
我发现 JetBrains 团队成员的评论指出:
IDEA 会自动排除 build 'target' 文件夹,前提是它下面没有生成的源,否则它会排除除生成的所有子文件夹之外的所有子文件夹。
generated-sources
文件夹中的标准 Avro 。maven 不会忽略此文件夹,因为其中生成的类将被视为重复。默认情况下,Maven 只会忽略该target
文件夹。
要修复在以下添加此行pom.xml
:
<sourceDirectory>${project.basedir}/src/main/target/resources/avro</sourceDirectory>
语境:
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>${avro.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/target/resources/avro</sourceDirectory>
<stringType>String</stringType>
<createSetters>false</createSetters>
<outputDirectory>${project.basedir}/target/</outputDirectory><enableDecimalLogicalType>true</enableDecimalLogicalType>
<fieldVisibility>private</fieldVisibility>
</configuration>
</execution>
</executions>
这会将文件夹放在generated-resources
文件夹下target
。
我的情况对此有所帮助:
<!-- 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>
我解决了同样的问题
- 清洁 Maven 项目:-
mvn clean
- 从 src 中删除 com 文件夹然后编译
- 将 com 从生成的复制到 src->main-->java
- 再次编译
希望这个帮助..