3

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?

lpacheco
  • 976
  • 1
  • 14
  • 27
  • Your mistake was copying generated source into your project's src/main folders. Can you clean it out? You're digging yourself into a deeper mess by trying live with that mistake. In java you can simply `extend` an existing class to override it -- you don't have to physically change the existing class at all. – Software Engineer Dec 22 '16 at 00:10
  • I understand that as a bad smell in general, but I'm not sure I can follow that advice with code generated by swagger-codegen. Actually, I have another open question about how to deal with swagger-codegen. http://stackoverflow.com/questions/41184952/how-to-properly-use-the-server-stubs-generated-from-a-swagger-specification – lpacheco Dec 22 '16 at 12:54

1 Answers1

1

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>
Xendar
  • 466
  • 1
  • 6
  • 15
  • I understand that you solution copies the `.swagger-codegen-ignore` to the right place at the beginning of the build, but is your `maven-codegen-plugin` actually ignoring the generation of classes listed in it? Mine isn't. What version of the plugin are you using? – lpacheco Dec 26 '16 at 12:40
  • Your solution is more elegant than mine for avoiding that `clean` wipes `.swagger-codegen-ignore` (I used an exclusion for `maven-clean-plugin`), but `swagger-codegen-maven-plugin` is still generating the `*Impl.java` files. – lpacheco Dec 26 '16 at 13:02
  • 1
    Yes, the impl classes are not generated. I do not have the file at sight right now but if I'm not wrong I have set `**/*Impl.java` – Xendar Dec 27 '16 at 19:25
  • I'll try that! I didn't include any path, only the full name of the Java files. – lpacheco Dec 28 '16 at 11:31
  • Thank you, @Xendar! Using `**/*Impl.java` did it. If you create an answer, I will make it the accepted one. – lpacheco Dec 28 '16 at 17:26
  • Glad that it worked for you too! You're already commenting on an answer I think ;) – Xendar Dec 29 '16 at 08:30