gradle dependencies for main and test - gradle

Selenide + Gradle When declaring a dependency like this:
testImplementation group: 'com.codeborne', name: 'selenide', version: '6.6.3' , I can only access Selenid elements and methods in test/java. But the page objects are in the src/main/java section. How should I write a dependency so that methods can be called both in test and in main? I did this, but it seems to me that it is wrong:
dependencies {
testImplementation group: 'com.codeborne', name: 'selenide', version: '6.6.3'
compileOnly group: 'com.codeborne', name: 'selenide', version: '6.6.3'
}

testImplentation means the dependency only has scope for testing. Change it to just implentation and it should work.

Related

The service APIs should not be bundled with the implementations.; creating "Ghost" implementation

I am working on the following Nifi controller service bundle:
nifi-controllers-bundle
nifi-controllers
nifi-controllers-api
nifi-controllers-nar
nifi-controllers-api-nar
I have a custom controller service created(StandardMyService) that extends DBCPConnectionPool in the nifi-controllers module and when I try to use it in UI I get the following exception in logs: ERROR [NiFi Web Server-36] o.a.nifi.controller.ExtensionBuilder Could not create Controller Service of type com.xxx.yyy.StandardMyService for ID 812de259-0181-1000-3536-a06e3ae9f2ed due to: Controller Service com.xxx.yyy.StandardMyService is bundled with its supporting APIs org.apache.nifi.dbcp.DBCPService. The service APIs should not be bundled with the implementations.; creating "Ghost" implementation and this one in UI: 'Missing Controller Service' validated angainst 'Any property' is invalid because controller service is of type com.xxx.yyy.StandardMyService but this is not a valid Reporting Task Type
I do not think there is something wrong with the way I have implemented the StandardMyService class because I have tested it in a nifi controller service maven archetype and it works.
My nifi-controllers build.gradle looks like this:
dependencies {
implementation group: 'com.oracle.ojdbc', name: 'ojdbc8', version: '19.3.0.0'
implementation group: 'org.apache.nifi', name: 'nifi-nar-bundles', version: '1.12.0', ext: 'pom'
compileOnly group: 'org.apache.nifi', name: 'nifi-api', version: '1.16.1'
implementation group: 'org.apache.nifi', name: 'nifi-utils', version: '1.16.1'
compileOnly group: 'org.apache.nifi', name: 'nifi-dbcp-service-api', version: '1.16.1'
implementation group: 'org.apache.nifi', name: 'nifi-dbcp-service-nar', version: '1.16.1'
compileOnly group: 'org.apache.nifi', name: 'nifi-dbcp-service', version: '1.16.1'
testImplementation group: 'org.apache.nifi', name: 'nifi-mock', version: '1.16.1'
testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.36'
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
implementation group: 'com.fasterxml', name: 'aalto-xml', version: '1.3.2'
}
tasks.named('test') {
useJUnitPlatform()
}
I got it to work by extending AbstractControllerService instead of DBCPConnectionPool, therefore not needing org.apache.nifi.dbcp.DBCPService anymore.
LE: Having the project built with gradle, there is no
<type>nar</type>
for nifi-standard-services-api-nar that puts this dependency in the MANIFEST file of the nar archive of processors-nar. When I added manually these lines in MANIFEST file, everything worked:
Nar-Dependency-Id: nifi-standard-services-api-nar
Nar-Dependency-Group: org.apache.nifi
and the only dependency needed in the project is nifi-dbcp-service-api in processors build.gradle.

Unable to fetch gradle dependency net.devh:grpc-server-spring-boot-starter

I am using grpc springboot. I can download all dependencies apart from 'net.devh:grpc-server-spring-boot-starter:2.12.0.RELEASE'.
Due to the I can't use annotation #GrpcService
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'io.grpc:grpc-netty-shaded:1.37.0'
implementation 'io.grpc:grpc-protobuf:1.37.0'
implementation 'io.grpc:grpc-stub:1.37.0'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
implementation 'org.springframework.boot:spring-boot-starter:2.4.5'
implementation 'net.devh:grpc-server-spring-boot-starter:2.12.0.RELEASE'
compileOnly 'jakarta.annotation:jakarta.annotation-api:1.3.5'
}
Any help much appreciated
Just try the install dependencies and reimport them.
If it doesn't work, close the IDE and open it again.

Multiple Build-Tasks with changing sets of dependencies in Gradle

