From d30fa0f3dba71cf8fb9a0e1abc0b24fa2c2e3df1 Mon Sep 17 00:00:00 2001 From: oppofind <836575280@qq.com> Date: Thu, 20 Aug 2020 00:19:09 +0800 Subject: [PATCH] Add support for open api. --- build.gradle | 2 +- .../gradle/constant/GlobalConstants.java | 108 +++++++++--------- .../gradle/plugin/SmartDocPlugin.java | 5 + .../com/smartdoc/gradle/task/OpenApiTask.java | 22 ++++ 4 files changed, 83 insertions(+), 54 deletions(-) create mode 100644 src/main/java/com/smartdoc/gradle/task/OpenApiTask.java diff --git a/build.gradle b/build.gradle index f326c1a..169dd24 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,7 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.13' - compile 'com.github.shalousun:smart-doc:1.9.1' + compile 'com.github.shalousun:smart-doc:1.9.2' } tasks.withType(JavaCompile) { options.encoding = "UTF-8" diff --git a/src/main/java/com/smartdoc/gradle/constant/GlobalConstants.java b/src/main/java/com/smartdoc/gradle/constant/GlobalConstants.java index c796350..8a9ef09 100644 --- a/src/main/java/com/smartdoc/gradle/constant/GlobalConstants.java +++ b/src/main/java/com/smartdoc/gradle/constant/GlobalConstants.java @@ -1,53 +1,55 @@ -/* - * smart-doc https://github.com/shalousun/smart-doc - * - * Copyright (C) 2018-2020 smart-doc - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package com.smartdoc.gradle.constant; - -/** - * @author yu 2019/12/13. - */ -public interface GlobalConstants { - - String ERROR_MSG = "Failed to build ApiConfig, check if the configuration file is correct."; - - String DEFAULT_CONFIG = "./src/main/resources/default.json"; - - String TASK_GROUP = "Documentation"; - - String REST_HTML_TASK = "smartDocRestHtml"; - - String REST_ADOC_TASK = "smartDocRestAdoc"; - - String REST_MARKDOWN_TASK = "smartDocRestMarkdown"; - - String POSTMAN_TASK = "smartDocPostman"; - - String RPC_HTML_TASK = "smartDocRpcHtml"; - - String RPC_ADOC_TASK = "smartDocRpcAdoc"; - - String RPC_MARKDOWN_TASK = "smartDocRpcMarkdown"; - - String EXTENSION_NAME = "smartdoc"; - - String SRC_MAIN_JAVA_PATH = "/src/main/java"; -} +/* + * smart-doc https://github.com/shalousun/smart-doc + * + * Copyright (C) 2018-2020 smart-doc + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.smartdoc.gradle.constant; + +/** + * @author yu 2019/12/13. + */ +public interface GlobalConstants { + + String ERROR_MSG = "Failed to build ApiConfig, check if the configuration file is correct."; + + String DEFAULT_CONFIG = "./src/main/resources/default.json"; + + String TASK_GROUP = "Documentation"; + + String REST_HTML_TASK = "smartDocRestHtml"; + + String REST_ADOC_TASK = "smartDocRestAdoc"; + + String REST_MARKDOWN_TASK = "smartDocRestMarkdown"; + + String POSTMAN_TASK = "smartDocPostman"; + + String OPEN_API_TASK = "smartDocOpenApi"; + + String RPC_HTML_TASK = "smartDocRpcHtml"; + + String RPC_ADOC_TASK = "smartDocRpcAdoc"; + + String RPC_MARKDOWN_TASK = "smartDocRpcMarkdown"; + + String EXTENSION_NAME = "smartdoc"; + + String SRC_MAIN_JAVA_PATH = "/src/main/java"; +} diff --git a/src/main/java/com/smartdoc/gradle/plugin/SmartDocPlugin.java b/src/main/java/com/smartdoc/gradle/plugin/SmartDocPlugin.java index 8a705ec..358e215 100644 --- a/src/main/java/com/smartdoc/gradle/plugin/SmartDocPlugin.java +++ b/src/main/java/com/smartdoc/gradle/plugin/SmartDocPlugin.java @@ -59,6 +59,11 @@ public void apply(Project project) { postmanTask.setGroup(GlobalConstants.TASK_GROUP); postmanTask.dependsOn(javaCompileTask); + // create open api + OpenApiTask openApiTask = project.getTasks().create(GlobalConstants.OPEN_API_TASK, OpenApiTask.class); + openApiTask.setGroup(GlobalConstants.TASK_GROUP); + openApiTask.dependsOn(javaCompileTask); + //create rpc html RpcHtmlTask rpcHtmlTask = project.getTasks().create(GlobalConstants.RPC_HTML_TASK, RpcHtmlTask.class); rpcHtmlTask.setGroup(GlobalConstants.TASK_GROUP); diff --git a/src/main/java/com/smartdoc/gradle/task/OpenApiTask.java b/src/main/java/com/smartdoc/gradle/task/OpenApiTask.java new file mode 100644 index 0000000..06c80c9 --- /dev/null +++ b/src/main/java/com/smartdoc/gradle/task/OpenApiTask.java @@ -0,0 +1,22 @@ +package com.smartdoc.gradle.task; + +import com.power.doc.builder.OpenApiBuilder; +import com.power.doc.model.ApiConfig; +import com.thoughtworks.qdox.JavaProjectBuilder; +import org.gradle.api.logging.Logger; + +/** + * Support OpenApi 3.0+ + * @author yu 2020/8/20. + */ +public class OpenApiTask extends DocBaseTask { + + @Override + public void executeAction(ApiConfig apiConfig, JavaProjectBuilder javaProjectBuilder, Logger logger) { + try { + OpenApiBuilder.buildOpenApi(apiConfig, javaProjectBuilder); + } catch (Exception e) { + e.printStackTrace(); + } + } +}