Jaxb Plugin Issue with gradle7 and jdk11 - build.gradle

I am trying to run build locally using gradle 7.4 and jdk 11 with below setting in build.gradle.
apply plugin: 'com.github.jacobono.jaxb'
dependencies {
.....
jaxb "com.sun.xml.bind:jaxb-xjc:3.0.2"
jaxb "com.sun.xml.bind:jaxb-impl:3.0.2"
}
jaxb {
xsdDir='src/main/resources/xsd' // Define the folder for location of the schema file
xjc {
generatePackage 'com.demo.bizid.entities' // Set the package name for generated source files
destinationDir='build/jaxb-generated'
}
}
sourceSets.main.java.srcDirs 'build/jaxb-generated'
tasks.withType(JavaCompile) {
dependsOn xjc
}
But getting build issue with below error
Some problems were found with the configuration of task ':xsd-dependency-tree' (type 'JaxbDependencyTree').
- In plugin 'com.github.jacobono.jaxb' type 'org.gradle.jacobo.plugins.task.JaxbDependencyTree' property 'dependencyTreeFactory' is missing an input or output annotation.
Reason: A property without annotation isn't considered during up-to-date checking.

Related

Import XSD to OpenAPI

I have some model definition inside a XSD file and I need to reference these models from an OpenApi definition. Manually remodeling is no option since the file is too large, and I need to put it into a build system, so that if the XSD is changed, I can regenerate the models/schemas for OpenApi.
What I tried and what nearly worked is using xsd2json and then converting it with the node module json-schema-to-openapi. However xsd2json is dropping some of the complexElement models. For example "$ref": "#/definitions/tns:ContentNode" is used inside of one model as the child type but there is no definition for ContentNode in the schema, where when I look into the XSD, there is a complexElement definition for ContentNode.
Another approach which I haven't tried yet but seems a bit excessive to me is using xjb to generate Java models from the XSD and then using JacksonSchema to generate the json schema.
Is there any established library or way, to use XSD in OpenApi?
I ended up implementing the second approach using jaxb to convert the XSD to java models and then using Jackson to write the schemas to files.
Gradle:
plugins {
id 'java'
id 'application'
}
group 'foo'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-jsonSchema', version: '2.9.8'
}
configurations {
jaxb
}
dependencies {
jaxb (
'com.sun.xml.bind:jaxb-xjc:2.2.7',
'com.sun.xml.bind:jaxb-impl:2.2.7'
)
}
application {
mainClassName = 'foo.bar.Main'
}
task runConverter(type: JavaExec, group: 'application') {
classpath = sourceSets.main.runtimeClasspath
main = 'foo.bar.Main'
}
task jaxb {
System.setProperty('javax.xml.accessExternalSchema', 'all')
def jaxbTargetDir = file("src/main/java")
doLast {
jaxbTargetDir.mkdirs()
ant.taskdef(
name: 'xjc',
classname: 'com.sun.tools.xjc.XJCTask',
classpath: configurations.jaxb.asPath
)
ant.jaxbTargetDir = jaxbTargetDir
ant.xjc(
destdir: '${jaxbTargetDir}',
package: 'foo.bar.model',
schema: 'src/main/resources/crs.xsd'
)
}
}
compileJava.dependsOn jaxb
With a converter main class, that does something along the lines of:
package foo.bar;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
import foo.bar.model.Documents;
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
try {
JsonSchema schema = schemaGen.generateSchema(Documents.class);
System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
It is still not perfect though,... this would need to iterate over all the model classes and generate a file with the schema. Also it doesn't use references, if a class has a member of another class, the schema is printed inline instead of referencing. This requires a bit more customization with the SchemaFactoryWrapper but can be done.
The problem you have is that you are applying inference tooling over a multi-step conversion. As you have found, inference tooling is inherently fussy and will not work in all situations. It's kind of like playing Chinese whispers - every step of the chain is potentially lossy, so what you get out the other end may be garbled.
Based on the alternative approach you suggest, I would suggest a similar solution:
OpenAPI is, rather obviously, an API definition standard. It should be possible for you to take a code first approach, composing your API operations in code and exposing the types generated from XJB. Then you can use Apiee and its annotations to generate the OpenAPI definition. This assumes you are using JAX-RS for your API.
This is still a two-step process, but one with a higher chance of success. The benefit here is that your first step, inferring your XSD types into java types, will hopefully have very little (if any) impact on the code which defines your API operations. Although there will still be a manual step (updating the models) the OpenAPI definition will update automatically once the code has been rebuilt.

Where to use converters?

I've just installed AndroidAnnotations and I want to use the #Rest annotation, however, as I read:
You MUST define converters field on this #Rest annotation, which
corresponds to the Spring HttpMessageConverters that will be provided
to the RestTemplate.
So, where to get MappingJackson2HttpMessageConverter? and how to install it?
Or at least, to convert my expected json string into a json object?
Is there any simple example?
Thanks
As it is written in the documentation, you have to add the Spring REST template dependency, which is the underlying REST library, AA is just a wrapper around it.
You can do it by adding this to your build.gradle.
dependencies {
compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
} repositories {
maven {
url 'https://repo.spring.io/libs-milestone'
}
}
Now you can add any converter, like it is outlined in the doc:
#Rest(converters = { MappingJackson2HttpMessageConverter.class })
public interface MyRestClient {
#Get("/events")
EventList getEvents();
}

Startup.cs error (ASP.Net Core configuration)

I am trying to set up an ASP.Net Core application to read in configuration settings from a json file. I am using VS2015 and .NetCore 1.0 (with .Net Core Tools preview 2). I am having problems getting a simple piece of boiler plate code to compile.
I am using the following code, which was published at
http://asp.net-hacker.rocks/2016/03/21/configure-aspnetcore.html
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// This will push telemetry data through Application Insights
// pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
Configuration = builder.Build();
}
However, the IDE/compiler complains that 'the name "Configuration" does not exist in the current context' (last line of code). The only suggestion from the IDE is to include Microsoft.Extensions.Configuration. However this is a namespace which does not contain an object or property named "Configuration".
In addition 'AddApplicationInsightsSettings' fails with does IConfigurationBuilder not contain a definition for AddApplicationInsightsSettings and no extension method AddApplicationInsightsSettings accepting a first argument of type IConfigurationBuilder could be found
Any suggestions please ?
Thanks
Simply add Configuration property to your Startup class, tutorial has missed this 'step':
public IConfigurationRoot Configuration { get; set; }
ConfigurationBuilder.Build() method just returns instance of IConfigurationRoot, that you should save, if need to get settings further in Startup class (in ConfigureServices method for example).
Regarding second error, looks like you didn't add the Application Insights dependency:
{
"dependencies": {
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0"
}
}

