如何让maven在编译时只考虑一组重复文件中的一个?

How to make maven only consider one of a set of duplicate files when compiling?

我正在使用 Swagger (OpenAPI) 来设计和实现 API。我已经有一个功能性的 swagger.yml 和一个 Maven 项目集来基于它构建文档、服务器和客户端。

swagger-codegen-maven-plugin/target/generated-sources/swagger 目录中生成摘要 类 和实现 类。我应该用自己的实现覆盖 类,所以我将实现 类 的初始版本移动到 /src/main/java 目录。

移动 类 后,我的构建正确完成,但如果我执行 mvn clean compile 我会收到错误消息,因为我的构建中有重复的 类。

每次我进行干净构建时,swagger-codegen-maven-plugin 都会重新生成所有代码,包括 类 我正在重写的代码。

swagger-codegen-maven-plugin 创建一个 .swagger-codegen-ignore ,我应该在其中列出我不希望它生成的文件,但它似乎不起作用(我使用的是版本2.2.2-插件的SNAPSHOT).

#.swagger-codegen-ignore
OrcamentoApiServiceImpl.java
PropostaApiServiceImpl.java

作为一种变通方法,我试图让 maven-compiler-plugin 排除文件的生成版本,这样只有我修改后的实现才会用于编译,但它也不起作用。这是我正在使用的配置:

<build> 
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <excludes>
                            <exclude>OrcamentoApiServiceImpl.java</exclude>
                            <exclude>PropostaApiServiceImpl.java</exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如何让 maven-compiler-plugin 忽略由 swagger Maven 插件生成的文件的版本并只考虑我的版本?

或者:您能否提出替代解决方案,或者如何让 swagger-codegen-maven-plugin 考虑到 .swagger-codegen-ignore

我遇到了完全相同的问题,我设法让 swagger 忽略文件工作。根据我从他们的文档中了解到的内容(通过适当的示例可能会更清楚),忽略文件必须设置在生成代码的根目录中。所以我在我的 pom.xml 中添加了这个插件,以便从它的实际位置复制它:

  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/generated-sources/swagger</outputDirectory>
          <resources>
            <resource>
              <directory>${basedir}/src/main/resources</directory>
              <includes>
                <include>.swagger-codegen-ignore</include>
              </includes>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>