How to validate JSON in Gradle build - json

I have a Gradle build where a large JSON configuration is bundled into a package for later upload onto a server. Sometimes when changes are made to the file, the file is not valid any more and thus fails to upload on the server.
I would like to find this earlier by adding a validate-step in the Gradle build.
When looking around I could not find a documented way to do this, I saw the project gradle-json-validator which looks promising, but there is no documentation whatsoever, so I am not sure how this can be used...
Any hint on gradle-json-validator or any other way to validate a JSON file as part of the Gradle build steps?

From source, it would seem, the usage would be:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'cz.alenkacz.gradle:json-validator:0.9.9'
}
}
apply plugin: 'cz.alenkacz.gradle.jsonvalidator'
The plugin doesn't seem to have an extension to do configuration. But seems to use jsonSchema and targetJsonFile as input schema and file-to-validate. I would try setting them at the root level of build.gradle
validateJson.jsonSchema = new File('/path/to/schema')
validateJson.targetJsonFile = new File('/path/to/jsonFile')
and the task to run is:
gradle validateJson

I have improved the readme file in the repository with proper usage example.
Hope that helps. https://github.com/alenkacz/gradle-json-validator

Related

How can I add and compile local copy of a library into Android React Native module?

How Can I add to native module, external library source code.
In the case, I want to add a local copy of ExoPlayer to react-native-video module.
I want to "git clone" the original source of the ExoPlayer
inside react-native-video/android-exoplayer ,make some modifications on it and test it.
Currently the depencies of /react-native-video/android-exoplayer/build.gradle are:
dependencies {
//noinspection GradleDynamicVersion
provided "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"
compile 'com.google.android.exoplayer:exoplayer:2.7.3'
compile('com.google.android.exoplayer:extension-okhttp:2.7.3') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.9.1'
}
current file structure:
build
build.gradle
ExoPlayer
/src
This directory "ExoPlayer" I got with "git clone" and want to use instead of
compile 'com.google.android.exoplayer:exoplayer:2.7.3'
I found the solution:
In the root Project settings.gradle should add this:
gradle.ext.exoplayerRoot = '../node_modules/react-native-video/android-exoplayer/ExoPlayer-r2.7.3'
gradle.ext.exoplayerModulePrefix = 'exoplayer-'
apply from: new File(gradle.ext.exoplayerRoot, 'core_settings.gradle')
In react-native-video/android-exoplayer/build.gradle dependencies section add this:
implementation project(':exoplayer-library-core')
implementation project(':exoplayer-library-hls')
implementation project(':exoplayer-library-smoothstreaming')
implementation project(':exoplayer-library-dash')
implementation project(':exoplayer-library-ui')
implementation project(':exoplayer-extension-okhttp')
and remove all rows started with "compile"

Unable to update file in appDataFolder using Google Drive REST API V3 on Android

This is the code i'm using to update the file.
File metadata = generateFileMetadata(fileId, thumbnail, properties);
return mService.files().update(fileId, metadata, generateFileContents())
.setFields("id, name, appProperties")
.execute();
This code generates a
java.lang.IllegalArgumentException.
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
at com.google.api.client.googleapis.media.MediaHttpUploader.setInitiationRequestMethod(MediaHttpUploader.java:872)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.initializeMediaUpload(AbstractGoogleClientRequest.java:237)
at com.google.api.services.drive.Drive$Files$Update.<init>(Drive.java:3163)
at com.google.api.services.drive.Drive$Files.update(Drive.java:3113)
Using breakpoints I could see that the String passed to the setInitiationRequestMethod is PATCH (not POST or PUT):
public MediaHttpUploader setInitiationRequestMethod(String initiationRequestMethod) {
Preconditions.checkArgument(initiationRequestMethod.equals(HttpMethods.POST)
|| initiationRequestMethod.equals(HttpMethods.PUT));
this.initiationRequestMethod = initiationRequestMethod;
return this;
}
this is what i have in my build.gradle
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile('com.google.api-client:google-api-client-android:1.21.0') {
exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-drive:v3-rev11-1.21.0') {
exclude group: 'org.apache.httpcomponents'
}
if I remove the file content (generateFileContents()) I'm able to update the metadata just fine.
How do I solve this?
I ran into this bug while writing a Drive REST API integration for an Android app (with Android Studio/Gradle). Since I'm not particularly experienced with Android's build system, resolving the issue cost me a few hours. Maybe this helps somebody with the same problem:
Clone the google-api-java-client repo from GitHub https://github.com/google/google-api-java-client
Install Maven https://maven.apache.org/run-maven/ (e.g. brew install maven on OSX)
On the command line, change into the google-api-client sub dir of the repo you cloned above
Run mvn clean install
This will produce a subdir called target in the google-api-client directory
In there, find google-api-client-1.22.0-SNAPSHOT.jar, rename it to google-api-client-1.21.00.jar (the renaming is probably not needed)
Drop the .jar in the libs folder of your android project
Tell Gradle to ignore the google-api-client dependency of the libraries you use, in my case this was:
compile('com.google.api-client:google-api-client-android:1.21.0') {
exclude group: 'org.apache.httpcomponents'
exclude module: 'google-api-client'
}
compile('com.google.apis:google-api-services-drive:v3-rev14-1.21.0') {
exclude group: 'org.apache.httpcomponents'
exclude module: 'google-api-client'
}
Add the Jackson dependency again, in case you miss it now. Do the same with other google-api-java-client dependencies if you need them.
compile('com.google.http-client:google-http-client-jackson2:1.21.0'){
exclude group: 'org.apache.httpcomponents'
}
Build your project, update(...) should now work.
Make a note to scrap these changes once Google has updated the library.
Take a look at the current commit of the google-api-java-client.
Unfortunately the fix was not released yet (fix on 21 Nov 2015 vs release on 19 Nov 2015), so you may have to build locally the project (with maven for instance)
The MediaHttpUploader javadocs suggests that it will only be used for HttpMethods#POST, and HttpMethods#UPDATE. Using update, based on the Files resource, indicates its using a PATCH method - leading to the IllegalArgumentException.
The overridden update method should only be used if you're uploading media content.
I have the same exception in a Desktop application.
Instead, using the Drive Api V2, the update goes well.

