Skip to content

Replace maven settings for Android client with gradle #699

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

Merged
merged 1 commit into from
Apr 28, 2015
Merged

Replace maven settings for Android client with gradle #699

merged 1 commit into from
Apr 28, 2015

Conversation

0legg
Copy link
Contributor

@0legg 0legg commented Apr 28, 2015

While being well-known and widely used in JEE development, Maven is very rare guest in Android projects. There is no official support for Maven from Google, while Gradle is standard de-facto for Android development.

@fehguy
Copy link
Contributor

fehguy commented Apr 28, 2015

@0legg do you see an issue with providing both scripts?

@0legg
Copy link
Contributor Author

0legg commented Apr 28, 2015

@fehguy Actually no, but 95% of Android devs will just delete pom.xml asking "What is this stuff about?"

@fehguy
Copy link
Contributor

fehguy commented Apr 28, 2015

Will this produce a fat jar? I'm all for going with the right conventions for the language.

@0legg
Copy link
Contributor Author

0legg commented Apr 28, 2015

No, the jar will be fatter only by pom.xml (well, actually even this file will be removed from resulting apk, so there'll be no difference). I can return it easily, but I don't think that this is something more than tribute to history

@fehguy
Copy link
Contributor

fehguy commented Apr 28, 2015

no i mean, will this script produce a single jar, which has been another request. I'll go ahead and merge this, thank you.

fehguy added a commit that referenced this pull request Apr 28, 2015
Replace maven settings for Android client with gradle
@fehguy fehguy merged commit 2f8e0bb into swagger-api:develop_2.0 Apr 28, 2015
@who
Copy link
Contributor

who commented Apr 29, 2015

@fehguy @0legg

I agree with this change, but this PR appears to have broken the build of the generated android client out of the box. This worked before PR (using mvn commands)

me@box:/tmp/tmp.sUqSLYv9WH$ cd $(mktemp -d /tmp/tmp.XXXXXX)
me@box:/tmp/tmp.bnfnTK$ git clone https://github.com/swagger-api/swagger-codegen.git
Cloning into 'swagger-codegen'...
remote: Counting objects: 28511, done.
remote: Compressing objects: 100% (56/56), done.
remote: Total 28511 (delta 13), reused 0 (delta 0), pack-reused 28443
Receiving objects: 100% (28511/28511), 15.69 MiB | 3.16 MiB/s, done.
Resolving deltas: 100% (12460/12460), done.
Checking connectivity... done.
me@box:/tmp/tmp.bnfnTK$ cd swagger-codegen/
me@box:/tmp/tmp.bnfnTK/swagger-codegen$ git checkout develop_2.0
Branch develop_2.0 set up to track remote branch develop_2.0 from origin.
Switched to a new branch 'develop_2.0'
me@box:/tmp/tmp.bnfnTK/swagger-codegen$ git show | grep Date
Date:   Tue Apr 28 15:21:51 2015 -0700
me@box:/tmp/tmp.bnfnTK/swagger-codegen$ mvn clean package >> /dev/null 2>&1   
me@box:/tmp/tmp.bnfnTK/swagger-codegen$ ./bin/android-java-petstore.sh 
reading from modules/swagger-codegen/src/test/resources/2_0/petstore.json
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/User.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Category.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Pet.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Tag.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Order.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/api/UserApi.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/api/PetApi.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/api/StoreApi.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/build.gradle
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/ApiInvoker.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/HttpPatch.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/JsonUtil.java
writing file /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/ApiException.java
me@box:/tmp/tmp.bnfnTK/swagger-codegen$ cd /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java
me@box:/tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java$ gradle build

FAILURE: Build failed with an exception.

* Where:
Build file '/tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating root project 'android-java'.
> Plugin with id 'com.android.application' not found.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.434 secs

@0legg
Copy link
Contributor Author

0legg commented Apr 29, 2015

@who Oh, my bad. This PR generates only one module, but not the whole project. I'll try to figure out how to do it without including into templates all the typical Android project gradle-connected stuff like gradle wrapper and so on

@who
Copy link
Contributor

who commented Apr 29, 2015

@0legg I don't think this should generate a module or a whole project. It should generate stand-alone code which can be compiled into a jar, and then the jar can be imported as a dependency into your Android app.

@who
Copy link
Contributor

who commented Apr 29, 2015

This may be useful for a starting point.

This build.gradle ...

apply plugin:'java'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.2'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

ext {
    swagger_annotations_version = "1.5.3-M1"
    jackson_version = "2.5.2"
    apache_httpcore_version = "4.3.3"
    apache_httpclient_version = "4.3.5.1"
    apache_httpmime_version = "4.4.1"
    junit_version = "4.8.1"
}

dependencies {
    compile "com.wordnik:swagger-annotations:$swagger_annotations_version"
    compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
    compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
    compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
    compile "org.apache.httpcomponents:httpcore:$apache_httpcore_version"
    compile "org.apache.httpcomponents:httpclient:$apache_httpclient_version"
    compile "org.apache.httpcomponents:httpmime:$apache_httpmime_version"
    testCompile "junit:junit:$junit_version"
}

... yields this output ...

$ gradle clean build
:clean UP-TO-DATE
:compileJava
Note: /tmp/tmp.bnfnTK/swagger-codegen/samples/client/petstore/android-java/src/main/java/io/swagger/client/ApiInvoker.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 7.165 secs

... and this file structure:

$ tree build
build
├── classes
│   ├── main
│   │   └── io
│   │       └── swagger
│   │           └── client
│   │               ├── api
│   │               │   ├── PetApi.class
│   │               │   ├── StoreApi.class
│   │               │   └── UserApi.class
│   │               ├── ApiException.class
│   │               ├── ApiInvoker$1.class
│   │               ├── ApiInvoker$2.class
│   │               ├── ApiInvoker.class
│   │               ├── HttpPatch.class
│   │               ├── JsonUtil.class
│   │               └── model
│   │                   ├── Category.class
│   │                   ├── Order.class
│   │                   ├── Order$StatusEnum.class
│   │                   ├── Pet.class
│   │                   ├── Pet$StatusEnum.class
│   │                   ├── Tag.class
│   │                   └── User.class
│   └── test
│       └── io
│           └── swagger
│               └── petstore
│                   └── test
│                       ├── PetApiTest.class
│                       ├── StoreApiTest.class
│                       └── UserApiTest.class
├── dependency-cache
├── libs
│   └── android-java.jar
├── reports
│   └── tests
│       ├── classes
│       │   ├── io.swagger.petstore.test.PetApiTest.html
│       │   ├── io.swagger.petstore.test.StoreApiTest.html
│       │   └── io.swagger.petstore.test.UserApiTest.html
│       ├── css
│       │   ├── base-style.css
│       │   └── style.css
│       ├── index.html
│       ├── js
│       │   └── report.js
│       └── packages
│           └── io.swagger.petstore.test.html
├── test-results
│   ├── binary
│   │   └── test
│   │       ├── output.bin
│   │       ├── output.bin.idx
│   │       └── results.bin
│   ├── TEST-io.swagger.petstore.test.PetApiTest.xml
│   ├── TEST-io.swagger.petstore.test.StoreApiTest.xml
│   └── TEST-io.swagger.petstore.test.UserApiTest.xml
└── tmp
    ├── compileJava
    ├── compileTestJava
    └── jar
        └── MANIFEST.MF

27 directories, 35 files

@0legg
Copy link
Contributor Author

0legg commented Apr 29, 2015

@who I've tried to apply this approach, but have failed to run the tests. Android internal version of HttpClient conflicts with provided one, this inconsistence leads to test failure. I'm trying to workaround this behavior.

@who
Copy link
Contributor

who commented Apr 29, 2015

@0legg Yes, I found the same test failures, so I switched over to org.apache.httpcomponents.httpclient in the mean time.

@fehguy
Copy link
Contributor

fehguy commented Apr 29, 2015

Guys, shall I roll this back?

@who
Copy link
Contributor

who commented Apr 29, 2015

@fehguy @0legg I support using gradle instead of maven, but I think that yes, this should be rolled back until the following workflow works from the command line:

  1. git clone swagger-codegen develop_2.0
  2. generate a client
  3. gradle build the generated client without error

@0legg
Copy link
Contributor Author

0legg commented Apr 29, 2015

@fehguy @who I've just made the changes in my forked version, so that user can now create aar library from generated client or use it straightforward as android library project.

@who
Copy link
Contributor

who commented Apr 29, 2015

@0legg @fehguy

Here is a rudimentary console build integration test:

cd $(mktemp -d /tmp/tmp.XXXXXX);
git clone https://github.com/swagger-api/swagger-codegen.git;
cd swagger-codegen;
git checkout develop_2.0;
./bin/android-java-petstore.sh;
cd ./samples/client/petstore/android-java;
gradle build;

@0legg
Copy link
Contributor Author

0legg commented Apr 29, 2015

I think we can put back maven version alongside with gradle one, so that who needs to generate jar will use mvn package, otherwise will use it as android archived library or even as is

@who
Copy link
Contributor

who commented Apr 29, 2015

@0legg No need to have mvn and gradle together - can't you just add apply plugin: 'java' to get the jar as an artifact from a gradle build?

@0legg
Copy link
Contributor Author

0legg commented Apr 29, 2015

So now I achieved my goal =) Tests are passed, gradle generates both jar and aar, and generated module can be used as android library without additional packaging. @fehguy, do I need to provide another pull request for these fixes?

@who
Copy link
Contributor

who commented Apr 29, 2015

👍 @0legg I just pulled your fork and was able to pass my tests as well.

@webron
Copy link
Contributor

webron commented Apr 29, 2015

@0legg - since it was already merged, another PR is required, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants