Unable to access generated java files - java

My project uses gRPC and it generates a file in generated folder.
This is my build.gradle of the module.
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'io.grpc:grpc-okhttp:1.4.0'
compile 'io.grpc:grpc-protobuf-lite:1.4.0'
compile 'io.grpc:grpc-stub:1.4.0'
compile 'javax.annotation:javax.annotation-api:1.2'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
apply plugin: 'com.google.protobuf'
def grpcVersion = '1.4.0' // CURRENT_GRPC_VERSION
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.3.0'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {
// To generate deprecated interfaces and static bindService method,
// turn the enable_deprecated option to true below:
option 'enable_deprecated=false'
}
}
}
}
// Inform IntelliJ projects about the generated code.
apply plugin: 'idea'
idea {
module {
// Not using generatedSourceDirs because of
// https://discuss.gradle.org/t/support-for-intellij-2016/15294/8
sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
}
}
How do I fix this?

You have to add this folder to your source sets.
The following should work:
sourceSets {
generated{
java.srcDir "${projectDir}/build/generated/source/proto/main/java"
}
}
Here is a comprehensive documentation for customizing the source sets.

I figured out. My iml file includes the folder but also excludes it. I think exclusion takes precedence, so I have to remvoe the <excludeFolder> element.

Related

gradle generates protobuf class but shows compile error

The generated protobuf class is under generated-sources as expected.
But it has references to com.google.protobuf, for example below code. And I get compilation error saying com.google.protobuf not found.
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
The below is my build.gradle file.
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}
I think the problem is that the protobuf library is not showing up in the external libraries of my intellij project. Is there a way to make it work with gradle?
Working gradle file:
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.google.protobuf:protobuf-java:3.11.1'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.6.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}

Building a project and a Spock test suite with gradle

I'm trying to build a Springboot web application that creates a WAR and also a suite of tests using the Spock framework but every time I try to run gradle build the console will execute the tests but will not create the web app WAR although it mentions a file in the web app source code saying a deprecation warning so I know it is at least doing something with it. Is there some kind of configuration I'm missing from my build.gradle or concept about how sourceSets work? I can compile the web app fine and the tests when I do them separately with their own build.gradle files but I was hoping that the code below could build the WAR and execute the tests in the same file.
plugins {
id 'java'
id 'maven-publish'
id 'war'
id 'groovy'
}
repositories {
mavenLocal()
maven {
url = 'https://repo.spring.io/libs-release'
}
maven {
url = 'http://repo.maven.apache.org/maven2'
}
}
sourceSets{
main {
java { srcDir "src\\TestingWebAppSpringBoot\\src\\main\\java" }
resources {srcDir "src\\TestingWebAppSpringBoot\\src\\main\\resources" }
groovy { srcDirs = [] }
}
test{
groovy {
srcDirs = ["src\\test_pack\\com\\example\\diag\\test\\spock\\main", "src\\test_pack\\com\\example\\diag\\test\\spock\\test" ]
}
}
}
test{
reports.html.destination = file("build\\spockTestResults")
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:2.0.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-web:2.0.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-security:2.0.0.RELEASE'
compile 'org.codehaus.groovy:groovy-all:2.4.14'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile (
fileTree(dir: 'build\\server\\_archives\\_release', include: '*.jar'),
fileTree(dir: 'build\\common\\_archives\\_release', include: '*.jar'),
'net.sourceforge.htmlunit:htmlunit:2.26',
"org.codehaus.groovy:groovy-all:2.4.5",
"org.spockframework:spock-core:1.0-groovy-2.4"
)
}
group = 'com.something.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

ProtoBuffer files generated after building project missing dependencies

I am trying to create implement client-server communication with the help of grpc
proto file -
syntax="proto3";
package com.project.grpc.roleservice;
message UserRequest {
string userName=1;
}
message RoleReply {
string userRole=1;
}
service UserRoleFromServer{
rpc getRoleUser(stream UserRequest) returns (stream RoleReply);
}
Gradle build -
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.google.protobuf:protobuf-gradle-plugin:0.8.5")
}
}
plugins {
id 'java'
}
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.protobuf'
group 'com.project.grpc.roleservice'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.5.1-1"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.16.1'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile( 'io.grpc:grpc-netty-shaded:1.16.1')
compile('io.grpc:grpc-protobuf:1.16.1')
compile('io.grpc:grpc-stub:1.16.1')
compile 'io.github.lognet:grpc-spring-boot-starter:3.0.0'
runtime('org.springframework.boot:spring-boot-devtools')
runtime('mysql:mysql-connector-java')
testCompile (group: 'junit', name: 'junit', version: '4.12')
}
After building the project all the files are generated in a build folder and a UserRoleFromServerGrpc is generated which I want to extend and implement in my server-service file
But The UserRoleFromServerGrpc has a lot of errors it asks for following dependencies
Streamobserver needs io.grpc:grpc-stub:1.16.1 ,grpc-core
Add io.grpc:grpc-stub:1.16.1 to classpath
but I have already defined these dependencies in gradle build
How to resolve this issue
Screenshot of IDE

Gradle wsdl generating

I want to generate java files from wsdl. I try to use wsdl2java gradle plugin. I define the plugin:
subprojects {
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'application'
apply plugin: 'no.nils.wsdl2java'
....
}
but I got this error:
> Plugin with id 'no.nils.wsdl2java:no.nils:wsdl2java' not found.
I checked the syntax (many times it is good). I googled for the plugin, it is used by many people.
Does anybody have an idea what is going wrong?
Upd:
I have a main gradle where the plugins defined, and there are three sub project, where I want to use this plugin.
I defined the sub projects in the settings.gradle:
include 'project1', 'project2', 'project3'
I made a folder and the build.gradle file for each project.
If I commented out the apply plugin: 'no.nils.wsdl2java' in the main build.gradle and the wsdl2java methods in the sub projects the gradle works fine.
You add the buildscript inside the subprojects-closure, thats not supported, see this Gradle discussion (Buildscript {} in subprojects {} ignored?).
You do not have to add the buildscript for every project, it is enough to just declare it on the root-build.gradle
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'application'
apply plugin: 'no.nils.wsdl2java'
....
}
I was able to get the wsdl2 java working in my gradle build with jaxws and ant tasks. Here it is
apply plugin: 'java'
repositories {
mavenCentral()
flatDir {
dirs 'lib'
}
}
configurations { jaxws }
dependencies { jaxws 'com.sun.xml.ws:jaxws-tools:2.2.6' }
dependencies {
compile 'com.sun.xml.bind:jaxb-impl:2.2.6'
}
task generateSCMOrderImportServiceClient{
if(!file("./lib/employee-services-client.jar").exists()) {
def rootDir = file("build/wsdlToJava/employee-services-client");
def javaDir = file("${rootDir}/java");
def wsdlJarDir = file("${projectDir}/lib");
def classesDir = file("${rootDir}/classes");
def wsdlDir=file("${projectDir}/src/main/resources/wsdl");
def wsdlFile = file("${wsdlDir}/employee-services.wsdl")
doLast{
classesDir.mkdirs()
javaDir.mkdirs()
wsdlJarDir.mkdirs()
copy {
from "${wsdlFile}"
into "${classesDir}"
}
ant {
taskdef(name: 'wsimport',
classname: 'com.sun.tools.ws.ant.WsImport',
classpath: configurations.jaxws.asPath)
wsimport(keep: true,
destdir: classesDir,
sourcedestdir: javaDir,
extension: "true",
verbose: "true",
quiet: "false",
xnocompile: "false",
xendorsed: true,
wsdlLocation: "EmployeeServices.wsdl",
wsdl: "${wsdlFile}")
{
binding(dir:"${wsdlDir}", includes:"jaxb-bindings.xml,jaxws-bindings.xml")
xjcarg(value: "-XautoNameResolution")
}
}
ant.jar(
destfile: wsdlJarDir.path + "/employee-services-client.jar",
basedir: classesDir
)
}
}
}
compileJava.dependsOn generateSCMOrderImportServiceClient
I had achieved this task using this git repository. The build.gradle file looks something like this.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
plugins {
id 'org.springframework.boot' version '2.2.0.M6'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'no.nils.wsdl2java' version '0.10'
}
group = 'your.application.groupname'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
wsdl2java(
'com.sun.xml.bind:jaxb-xjc:2.3.0.1',
'javax.xml.bind:jaxb-api:2.3.1',
'javax.xml.ws:jaxws-api:2.3.1',
'org.apache.cxf:cxf-rt-wsdl:3.2.7',
'javax.jws:javax.jws-api:1.1',
'com.sun.xml.bind:jaxb-core:2.3.0.1',
'com.sun.xml.bind:jaxb-xjc:2.3.2',
'com.sun.xml.bind:jaxb-impl:2.3.2',
'javax.xml.bind:jaxb-api:2.3.1'
)
implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1'
implementation 'com.sun.xml.bind:jaxb-xjc:2.3.0.1'
implementation 'com.sun.xml.bind:jaxb-impl:2.3.2'
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'javax.xml.ws:jaxws-api:2.3.1'
implementation 'org.apache.cxf:cxf-rt-wsdl:3.2.7'
implementation 'javax.jws:javax.jws-api:1.1'
}
test {
useJUnitPlatform()
}
wsdl2java {
wsdlsToGenerate = [
['-p', 'your.package.name',
'-autoNameResolution', "$projectDir/src/main/resources/wsdl/some_wsdl_file.wsdl"]
]
generatedWsdlDir = file("$projectDir/src/main/java")
wsdlDir = file("$projectDir/src/main/resources/wsdl")
locale = Locale.ENGLISH
}
wsdl2javaExt {
cxfVersion = "2.5.1"
}
To generate the java code we need to run the gradle task as shown below.
$ gradlew wsdl2java
For anybody who is looking for generating with Apache CXF. I tried to avoid using any plugin. I did it by creating configuration with dependencies, externalized array of strings with wsdl file names and then just use it like this:
ext {
wsdlDir = file("${projectDir}/src/main/resources/wsdl")
outputDir = file("$buildDir/generated-sources")
sourceWsdls = [
"$wsdlDir/MyWsdlFile1.wsdl",
"$wsdlDir/MyWsdlFile2.wsdl",
"$wsdlDir/MyWsdlFile3.wsdl",
"$wsdlDir/MyWsdlFile4.wsdl"
]
}
sourceSets.main.java.srcDirs += "$outputDir"
dependencies {
cxf (
'org.apache.cxf:cxf-tools-wsdlto-core:3.3.6',
'org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:3.3.6',
'org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:3.3.6'
)
}
task generateJavaClasses {
doLast{
sourceWsdls.each { wsdlFile ->
javaexec {
classpath configurations.cxf
main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
args '-d', outputDir
args '-b', 'PATH/TO/BINDING/FILE.xjb'
args wsdlFile
}
}
}
}
This is just simple calling javaexec of Apache CXF class - see docs for all args and options.
Note: If you have a folder with wsdl files and you want to generated classes from all wsdl files there, it is even easier and just use it like this:
task generateJavaClassesAllWsdlFiles {
doLast{
// Find all wsdl files in directory defined in ext
fileTree(wsdlDir).matching {
include "*.wsdl"
}.each {
wsdlFile ->
println "Generating " + wsdlFile
javaexec {
classpath configurations.cxf
main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
args '-d', outputDir
args '-b', 'PATH/TO/BINDING/FILE.xjb'
args wsdlFile
}
}
}
}
I put it all into gist, if you want to see it complete, gist is here.
To solve this try adding the buildScript before the plugin declarations and apply the wsdl plugin after the plugin block for example:
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'war'
id "org.sonarqube" version "2.7"
}
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
for JDK8 you can update gradle with the following and specify cxfVersion under wsdl2java
apply plugin: 'no.nils.wsdl2java'
wsdl2java {
wsdlDir = file("$projectDir/src/main/resources/fooSchemas")
cxfVersion = '3.3.2'
wsdlsToGenerate = [
["$projectDir/src/main/resources/emailSchemas/FooService.wsdl"]
]
}

