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')
}
}
Related
I am beating against a wall for a whole week. I can't really see the reason why it doesn't work. I have a project where I want to run my Java project with the following build.gradle config:
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.3'
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id 'org.web3j' version "4.8.4"
}
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
group = 'com.suplab'
sourceSets {
main {
solidity {
srcDir "contracts"
}
}
test {
solidity {
srcDir "contracts"
}
}
}
ext {
web3jVersion = '4.8.4'
}
npmInstall {
enabled false
}
dependencies {
implementation "org.web3j:core:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate:hibernate-core:5.6.5.Final'
}
I use gradle version 6.8, since it was specified by developer of the plugin. When I try to build this project from the parent, root folder that is the root of the project, it fails with following error:
A problem occurred configuring project ':api'.
> Failed to notify project evaluation listener.
> Task with name 'resolveSolidity' not found in project ':api'.
> Could not create task ':api:generateTestContractWrappers'.
> Task with name 'compileTestSolidity' not found in project ':api'.
I can't figure it out. I checked it against the build that being generated with web3j-cli, and it's the same for the configuration property (both configs have it blank). So, I don't see any reason WHY it fails. Here's the webj3-cli generated build file:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
id "com.github.johnrengelman.shadow" version "5.2.0"
id 'org.web3j' version '4.8.4'
}
group 'org.web3j'
version '0.1.0'
sourceCompatibility = 1.8
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
web3j {
generatedPackageName = 'org.web3j.generated.contracts'
excludedContracts = ['Mortal']
}
ext {
web3jVersion = '4.8.4'
logbackVersion = '1.2.3'
}
dependencies {
implementation "org.web3j:core:$web3jVersion",
"ch.qos.logback:logback-core:$logbackVersion",
"ch.qos.logback:logback-classic:$logbackVersion"
implementation "org.web3j:web3j-unit:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
jar {
manifest {
attributes(
'Main-Class': 'org.web3j.Web3App',
'Multi-Release':'true'
)
}
}
application {
mainClassName = 'org.web3j.Web3App'
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Exactly what the title says. For the record, I am using OpenJDK 15.0.1 and Gradle 6.7. I am trying to run code in a subproject with dependencies. When I try, I get a compiler error like so:
> Task :browser-core:compileJava FAILED
1 actionable task: 1 executed
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:3: error: module not found: javafx.graphics
requires javafx.graphics;
^
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:5: error: module not found: net.sf.cssbox
requires net.sf.cssbox;
^
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:6: error: module not found: net.sf.cssbox.jstyleparser
requires net.sf.cssbox.jstyleparser;
^
As far as I'm concerned, the dependencies in question (JavaFX, CSSBox) are already specified under the root build.gradle file. Why does the compiler overlook these dependencies, and how do I fix it?
For reference, here's my root build.gradle:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
java {
modularity.inferModulePath = true
}
allprojects {
plugins.withType(JavaPlugin, {
dependencies {
//determine dependency names
def os = org.gradle.internal.os.OperatingSystem.current()
def fxDep = ""
if (os.isWindows()) {
fxDep = "win"
}
else if (os.isMacOsX()) {
fxDep = "mac"
}
else if (os.isLinux()) {
fxDep = "linux"
}
//implementation 'org.slf4j:slf4j-jdk14:1.7.30'
implementation 'org.slf4j:slf4j-nop:1.7.30'
implementation('net.sf.cssbox:cssbox:5.0.0') {
exclude group: 'xml-apis', module: 'xml-apis'
}
implementation "org.openjfx:javafx-base:15.0.1:${fxDep}"
implementation "org.openjfx:javafx-controls:15.0.1:${fxDep}"
implementation "org.openjfx:javafx-graphics:15.0.1:${fxDep}"
implementation "org.openjfx:javafx-fxml:15.0.1:${fxDep}"
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
}
})
}
test {
useJUnitPlatform()
}
and the subproject's build.gradle:
plugins {
id 'application'
}
group 'io.github.jgcodes'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
It turns out that
java {
modularity.inferModulePath = true;
}
needs to be set for all subprojects individually. Specifically, you need to place it in the allprojects closure.
I am trying to combine a Spring Boot backend api and a React frontend app into one single jar.
I was able to build a jar file containing both by following this article (https://medium.com/xebia-engineering/a-minimalistic-guide-to-building-and-deploying-monolithic-spring-boot-react-applications-39440035b27) but the frontend jar ends up in BOOT-INF/lib/frontend.jar instead of META-INF/resources. How can I create a jar file with the webjar in the right directory so it can render a React page?
frontend/build.gradle
plugins {
id "com.moowork.node" version "1.3.1"
}
apply plugin: "java"
apply plugin: "com.moowork.node"
node {
version = "12.9.1"
download = true
}
task bundle(type: NpmTask, dependsOn: npmInstall) {
args = ["run", "build"]
}
task uiTest(type: NpmTask) {
environment = ["CI": "true"]
args = ["run", "test"]
}
task run(type: NpmTask) {
args = ["start"]
}
check.dependsOn(test)
jar.dependsOn(bundle)
task webjar(type: Jar) {
from(fileTree("build")) {
into "META-INF/resources"
}
}
check.dependsOn(test)
jar.dependsOn(bundle)
jar.finalizedBy("webjar")
backend/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven {
url 'https://repo.spring.io/libs-milestone'
}
}
jar {
manifest {
attributes "Main-Class": "com.example.kasahararestapi.StockCheckAppApplication"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Test.class" // whatever Ant pattern matches your test class files
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.social:spring-social-github:1.0.0.M4'
implementation 'commons-io:commons-io:2.6'
implementation 'org.apache.commons:commons-lang3:3.0'
implementation 'io.jsonwebtoken:jjwt:0.9.0'
implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.807')
implementation 'com.amazonaws:aws-java-sdk-s3'
implementation 'org.springframework.boot:spring-boot-starter-mail:1.2.0.RELEASE'
implementation 'org.modelmapper:modelmapper:2.3.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
implementation project(path: ':frontend', configuration: 'default')
}
setting.gradle
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'stock_check_app'
include 'backend'
include 'frontend'
I have a simple multi-project structure as follows:
|- gps-framework
|- build.gradle
|- settings.gradle
|- gps-web-service
|----build.gradle
|----src/main....
Under the root build.gradle located in gps-framework/ looks like this:
plugins {
id 'java'
id "java-library"
id "maven-publish"
id "jacoco"
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
subprojects {
subproject ->
apply plugin: 'maven-publish'
apply plugin: 'jacoco'
def isApp = subproject.name == 'gps-web-service'
if (isApp) {
apply plugin: 'java'
} else {
apply plugin: 'java-library'
}
group = 'com.mycompnay'
version = project.hasProperty('customVersion') ? project['customVersion'] : 'integration-SNAPSHOT'
//System.out.println(subproject.name)
compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
options.encoding = 'UTF-8'
}
compileTestJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
mavenLocal()
maven {
name "release"
url "my-custom-mvn-repo/release"
authentication {
//
}
}
maven {
name "snapshot"
url "my-custoom-mvn-repo/snapshot"
authentication {
//
}
}
}
configurations {
runtime.exclude(group: "org.slf4j", module:"slf4j-log4j12")
}
publishing {
repositories {
maven {
authentication {
//
}
def isSnapshot = subproject.version.contains('SNAPSHOT')
url = isSnapshot ? "my-custom-mvn-repo/snapshot" : "my-custom-mnv-repo/release"
}
}
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
if (subproject.hasProperty('jacocoTestCoverageVerification')) {
subproject.check.dependsOn subproject.jacocoTestCoverageVerification
}
test {
testLogging {
exceptionFormat = 'full'
}
}
}
The build.gradle of sub-project gps-web-service located in gps-framework/gps-web-service looks like this:
group = 'com.mycompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
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()
}
From what I have read so far, since the repository settings are defined in the root build.gradle and these settings are injected into these subprojects, I do not need to redefine repository settings. This does not appear to be the case as when I run
./gradlew build
The error I get is:
Could not find org.springframework.boot:spring-boot-starter-web:.
Required by:
project :gps-web-service
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
Am I misunderstanding the whole concept of injection of project properties or what am I missing here? I will try duplicating the repo settings in the sub-projects build.gradle but was hoping this is not necessary.
To solve this specific problem, I had to define the spring boot plugin in the sub-project's build.gradle as follows:
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
The resulting file looks like this:
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
group = 'com.mycompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
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()
}
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