首页 文章

将swagger-codegen项目导入现有的Android项目

提问于
浏览
1

我试图将swagger-codegen生成的“模块”项目集成到我的Android项目中 .

之前没有使用gradle那么多,并且swagger-codegen从我的角度创建了一个非常混乱的build.gradle .

我很难找到有关如何执行此操作的文档 . 而且我感到有点失落 .

我使用FAQ中描述的这种方法

mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
 -i http://petstore.swagger.io/v2/swagger.json \
 -l java --library=okhttp-gson \
 -o /var/tmp/java/okhttp-gson/

所以我试图从swagger-codegen生成的项目中复制源代码并合并两个gradle构建文件 . 我删除了Junit测试,因为我无法使Junit依赖工作(Implementing Swagger-codegen project - Error:(23, 17) Failed to resolve: junit:junit:4.12) . 但后来我陷入了插件之间的一些冲突?

The 'java' plugin has been applied, but it is not compatible with the Android plugins.

这是build.gradle:

import static jdk.nashorn.internal.runtime.regexp.joni.ApplyCaseFold.apply

apply plugin: 'idea'
apply plugin: 'eclipse'

group = 'io.swagger'
version = '1.0.0'

buildscript {
   repositories {
       jcenter()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:2.1.2'
       // classpath 'com.android.tools.build:gradle:1.5.+'
       classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
   }
}

repositories {
   jcenter()
   maven { url 'http://repo1.maven.org/maven2' }
}


if(hasProperty('target') && target == 'android') {

   apply plugin: 'com.android.library'
   apply plugin: 'com.github.dcendents.android-maven'

   android {
       compileSdkVersion 23
       buildToolsVersion '23.0.2'
       defaultConfig {
            minSdkVersion 14
            targetSdkVersion 23
       }
        compileOptions {
           sourceCompatibility JavaVersion.VERSION_1_7
           targetCompatibility JavaVersion.VERSION_1_7
       }

       // Rename the aar correctly
       libraryVariants.all { variant ->
           variant.outputs.each { output ->
               def outputFile = output.outputFile
               if (outputFile != null &&    outputFile.name.endsWith('.aar')) {
                    def fileName = "\u0024{project.name}-  \u0024{variant.baseName}-\u0024{version}.aar"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

    dependencies {
        provided 'javax.annotation:jsr250-api:1.0'
    }
}

afterEvaluate {
    android.libraryVariants.all { variant ->
        def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
        task.description = "Create jar artifact for ${variant.name}"
        task.dependsOn variant.javaCompile
        task.from variant.javaCompile.destinationDir
        task.destinationDir = project.file("${project.buildDir}/outputs/jar")
        task.archiveName = "${project.name}-${variant.baseName}-${version}.jar"
        artifacts.add('archives', task);
    }
}

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

artifacts {
    archives sourcesJar
}

} else {

apply plugin: 'java'
apply plugin: 'maven'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

install {
    repositories.mavenInstaller {
        pom.artifactId = 'XxxxXxxx'
    }
}

    task execute(type:JavaExec) {
       main = System.getProperty('mainClass')
       classpath = sourceSets.main.runtimeClasspath
    }
}

dependencies {
    compile 'io.swagger:swagger-annotations:1.5.8'
    compile 'com.squareup.okhttp:okhttp:2.7.5'
    compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'joda-time:joda-time:2.9.3'
   // testCompile 'junit:junit:4.12'
}

我在这里做错了吗?将swagger-codegen代码实现到我的项目中的正确方法是什么?

2 回答

  • 1

    快速导入它的方法是编译swagger生成的项目,然后将.jar文件复制到我的android项目并将其添加为库 .

  • 1

    我很难找到有关如何执行此操作的文档 . 而且我感到有点失落 .

    您可以克隆Android swagger-codegen示例 .

    (它确实使用了Junit,所以我不确定你得到了什么错误)

    除非那是你的意思

    到目前为止,我尝试复制源并合并两个gradle构建文件

    我问,哪两个Gradle文件?看起来您将Android Gradle文件与Java Gradle文件合并,这似乎会导致更多问题因为您正在...

    已经应用了'java'插件,但它与Android插件不兼容 .

    当你有这条线时,这似乎很自我解释

    apply plugin: 'java'
    

    除了检查构建目标之外,你在这里尝试做什么并不太清楚

    if(hasProperty('target') && target == 'android')
    

相关问题