Error while building fat jar for deploying my jar to remote Cluster

I have set up a single node cluster and trying to deploy my sample topology to it .
I believe to deploy my topology to cluster I have to submit the jar with all dependencies to the Cluster .
For that I created a sample project and added a simple topology to it .
While generating the fat jar using gradle I am seeing this error
gradle fatjar gives below error
Could not expand ZIP '/Users/agarg/.gradle/caches/modules-2/files-
2.1/org.apache.storm/storm-
core/0.9.5/d2bf27db853347dcf66990b4514db20a7897303e/storm-core-0.9.5.jar'.
Could not copy zip entry /Users/agarg/.gradle/caches/modules-2/files-
2.1/org.apache.storm/storm-
core/0.9.5/d2bf27db853347dcf66990b4514db20a7897303e/storm-core-
0.9.5.jar!META-INF/license/LICENSE.base64.txt to '/Users/agarg/Documents/notificationRepo/sample/build/tmp/expandedArchives/storm-
core-0.9.5.jar_366us3312tpl54tci2fld83fij/META-INF/license/LICENSE.base64.txt'.
Here is my build.gradle file fo reference :
dependencies {
compile group: 'clj-stacktrace' , name: 'clj-stacktrace',version: cljStackTrace
compile group: 'org.apache.storm' , name: 'storm-core',version: stormVersion
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'storm.topology.ExclamationTopology'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
buildscript {
repositories {
mavenCentral()
maven { url 'http://repo.spring.io/snapshot' }
maven { url 'http://repo.spring.io/milestone' }
maven { url "https://clojars.org/repo/" }
maven { url "http://repo.maven.apache.org/maven2" }
}
}
Can nyone help me what is going wrong here ???
Thanks In Advance
I am not familiar with gradle, however, you do not need to include "storm-core" or transitive dependencies in your fat jar. It is sufficient if your far jar contains Spout and Bolt classes (and maybe some 3rd party libraries you are using that are not part of Storm)

Gradle dependency on project for all configurations

I'm looking into using Gradle instead of Ant/Ivy. I'm trying to create dependencies between my projects for all configurations, so that for example, project1.compile depends on project2.compile, project1.runtime depends on project2.runtime, etc.
In Ivy, I did this with the following XML:
project1/ivy.xml
<dependency conf="*->#" org="myorg" name="project2" rev="latest.integration" />
In Gradle, here's what I have tried:
project1/build.gradle
configurations.each { config ->
config.dependencies.add project(path: ':project2', configuration: config.name)
}
But it complains that the project function doesn't exist:
> Could not find method project() for arguments [{path=:project2, configuration=archives}] on project ':project1'.
Any ideas how to do this?
configurations.all { config ->
project.dependencies.add(config.name,
project.dependencies.project(
path: ':project2', configuration: config.name))
}
To anyone looking for a working answer to the same question in 2023:
configurations.all {
withDependencies {
add(
project.dependencies.module("org.example:example-artifact:0.0.1") // external dependency
)
add(
project.dependencies.platform("org.example:example-bom:0.0.1") // platform BOM
)
add(
project.dependencies.project(":project2") // project submodule
)
}
}