Build Failed when specified dependency version using variable - java

I am trying to migrate my maven project to gradle. I specify spring version for all the project in variable springVersion. But from some reason build fails on one particular dependency org.springframework:spring-web:springVersion. When I type the version directly org.springframework:spring-web:3.1.2.RELEASE everything compiles. Here is my build.gradle file:
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
ext {
springVersion = "3.1.2.RELEASE"
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework:spring-context:springVersion'
compile 'org.springframework:spring-web:springVersion'
compile 'org.springframework:spring-core:springVersion'
compile 'org.springframework:spring-beans:springVersion'
testCompile 'org.springframework:spring-test:3.1.2.RELEASE'
testCompile 'org.slf4j:slf4j-log4j12:1.6.6'
testCompile 'junit:junit:4.10'
}
version = '1.0'
jar {
manifest.attributes provider: 'gradle'
}
}
ERROR MESSAGE:
* What went wrong:
Could not resolve all dependencies for configuration ':hi-db:compile'.
> Could not find group:org.springframework, module:spring-web, version:springVersion.
Required by:
hedgehog-investigator-project:hi-db:1.0
The same is with org.springframework:spring-test:3.1.2.RELEASE when performing tests.
Whats causing he problem and how to solve it?

You are using springVersion as the version, literally. The correct way to declare the dependencies is:
// notice the double quotes and dollar sign
compile "org.springframework:spring-context:$springVersion"
This is using Groovy String interpolation, a distinguishing feature of Groovy's double-quoted strings. Or, if you want to do it the Java way:
// could use single-quoted strings here
compile("org.springframework:spring-context:" + springVersion)
I don't recommend the latter, but it hopefully helps to explain why your code doesn't work.

Or you can define lib version via variable in dependencies like this:
dependencies {
def tomcatVersion = '7.0.57'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}
}

Related

How to use gradle 'api' dependency

I tried using the 'api' dependency keyword in my project , but I got this error saying it cannot find method api()
I tried it on a new project. this is the build.gradle file:
plugins {
id 'java'
}
group 'com.test'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
api group: 'com.google.guava', name: 'guava', version: '27.0.1-jre'
}
I am using gradle V4.9.
when I run gradle build I get this:
Could not find method api() for arguments [{group=com.google.guava, name=guava, version=27.0.1-jre}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
If I replace 'api' with 'implementation' everything works fine
What am I missing here?
Is there any setting that needs to be done?
The api configuration comes from the java-library plugin, in your build script you have just applied java plugin. See https://docs.gradle.org/current/userguide/java_library_plugin.html
The key difference between the standard Java plugin and the Java Library plugin is that the latter introduces the concept of an API exposed to consumers. A library is a Java component meant to be consumed by other components. It’s a very common use case in multi-project builds, but also as soon as you have external dependencies.
Just apply the java-library plugin (which extends java plugin) and it should work:
plugins {
id 'java-library'
}
For those using kotlin build scripts use the following:
plugins {
id("org.gradle.kotlin.kotlin-dsl")
}

Gradle not able to detect osdetector plugin

I am using OpenSSL for my Java GRPC project which uses gradle.
I read the documetation which mentions that we should make security settings as mentioned in this link.
I have included the osdetector plugin in the build.gradle file.
But when I build the project, gradle is not able to resolve the osdetector plugin and throwing error
> Failed to apply plugin [id 'com.google.protobuf']
> Plugin with id 'osdetector' not found.
My gradle file is as follows:
def neo4jVersion = "2.3.3"
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'
apply plugin: 'com.google.osdetector'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.3'
classpath 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
}
}
protobuf {
protoc {
// The version of protoc must match protobuf-java. If you don't depend on
// protobuf-java directly, you will be transitively depending on the
// protobuf-java version that grpc depends on.
artifact = "com.google.protobuf:protoc:3.0.0-beta-2"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:0.12.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
generatedFilesBaseDir = "$projectDir/src/generated"
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'io.grpc:grpc-all:0.12.0'
compile "org.neo4j:neo4j:${neo4jVersion}"
compile "org.neo4j:neo4j-ogm-core:2.0.1"
compile "org.neo4j:neo4j-ogm-http-driver:2.0.1"
compile 'com.google.apis:google-api-services-storage:v1-rev71-1.22.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
compile 'io.netty:netty-tcnative-boringssl-static:1.1.33.Fork14:' + osdetector.classifier
}
The compile dependency alone is getting resolved however.
I think I am missing something basic here. Please let me know the solution.
Updated
protobuf-gradle-plugin is not compatible with osdetector-gradle-plugin-1.4.0, since that version changed the name of the plugin. Swapping to version osdetector 1.2.1 should fix the problem.

Gradle can't find package in dependent test project

I'm trying to run ./gradlew build for a nested multi project structure and I'm running into issues that only seem to appear for projects with test source roots. Being new to both java and gradle I'm sure I'm breaking more than one convention, but I still think this should be working.
Essentially, all the dependencies seem to be added fine, but when I have one project that only has a Test srcDir that depends on another project that has a Test srcDir, it doesn't recognize packages/symbols in that root project. However, projects with regular srcDirs (not test) don't seem to have a problem.
My project has more going on than this, but here is the simplest setup I've tried that illustrates the problem.
My project structure:
qatests
/Applications
/AppGroupName
/AppBasePageObjects
/src
/AppBaseTests
/src
/BasePageObjects
/src
/BaseTests
/src
My settings.gradle in qatests:
rootProject.name = 'QaTests'
include 'BasePageObjects'
include 'BaseTests'
include 'Applications:AppGroupName'
include 'Applications:AppGroupName:AppBasePageObjects'
include 'Applications:AppGroupName:AppBaseTests'
My build.gradle in qatests:
version '1.0-SNAPSHOT'
allprojects {
repositories {
mavenCentral()
}
}
subprojects{
if(project.name.contains("Tests")){
apply plugin: 'java'
sourceCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+'
}
if(project.name != "BaseTests")
{
println "$project.name depends on BaseTests"
dependencies {
testCompile project(':BaseTests')
}
}
sourceSets {
test {
java {
srcDir 'src'
}
}
}
}
if(project.name.contains("PageObjects")){
apply plugin: 'java'
sourceCompatibility = 1.8
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+'
}
if(project.name !="BasePageObjects")
{
dependencies {
compile project(':BasePageObjects')
}
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
}
}
I won't include my build.grade for the BaseTest project since that seems to compile fine during the gradlew build, but here is my build.gradle for AppBaseTests:
version '1.0-SNAPSHOT'
dependencies {
compile 'org.apache.commons:commons-lang3:3.4'
compile project(':Applications:AppGroupName:AppBasePageObjects')
}
When I run ./gradlew build in the qatests root, the BaseTests, BasePageObjects, and AppBasePageObjects projects seem to compile fine and AppBasePageObjects successfully uses packages and symbols from BasePageObjects. For some reason however, AppBaseTests can't seem to recognize packages and symbols in BaseTests.
If I clone this project from scratch, IntelliJ runs the gradle scripts scripts automatically and everything seems to work out of the box just fine with the dependencies, so this just confuses me even more.
I've tried adding compile and testcompile for all the project dependencies since that's the only real difference between AppBasePageObjects which works and AppBaseTests which doesn't work. I've also tried adding compileJava.dependsOn(':BaseTests:build') to the AppBaseTests build.gradle file. And a few other rabbit holes, but nothing seems to have any effect on this dependency issue.
For what it's worth, here is the first error I see in the actual build:
error: package Tests does not exist import Tests.BaseTest;
Any help or suggestions are greatly appreciated. If you would like to distribute some harsh insults I'll take those as well. Thank you.
I believe I found an answer while reading another solution here:
Multi-project test dependencies with gradle
It seems I needed to use testCompile project(':BaseTests').sourceSets.test.output for command line builds as well as testCompile project(':BaseTests') for IDE functionality in my root build.gradle file. This only seems to be required for the test projects.
Here are the actual changes in my root build.gradle file:
if(project.name != "BaseTests")
{
println "$project.name depends on BaseTests"
dependencies {
testCompile project(':BaseTests')
testCompile project(':BaseTests').sourceSets.test.output
}
}
This seems a bit hacky but works, if somebody has a more thorough and intelligent answer please post!

