Gradle, SpringBoot & Swagger [Part 1]
Yes… I know it's been a long time since I've last written. I must continue a post about Docker, however i learned new stuff during these days and as a friend of mine, Felipe Belluco told me once:
I think it is interesting that when we learn a new command or something new we should create a simple documentation.
That phrase reminds me of a problem that I have been trough with Gradle some time ago. As a “good programmer” I created a test project, however I forgot to version the project.
I've been trough the same problem but in a different context and when I searched for a solution I had written, I found out I had deleted the project.
The lesson of this story is that I lost more time searching for the solution again.
Leaving this sad story behind and in order to not repeating the same mistakes again I decided to document something interesting I've been working on: Swagger.
I wrote about Swagger here and here but what changed about this new post was the development process with Gradle and the discovery about a plug-in that does exactly what I was looking for: Java classes generation through a JSON or YML contracts. For a minute I thought I hadn't had searched correctly because when I complained about the thing, it was 2016 June 9th however the first commit on plug-in was 2016 October 1st. That was how I discovered gradle-swagger-generator-plugin.
I still want to write a post on why I changed Maven to Gradle. For now, let's continue.
All source code can be found at https://github.com/arthurfnsc/contractfirst-swagger.
I begun my project by getting Pet store API contract provide by Swagger as example in Swagger Editor. Finally the problem with contract-first/ API-first is solved in REST world!
The project I am working on is a multi-project Gradle. In order to not leting this post so long I will keep the main focus only on Java classes generation through a contract. I will soon write a post about multi-project Gradle configuration I am using.
Following build.gradle configuration file:
I added a directory folder inside src/main/resources calls swagger with petstore-api.json and config.json the first one is the contract and the second one some configuration to generate Java classes.
Following config.json file:
Don't pay attention to SpringFox, we will go further into it in the second part of this post.
The more important build.gradle task is generateSwaggerCode. In which task I described the API file access, configurations, besides to I want generate contract models and API. I also described that I wanted to use Spring Boot. If you want more specifications take a look here.
The problem that motivates me writing this post is copySwaggerGeneratedCode task. I have found a way to directly export Java classes to src/main/java however as I said before I lost the code.
Inside generateSwaggerCode task there is a variable called outputFile that by default points to ${buildDir}/swagger-code. I tried, without success to change its value to $projectDir and in the end I created a task that copies files after generateSwaggerCode task invocation.
You can execute gradle generateSwaggerCode and after a bit your classes will be created inside specified folder.
In this example I also used generateSwaggerDoc task which is a very interesting local HTML generating API documentation, however, it's not so interesting as the documentation that we will generate with Swagger in the next post.
See you there…