Spring Boot in STS isn't using generated code classes

I am trying to use mapstruct generated classes on a new spring-boot project(personal), and it seems my build script requires something else.
The classes are being generated correctly cause I can see them(java and class files in the build folder) and when the application is executed from the jar file, it actually works.
The problem is that when it is executed from eclipse STS, it says spring cant find the generated clases, and yes I made sure, they are created using #Component, and are in the ComponentScanPath.
build.gradle
buildscript {
ext {
springBootVersion = '1.1.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
ext {
javaLanguageLevel = '1.8'
generatedMapperSourcesDir = "${buildDir}/generated-src/mapstruct/main"
}
configurations {
mapstruct
}
sourceSets.main {
ext.originalJavaSrcDirs = java.srcDirs
java.srcDir "${generatedMapperSourcesDir}"
}
jar {
baseName = 'MtgGrimoire'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-websocket")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-ws")
compile('org.postgresql:postgresql:9.3-1102-jdbc41')
compile( 'org.mapstruct:mapstruct:1.0.0.Beta1' )
compile fileTree(dir: 'libs', include: ['*.jar'])
mapstruct( 'org.mapstruct:mapstruct-processor:1.0.0.Beta1' )
testCompile("org.springframework.boot:spring-boot-starter-test")
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.12'
}
task generateMainMapperClasses(type: JavaCompile) {
ext.aptDumpDir = file( "${buildDir}/tmp/apt/mapstruct" )
destinationDir = aptDumpDir
classpath = compileJava.classpath + configurations.mapstruct
source = sourceSets.main.originalJavaSrcDirs
ext.sourceDestDir = file ( "$generatedMapperSourcesDir" )
options.define(
compilerArgs: [
"-nowarn",
"-proc:only",
"-encoding", "UTF-8",
"-processor", "org.mapstruct.ap.MappingProcessor",
"-s", sourceDestDir.absolutePath,
"-source", rootProject.javaLanguageLevel,
"-target", rootProject.javaLanguageLevel,
]
);
inputs.dir source
outputs.dir generatedMapperSourcesDir;
doFirst {
sourceDestDir.mkdirs()
}
doLast {
aptDumpDir.delete()
}
}
compileJava.dependsOn generateMainMapperClasses
Also it seems it isn't generated inside the project bin folder

Categories

Resources