将swagger-codegen项目导入现有的Android项目 [英] Import swagger-codegen project into existing Android project

查看:320
本文介绍了将swagger-codegen项目导入现有的Android项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将swagger-codegen生成的模块"项目集成到我的Android项目中.

Im trying to integrate a "module"-project generated by swagger-codegen, into my Android project.

以前我没有在gradle上做太多工作,而swagger-codegen从我的角度来看创建了一个相当混乱的build.gradle.

Haven't worked that much with gradle before and the swagger-codegen creates a quite messy build.gradle from my point of view.

我很难找到有关如何执行此操作的文档.我感到有些迷茫.

I have a hard time finding documentation on how to do this. And I feel a bit lost.

我使用了常见问题解答

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依赖项正常工作(

So fare I tried to copy the source from the project that was generated by swagger-codegen and merge the two gradle build files. I removed the Junit tests because I couldn't get the Junit dependency working (Implementing Swagger-codegen project - Error:(23, 17) Failed to resolve: junit:junit:4.12). But then I got stuck with some conflict between the plugins?

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

这是build.gradle:

Here's the 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代码的正确方法是什么?

Am I doing something complete wrong here? What is the correct way to implement swagger-codegen code into my project?

推荐答案

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

The swift way to import it was to compile the swagger generated project then copy the .jar file to my android project and add its as a library.

这篇关于将swagger-codegen项目导入现有的Android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