How to control H2 driver version in Grails 3 Gradle project?

How to control H2 driver version in Grails/Gradle project?
Having problems with running Grails 3 app with H2 I found this answer: Grails accessing H2 TCP server hangs stating it may be caused by driver version difference.
My IDE reports Grails app uses 1.3.176 version of H2, while my server has 1.4.190. So, I would like to upgrade app's H2, but can't find where it is defined. I sought all project files and found no version definition.
UPDATE
My current build.gradle:
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
classpath "org.grails.plugins:hibernate:4.3.10.5"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
}
version "0.1"
group "multipleparentsgrails"
apply plugin: "spring-boot"
apply plugin: "war"
apply plugin: "asset-pipeline"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
assets {
minifyJs = true
minifyCss = true
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:hibernate"
compile "org.grails.plugins:cache"
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:scaffolding"
runtime "org.grails.plugins:asset-pipeline"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
// Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'
console "org.grails:grails-console"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
You should be able to specify it like any other dependency
runtime "com.h2database:h2:1.4.190"
There are many dependencies which may use H2. grails, hibernate, others.
I would go into your project. Let's say it's $HOME/projects/myproj.
1) Do a dependency report. Pipe it into grep so you don't have to wade through a 1,000 line report, and see what versions of H2 are being used.
cd $HOME/projects/myproj
./gradlew dependencies | grep 'H2'
2) Find the highest version number, and then explicitly include this in your build.gradle to force every dependency to use the most current version:
dependencies {
// all the other dependencies
runtime "com.h2database:h2:1.4.190" // where 1.4.190 is the most
// current version. as i
// type this it is 1.4.191
// according to maven central
}

gradle 2.8 subproject dependencies - compile() not found

I have a root gradle project and 10 subprojects. I want 5 dependencies to be specified in root project only, without copy-pasting them to all 10 modules. If I write:
subprojects{
dependencies{
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile 'org.springframework:spring-core:4.1.2.RELEASE'
compile 'org.springframework:spring-context:4.1.2.RELEASE'
}
}
it says to me that compile() method has not been found. How to make it work so I should specify deps only in one place?
Have you applied java plugin with:
allprojects {
apply plugin: 'java'
}
or:
subprojects {
apply plugin: 'java'
}
?
Please remember that you also need to add repositories block. It will be:
repositories {
mavenCentral()
jcenter()
}
Gradle Doc Reference: 8.2. Declaring your dependencies
You must wrap the compile dependencies with the dependencies keyword like so:
subprojects {
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile 'org.springframework:spring-core:4.1.2.RELEASE'
compile 'org.springframework:spring-context:4.1.2.RELEASE'
}
}
This applies to your root-only libraries as well.

Categories