ConfigurationBuilder Twitter4j class not found

Sorry, but it seems i cannot find any ConfigurationBuilder class in Twitter4j.
I'm using twitter4j version 3.0.3. I have already tried 3.0.2 android version too.
I have added the jar to build path like core, async, media, and stream.
I tried to :
- import twitter4j.*;
- import twitter4j.conf.*;
- import twitter4j.conf.ConfigurationBuilder;
But Eclipse says that :
Multiple markers at this line
- ConfigurationBuilder cannot be resolved to a
type
- ConfigurationBuilder cannot be resolved to a
type
What do i need?
I have already checked the source folders and there is a class named ConfigurationBuilder.java.
Thank you!
You have to make sure that Android Dependencies folder. To do that, you copy the twitter4j jar file to the libs folder and then right click to Configure Path and add the library.
After that's done, you will see that the library is also copied to the dependencies folder.
Right click on the project > Properties > Java Build Path > Libraries
Make sure that twitter4j-core-x.y.z.jar is really there
Go also to Order and Export tab in the same dialog
Make sure that the twitter4j-core-x.y.z.jar is selected (to get the jar distributed with the *.apk or else you will get something similar to the following at runtime: could not find class ConfigurationBuilder)
Project > Clean
Project > Build All

Is there a way to get a JSON-Schema from a Scala Case Class hierarchy?

I'm documenting an internal REST API written ini Scala, unfortunately we are not able to integrate Swagger, so for now we are going with an in-house solution for the doc generator.
I would like to generate a JSON-Schema to show how the response is when getting our resources. I'm just wondering if there is any shortcut to do this by taking advantage of the case classes already modeled.
The autoschema project is able to export JSON schema from Scala case classes. You can use it as follows:
case class MyType(myValue: Int)
AutoSchema.createSchema[MyType]
The Maven artifact seems to be no longer available but it is an SBT project available on Github so you can either copy the sources, build a Jar or add it as a dependency with SBT by putting in your build.sbt the following:
lazy val autoschemaProject =
ProjectRef(uri("https://github.com/coursera/autoschema.git"), "autoschema")
lazy val root = (project in file(".")).dependsOn(autoschemaProject)
I tested this with SBT 0.13.7. Notice that autoschema has its own dependencies (mainly play-json 2.3.2) so you might need to change their versions to avoid version conflicts with you own project dependencies.
As #mziccard stated, autoschema is the way to go. However, it's been a while since there has been some activity on the main repository. I took some time to fork it and update its dependencies and deprecated code (work that was done in other forks, I simply combined it). It is now published in maven central under my fork:
https://github.com/sauldhernandez/autoschema
You can use it by putting this in build.sbt:
libraryDependencies += "com.sauldhernandez" %% "autoschema" % "1.0.0"

Pulling a file from another job before building in Jenkins?

Hope you can help me on this one. I have a job called "Template" that generates a template.xml file. I have several other jobs that use this template.xml file. However, before they build, i wanted that they could pull the latest template.xml from the "Template" job workspace.
In your "template" job, under Post-Build Actions, choose to artifact your xml file using the archive option.
You can then use the "Copy Artifact Plugin" to copy it over to all other jobs.
Jenkins Job Setup for Artifact Generator Project:
Jenkins Job Setup for Artifact User Project:
In your Template job, you can archive the template.xml file as an artifact, then it will be available to your other jobs at a URL similar to the following:
http://myserver/jenkins/job/myjob/lastSuccessfulBuild/artifact/template.xml
I used Copy Artifacts plugin with Jenkinsfile. Here an example:
In the job that is producing the artifact you should do something like:
pipeline {
options {
copyArtifactPermission ‘*’ //Here you can specify the job name also
}
stages {
stage(“Run") {
...
archiveArtifacts artifacts: “my_artifact.yaml"
}
}
}
In the job that is consuming the artifact you can use something like:
stage("Consumer") {
steps {
script {
copyArtifacts filter: “my_artifact.yaml", projectName: 'PRODUCER_JOB',
selector: lastSuccessful()
}
}
}
In my end I use a pretty strainghforward approach. I run jenkins in a usual layout (ubuntu 16.04 server) and I need call the checkstyle maven plugin using the same checkstyle.xml config file residing inside a dev-resources job:
Than I call mvn checkstyle:
Hope it can be useful for someone else.