I would like to run tests of an library against different backend implementations.
The implementations are injected in my project by the magic of Spring.
Currently I do this, by changing the line
testImplementation group: 'org.mycorp.exmpl' name: 'redis-backend' version: '0.0.1'
to
testImplementation group: 'org.mycorp.exmpl' name: 'mongo-backend' version: '0.0.1'
to
testImplementation group: 'org.mycorp.exmpl' name: '<whatever-backend>' version: '<whatever.version>'
then running
gradlew test
(resp. clicking on 'test' in my IntelliJ IDE)
This is cumbersome. And I can't do that with my build pipeline (I guess).
In my imagination, I would be able to create
a task testRedis which depends on the task buildRedisTestClasses
a task testMongo which depends on the task buildMongoTestClasses
...
so I can just select the correct task in my IDE or run all of them in my build pipeline.
I found some hints on the web, that you can use something like this:
dependencies {
testImplementation group: 'org.mycorp.exmpl', name: 'redis-backend', version: '0.0.1', configuration: 'redis'
testImplementation group: 'org.mycorp.exmpl', name: 'mongo-backend', version: '0.0.1', configuration: 'mongo'
}
But thats about where I stopped finding/understanding.
(As you might notice, I am a gradle novice)
Try something alike:
./gradlew test -Dbackend=redis
dependencies {
if (System.getProperty("backend") == "redis") {
implementation "org.mycorp.exmpl:redis-backend:0.0.1"
} else {
implementation "org.mycorp.exmpl:mongo-backend:0.0.1"
}
}
Or even:
./gradlew test -Dbackend=redis-backend:0.0.1
implementation "org.mycorp.exmpl:${System.getProperty("backend")}"
Making it a project property -Pbackend with a default value suggested.
See Environment Options.

Gradle 4.x API instead of implementation - How can i use gradle API feature to expose internal dependencies

I'm using gradle 4.x and here is my problem.
I've two modules A and B.
Module B has project dependency on module A. Here are the gradle files.
build.gradle for module A
apply plugin: 'java-library'
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter'
implementation group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'io.github.benas', name: 'random-beans', version: '3.7.0'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
Here is the build.gradle for module B
dependencies {
implementation project(':moduleA')
implementation group: 'org.springframework.boot', name: 'spring-boot-starter'
implementation group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'io.github.benas', name: 'random-beans', version: '3.7.0' }
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
If you observe the build.gradle file for module B, i've a dependency for spring-kafka but it's a duplicate since it's already in module A build.gradle.
Now how can use gradle API option to expose spring-kafka (from module A) without actually mentioning it in module B's build.gradle when the jar file of module B is being consumed by another app?
Please suggest.
This is explained in the docs here (emphasis mine):
The plugin exposes two configurations that can be used to declare dependencies: api and implementation. The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.
So simply change implementation to api in moduleA:
api group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
Then remove the entry from moduleB's Gradle file.

Error building project while excluding transitive dependency in build.gradle file

I am creating a multi module project with Spring Boot for deploying my application and using gradle as my build tool.
Now, I am creating independent deployments for some modules in the project. Some of the project requires Embedded tomcat and some do not. All the common dependencies have been put on a common project and all other modules are dependent on this common project.
Most of the other deployment modules require an embedded tomcat application server and other web components (provided by org.springframework.boot', name: 'spring-boot-starter-web) so this has been included in the build.gradle for common project
Here is the build.gradle for common project:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: springBootVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBootVersion
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: springDataJpaVersion
Now, one of the other modules which is going to be deployed independently does not require this embedded tomcat and other mvc jars which comes with including spring-boot-starter-web but requires the other transitive dependencies from common project. As such, I want to exclude the transitive dependency for
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
I am doing it like this in the build.gradle other project
dependencies {
compile project(':common') {
exclude group 'org.springframework.boot', module:'spring-boot-starter-web'
}
}
But while building it is throwing this error:
startup failed: build file
'/Users/user1/Documents/repo/storm/build.gradle': 30: expecting '}',
found ',' # line 30, column 44.
oup 'org.springframework.boot', module:'
changing it to:
dependencies {
compile project(':common'){
exclude group 'org.springframework.boot:spring-boot-starter-web'
}
}
throws:
Could not find method exclude() for arguments [parent-project name] on
project ':common'.
How can I exclude transitive dependency here?
You need to invoke exclude on compile not on project:
dependencies {
compile(project(':common')) {
exclude group: 'org.springframework.boot', module:'spring-boot-starter-web'
}
}

Resources