2017-07-04 8 views
0

gradle cleanとgradle swaggerを実行すると、ClassNotFoundExceptionがスローされます。もしgradle swaggerが再び実行されれば(基本的に前の実行時にapiのビルドが行われた後)、うまく動作します。kongchen swagger-maven-pluginを使用している場合、クラスをgraleに表示しないクラスswagger

build.gradleは以下のようになります。

buildscript { 
    repositories { 
     maven { url hydraMavenRepo } 
     maven { url hydraPluginsRepo } 
    } 
    dependencies { 
     classpath "com.github.kongchen:swagger-maven-plugin:3.1.4" 
    } 
} 

apply plugin: 'java' 

configurations { 
    addclasspath 
} 

dependencies { 
    addclasspath files(project(':api:desktop-api').configurations['runtime'].files) 
    addclasspath files(project(':api:desktop-api').sourceSets['main'].output) 
    addclasspath files(project(':api:desktop-api').sourceSets.main.output.classesDir) 

    runtime project(':api:desktop-api') 
} 

sourceSets { 
    main { 
     runtimeClasspath += files(project(':api:desktop-api').sourceSets['main'].output) 
     runtimeClasspath += files(project(':api:desktop-api').sourceSets['main'].output.classesDir) 
     runtimeClasspath += files(project(':api:desktop-api').configurations['runtime'].files) 
    } 
} 


import com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo 
import com.github.kongchen.swagger.docgen.mavenplugin.ApiSource 
import io.swagger.models.Info 

task swagger(dependsOn: [':api:desktop-api:build']) { 
    doLast { 
     logger.info 'Swagger GenDoc...' 
     project.file(reportsDir).mkdirs() 

     // a trick to have all needed classes in the classpath 
     def customClassLoader = new GroovyClassLoader() 

     buildscript.configurations.classpath.each { 
      //println it.toURI().toURL() 
      customClassLoader.addURL(it.toURI().toURL()) 
     } 

     configurations.addclasspath.each { 
      customClassLoader.addURL(it.toURI().toURL()) 
     } 

     // the same settings as in the swagger-maven-example/pom.xml 
     final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo', true, customClassLoader).newInstance(
       apiSources: [ 
         new ApiSource(
           springmvc: false, 
           locations: ['com/vmware/vdi/hydra'], 
           schemes: ['http', 'https'], 
           host: 'vmware.com', 
           basePath: '/api', 
           info: new Info(
             title: "Hydra DS-REST API's", 
             version: 'v100', 
             description: "Hydra DS-REST API's", 
           ), 
           swaggerDirectory: reportsDir 
         ) 
       ] 
     ) 
     mavenTask.execute() 
     logger.info 'Swagger GenDoc task is completed' 
    } 
} 
+1

あなたはこの質問を編集し、どのようにGradleでは、ツールのバージョンを実行しているように、スタックトレースおよびその他のサポート情報を提供しなければならない、など – jdv

答えて

0

buildscript.classloaderは私が探していたものです。

以下

動作するコードです:

buildscript { 
    repositories { 
     maven { url mavenRepo } 
    } 
    dependencies { 
     classpath "com.github.kongchen:swagger-maven-plugin:3.1.4" 
    } 
} 

import com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo 
import com.github.kongchen.swagger.docgen.mavenplugin.ApiSource 
import io.swagger.models.Info 


task swagger(dependsOn: ':api:build') { 
    doLast { 
     logger.info 'Swagger GenDoc...' 
     project.file(<dir>).mkdirs() 

     FileCollection apiRuntimeFiles = files(project(':api').configurations['runtime'].files) 
     apiRuntimeFiles.each { 
      buildscript.classLoader.addURL(it.toURI().toURL()) 
     } 

     FileCollection apiClassFiles =files(project(':api').sourceSets['main'].output) 
     apiClassFiles.each { 
      buildscript.classLoader.addURL(it.toURI().toURL()) 
     } 

     final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo', true, buildscript.classLoader).newInstance(
       apiSources: [ 
         new ApiSource(
           springmvc: false, 
           locations: ['<loc>'], 
           schemes: ['http', 'https'], 
           host: '<host>', 
           basePath: '/api', 
           info: new Info(
             title: "REST API's", 
             version: 'v1', 
             description: "REST API's", 
           ), 
           swaggerDirectory: <dir> 
         ) 
       ] 
     ) 
     mavenTask.execute() 
     logger.info 'Swagger GenDoc task is completed' 
    } 
} 
0

あなたのビルドスクリプトのいくつかの欠点を持っています。

ビルドスクリプトの依存関係でビルドされたものに依存しないでください。これは鶏と卵の問題です。ビルドを実行するのに必要なクラスを取得するには、ビルドを実行する必要があります。これは、まったく信頼できるものではありません。

代わりに、ブロックをbuildscriptブロックの外で依存関係として宣言する必要があります。 buildscriptブロックは、buildscriptブロックで正しいswagger-maven-pluginのようなビルドスクリプトがビルドスクリプトを使用してビルド中に使用するGradle TasksやGradle Pluginsやクラスのように、自分自身を実行するために必要な依存関係のみを対象としています。

これ以外にも、実行フェーズ中ではなく、構成フェーズ中にスワッガの一部(インスタンシエーション、実行、および印刷)を実行します。ビルドが設定されているため、実際に実行したいタスクが何であっても、タスクがすでに起動しているかどうかにかかわらず、コンフィグレーションフェーズで実行されるのは、タスククロージャではなく、doFirstまたはdoLastブロックの外にありますそれとも、そうでないか。最新のチェックが動作し、時間を節約するためには、実行と生成するすべての出力の間で変更される可能性のあるファイルやプロパティなどのすべての入力を宣言する必要があるため、実際に必要なときにタスクを実行する。

また、printlnをビルドスクリプトで使用しないでください。これはJavaプログラムでSystem.out.printlnを使用する場合と同じです。代わりに、提供されたロギング機能を直接使用する必要があります。 g。 logger.info 'Swagger GenDoc task is completed'を実行しています。

+0

おかげで、あなたの入力を感謝。 – Ukeshava

+0

依存関係をbuildscriptの外に追加しました。しかし、URLがスキャンされるのを見ると、Javaのリフレクションでは、最初の実行時にapiクラスのパスが失われています。コードを編集しました(混乱の原因となる閉じた括弧も見逃しました)。あなたの提案を最新のものにするには、はい、それも追加します。まず、私はこの作業が必要です。任意の提案/ポインタ? – Ukeshava

+0

これは、実際のファイルを 'files'で解決するためです。これを明示的に行うべきではありません。プロジェクトを手近にすることなく正しく行う方法を記述するのは難しいです。 – Vampire

関連する問題