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

3.9k views Asked by At

I'm using Swagger (OpenAPI) to design and implement an API. I already have a functional swagger.yml and a Maven project set to build docs, servers and clients based on it.

The swagger-codegen-maven-plugin generate abstract classes and implementation classes in the /target/generated-sources/swagger directory. I'm supposed to overwrite the implementation classes with my own, so I moved the initial version of the implementation classes to the /src/main/java directory.

After moving the classes, my build completes correctly, but if I do a mvn clean compile I get errors for having duplicate classes in my build.

What happens is that everytime I do a clean build, swagger-codegen-maven-plugin regenerates all the code, including the classes I'm rewriting.

swagger-codegen-maven-plugin creates a .swagger-codegen-ignore where I'm supposed to list the files I don't want it to generate, but it doesn't seems to be working (I'm using version 2.2.2-SNAPSHOT of the plugin).

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

As a workaround, I'm trying to make maven-compiler-plugin exclude the generated version of the files, so that only my modified implementation will be used for compilation, but it is not working either. Here is the configuration I'm using:

<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>

How can I make maven-compiler-plugin ignore the version of the files generated by the swagger Maven plugin and consider only mine?

Or: can you suggest an alternative solution, or how to make swagger-codegen-maven-plugin take the .swagger-codegen-ignore into account?

1

There are 1 answers

6
Xendar On BEST ANSWER

I came across the exact same issue, and I managed to make the swagger ignore file work. From what I understood from their documentation (which could have been clearer with a proper example) the ignore file must be set at the root of the generated code. So I have added this plugin in my pom.xml to copy it from its actual location:

  <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>