Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable caching of Gradle 'ResolveTask' - remove compatibility to Gradle versions prior to 3.2 #3321

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions modules/swagger-gradle-plugin/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# swagger-gradle-plugin

**`swagger-gradle-plugin` supports gradle 3.2 and higher.**

## Installation
### Gradle 2.1 and higher
### Gradle 3.2 and higher

```
plugins {
Expand All @@ -10,6 +12,10 @@ plugins {
```
### Gradle 1.x and 2.0

**NOTE**: Since version `2.0.10` gradle 1.x and 2.x up to 3.1 are not supported.

with versions up to `2.0.9`:

```
buildscript {
repositories {
Expand All @@ -29,7 +35,7 @@ apply plugin: "io.swagger.core.v3.swagger-gradle-plugin"
### resolve

* Resolves project openAPI specification and saves the result in JSON, YAML or both formats.
All parameters except `outputFileName`, `outputFormat`, `classpath`, `skip`, `encoding` and `outputPath` correspond
All parameters except `outputFileName`, `outputFormat`, `classpath`, `skip`, `encoding`, `outputDir` and `outputPath` correspond
to `swagger` [configuration property](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#configuration-properties) with same name.

#### Example Usage
Expand All @@ -42,15 +48,15 @@ resolve {
prettyPrint = 'TRUE'
classpath = sourceSets.main.runtimeClasspath
resourcePackages = ['io.test']
outputPath = 'test'
outputDir = file('test')
}
```

#### Parameters
Parameter | Description | Required | Default
--------- | ----------- | --------- | -------
`classpath`|classpath for resources to scan (swagger and deps already included in classpath)|true|
`outputPath`|output path where file(s) are saved|true|
`outputDir`|output path where file(s) are saved|true|
`outputFileName`|file name (no extension)|false|`openapi`
`outputFormat`|file format (`JSON`, `YAML`, `JSONANDYAML`|false|`JSON`
`skip`|if `TRUE` skip execution|false|`FALSE`
Expand All @@ -67,6 +73,7 @@ Parameter | Description | Required | Default
`objectMapperProcessorClass`|see [configuration property](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#configuration-properties)|false|
`modelConverterClasses`|see [configuration property](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#configuration-properties)|false|
`contextId`|see [Context](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#context)|false|
`outputPath`|**DEPRECATED** output path where file(s) are saved|false|


**Note** parameter `openApiFile` corresponds to [config](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#configuration-properties) openAPI. It points to a location of a file in YAML or JSON format representing the input spec that will be merged with the resolved spec. Typically used to add Info section, or any other meta data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.slf4j.Logger;

Expand All @@ -22,15 +25,16 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@CacheableTask
public class ResolveTask extends DefaultTask {

private static Logger LOGGER = Logging.getLogger(ResolveTask.class);

public enum Format {JSON, YAML, JSONANDYAML};
Expand Down Expand Up @@ -72,6 +76,7 @@ public String getOutputFileName() {

@InputFile
@Optional
@PathSensitive(PathSensitivity.RELATIVE)
public File getOpenApiFile() {
return openApiFile;
}
Expand All @@ -81,7 +86,6 @@ public void setOpenApiFile(File openApiFile) {
}

@Classpath
@InputFiles
public Iterable<File> getClasspath() {
return classpath;
}
Expand All @@ -91,7 +95,6 @@ public void setClasspath(Iterable<File> classpath) {
}

@Classpath
@InputFiles
@Optional
public Iterable<File> getBuildClasspath() {
return buildClasspath;
Expand All @@ -105,17 +108,25 @@ public void setOutputFileName(String outputFileName) {
this.outputFileName = outputFileName;
}

@Input
/**
* @deprecated Use {@linkplain #getOutputDir()} instead.
*/
@Internal
@Deprecated
public String getOutputPath() {
return outputPath;
}

/**
* @deprecated Use {@linkplain #setOutputDir(File)} instead.
*/
@Internal
@Deprecated
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
outputDir = new File(outputPath);
}


@OutputDirectory
public File getOutputDir() {
return outputDir;
Expand Down Expand Up @@ -223,7 +234,6 @@ public void setContextId(String contextId) {
this.contextId = contextId;
}


@Input
@Optional
public String getScannerClass() {
Expand Down Expand Up @@ -284,7 +294,6 @@ public void setEncoding(String resourceClasses) {
this.encoding = encoding;
}


@TaskAction
public void resolve() throws GradleException {
if (skip) {
Expand Down Expand Up @@ -387,17 +396,12 @@ public void resolve() throws GradleException {
method=swaggerLoaderClass.getDeclaredMethod("resolve");
Map<String, String> specs = (Map<String, String>)method.invoke(swaggerLoader);

Path path = Paths.get(outputPath, "temp");
final File parentFile = path.toFile().getParentFile();
if (parentFile != null) {
parentFile.mkdirs();
}
if (specs.get("JSON") != null) {
path = Paths.get(outputPath, outputFileName + ".json");
Path path = outputDir.toPath().resolve(String.format("%s.json", outputFileName));
Files.write(path, specs.get("JSON").getBytes(Charset.forName(encoding)));
}
if (specs.get("YAML") != null) {
path = Paths.get(outputPath, outputFileName + ".yaml");
Path path = outputDir.toPath().resolve(String.format("%s.yaml", outputFileName));
Files.write(path, specs.get("YAML").getBytes(Charset.forName(encoding)));
}
} catch (IOException e) {
Expand Down