Gradle compileJava Task keeps failing - java

alright i've been hiting my head against the wall for quite sometime now, and now i dont even know what to search for to find a solution, here are my files
build.gradle
//Applying the Gradle BND Plugin for Workspace Builds
//https://github.com/bndtools/bnd/blob/master/biz.aQute.bnd.gradle/README.md
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:${bnd_version}"
}
}
apply plugin: 'biz.aQute.bnd.workspace'
apply plugin: 'java'
// Repositorios, aguante Maven Central.
repositories {
mavenCentral()
/* Excluded, uso la dependecia de otro lado ahora.
flatDir {
dirs '/home/feddericokz/devTools/Equinox/Equinox-Oxygen-1a/plugins'
}
*/
}
// Dependencias
dependencies {
// https://mvnrepository.com/artifact/org.osgi/org.osgi.core
compile group: 'org.osgi', name: 'org.osgi.core', version: '6.0.0'
}
settings.gradle
/*
* This settings file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* In a single project build this file can be empty or even removed.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.3.1/userguide/multi_project_builds.html
*/
/*
// To declare projects as part of a multi-project build use the 'include' method
include 'shared'
include 'api'
include 'services:webservice'
*/
rootProject.name = 'bndWorkspace'
include 'com.feddericokz.helloworld'
when trying to run gradle jar from the command line, i get an error as it the compiler cant find the osgi dependencies to compile the classes
Task :com.feddericokz.helloworld:compileJava FAILED
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:3: error: package org.osgi.framework does not exist
import org.osgi.framework.BundleActivator;
^
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:4: error: package org.osgi.framework does not exist
import org.osgi.framework.BundleContext;
^
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:6: error: cannot find symbol
public class HelloWorldActivator implements BundleActivator {
^
symbol: class BundleActivator
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:8: error: cannot find symbol
public void start(BundleContext bundleContext) throws Exception {
^
symbol: class BundleContext
location: class HelloWorldActivator
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:13: error: cannot find symbol
public void stop(BundleContext bundleContext) throws Exception {
^
symbol: class BundleContext
location: class HelloWorldActivator
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:7: error: method does not override or implement a method from a supertype
#Override
^
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:12: error: method does not override or implement a method from a supertype
#Override
^
7 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':com.feddericokz.helloworld:compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
2 actionable tasks: 1 executed, 1 up-to-date
What am i doing wrong?
EDIT: a typo

If you are using a Bnd workspace model build, then you must configure the build path through each project's bnd.bnd file using the -buildpath Bnd instruction. Then the Bnd gradle plugin will use that information to program the configurations for java compilation.
Your example shows setting a compile dependency for the root gradle project which is wrong since (1) you should be using -buildpath in the project's bnd.bnd file and (2) it is done in the root project which is not meaningful to any child projects like your com.feddericokz.helloworld project.
So change your build.gradle file to not apply the 'java' plugin to the root project and not set compile dependencies for the root project and change your Bnd workspace so that the workspace (cnf) has configured repositories to access the desired bundles, for example https://github.com/osgi/enroute.workspace/blob/4070ff6668a1ee79b9b01cfa4caab86869247e7b/cnf/ext/enroute.bnd#L22-L28, and then set each project's bnd.bnd file to have the desired bundles on the -buildpath.

It is unclear if bnd_version is defined in this line:
classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:${bnd_version}"
Consider adding a gradle.properties file with:
bnd_version=3.5.0
(or whatever version is apporpriate). As best as I can reproduce your situation, my example works for me with this.

Related

Gradle not resolving JRE dependency

I am a beginner with Gradle projects and am struggling to build a project.
I am receiving this error when running gradle -PmainClass run:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Could not resolve com.google.guava:guava:28.2-jre.
Required by:
project :
> Could not resolve com.google.guava:guava:28.2-jre.
> Could not get resource 'https://jcenter.bintray.com/com/google/guava/guava/28.2-jre/guava-28.2-jre.pom'.
> Could not GET 'https://jcenter.bintray.com/com/google/guava/guava/28.2-jre/guava-28.2-jre.pom'.
> sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 11s
1 actionable task: 1 executed
I am trying to run a Java application here by following the first example in this post.
For clarification, I do have an internet connection. I'm wondering if there is some sort of certificate I need.
Here is my build.gradle:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.3/userguide/tutorial_java_projects.html
*/
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
implementation files('./lib/jython-standalone-2.7.0.jar')
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
application {
// Define the main class for the application.
mainClassName = 'Zumi.App'
}
Any help is appreciated. Thanks!
Add mavenCentral() to your repositories block,
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
implementation files('./lib/jython-standalone-2.7.0.jar')
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
application {
// Define the main class for the application.
mainClassName = 'Zumi.App'
}

How to resolve java error: package javax.annotation is not visible, Java version 9

When building below Spring session sample using gradle:
https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-findbyusername.html
I encountered an error regarding to java.annotation module, anybody have idea how to resolve that?
/spring-session/spring-session-core/src/main/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfiguration.java:22: error: package javax.annotation is not visible
import javax.annotation.PostConstruct;
^
(package javax.annotation is declared in module java.xml.ws.annotation, which is not in the module graph)
warning: unknown enum constant When.MAYBE
reason: class file for javax.annotation.meta.When not found
1 error
1 warning
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':spring-session-core:compileJava'.
> Compilation failed; see the compiler error output for details.
I tried to add below config in build.gradle however the problem remain.
tasks.withType(AbstractCompile) {
options.compilerArgs += ["--add-modules", "java.xml.bind"]
}
tasks.withType(Test) {
jvmArgs += ["--add-modules", "java.xml.bind"]
}
I was getting the same error:
warning: unknown enum constant When.MAYBE
reason: class file for javax.annotation.meta.When not found
For me it was becuase I had annotation processing with lombok which wasn't picked-up with the project dependencies.
In the end I added annotationProcessor as part of the dependencies as follows:
dependencies {
// ...
compile('org.springframework.boot:spring-boot-starter-web')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
// ...
annotationProcessor('org.projectlombok:lombok')
}
Seem the sample is not yet upgraded to support java9, downgrade to java8 it runs well.

Generated tests fail in Spring Cloud Contract

I am new to spring-cloud-contract. Just trying to configure my build and I am getting error.
Here is my project structure
Project
| ==> Module
I have the following dependencies for classpath in my project build.gradle
Project Build file
buildscript {
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.2.1"
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:1.1.3.RELEASE"
}
apply from: "$projectDir/gradle/app/springcloud.gradle" //Gradle build file in module
}
Module Build file
apply plugin: 'groovy'
apply plugin: 'spring-cloud-contract'
apply plugin: 'org.springframework.boot'
dependencies {
//Spring cloud contract dependencies
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.cloud:spring-cloud-starter-contract-verifier')
testCompile "org.springframework.cloud:spring-cloud-starter-contract-stub-runner"
testCompile "com.jayway.restassured:rest-assured:2.5.0"
testCompile "com.jayway.restassured:spring-mock-mvc:2.5.0"
}
//Spring cloud contract dependency management
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE"
}
}
I have a groovy file which has a post request and a response. When I do gradle build, the tests are autogenerated but my build is feeling.
C:\Users\user\Projects\myProject\module\build\generated-test-sources\contracts\org\springframework\cloud\contract\verifier\tests\ContractVerifierTest.java:3: error: cannot find symbol
import com.jayway.jsonpath.DocumentContext;
^
symbol: class DocumentContext
location: package com.jayway.jsonpath
C:\Users\user\Projects\myProject\module\build\generated-test-sources\contracts\org\springframework\cloud\contract\verifier\tests\ContractVerifierTest.java:28: error: cannot find symbol
DocumentContext parsedJson =
JsonPath.parse(response.getBody().asString());
^
Am I doing something wrong? Is there any mismatch in the versions which I am using?
#Marcin Thanks for your inputs. This is how I solved this issue.
The build fails inside generated tests and jayway jsonpath is not showing in Red colour at the position of error. When I Ctrl + Hover (mouse) on the import it's referencing to com.jayway.jsonpath:json-path:2.2.0 which is not true. The reason is because I have imported many modules in Intellij and apparently one of the module also imports 2.2.0 and intellij is referencing it from external library.
So I did invalidate cache intellij and closed all unnecessary projects and reopened the project alone which I am working on, in a new window (Fresh session). Now gradle build and again I got the same error on the generated tests. But this time When I Ctrl + Hover (mouse) on the import its refering to the com.jayway.jsonpath:json-path:0.9.1 which is what I am expecting.
Now to solve this issue, I've examined the dependency tree using the below command and did some grep on it.
allDeps --configuration testRuntime
I found the answer, why gradle is replacing 2.2.0 with 0.9.1. I've figured out some project dependency is replacing it. Now the solution for this is modifying my module build file for jayway json-path as shown below. After that I've given build gradle command and now the errors are gone.
testCompile ('com.jayway.jsonpath:json-path:2.2.0') {
force = true
}
And finally, I've got exception saying that SpringCloudContractAssertions unable to find.
org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
So I replace Camden release with Dalston as shown below. Now it all works fine.
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE"
}
}

Could not determine the dependencies of task ':xxx:check'

I have a project using Gradle 1.12, it goes well using gradle assemble, but exception occurs when executing gradle build as follows:
* What went wrong:
Could not determine the dependencies of task ':JavaUtil:check'.
> groovy.lang.MissingMethodException: No signature of method: org.gradle.api.internal.file.DefaultSourceDirectorySet.getTaskName() is applicable for
rgument types: (java.lang.String, null) values: [findbugs, null]
Possible solutions: getName()
Having the dependencies settings in gradle script of JavaUtil's parent folder like this:
dependencies {
compile (project (':JavaUtil')) {transitive = false}
The same error occurs when I executed gradle build in JavaUtil folder.
Any ideas what happens here?

Gradle dependency - class not found

I have a Gradle project in IntelliJ.
I added this dependency to Maven dependencies list
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
When I try to compile my code using gradlew build I receive this compilation error:
gradlew build
:compileJava
....java:5: error: package org.imgscalr does not exist
import org.imgscalr.Scalr;
^
....java:52: error: cannot find symbol
BufferedImage thumbnail = Scalr.resize(image, 150);
^
symbol: variable Scalr
location: class PlaceController
2 errors
:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
What is happening?
After adding the dependency inside maven file, IntelliJ automatically downloads the file and imports it where it is needed. By doing this, the error from the IDE, that says tahat Scalr could not be found, dissapears.
You should be covering this in your build.gradle file instead. Here's an example of how it would look.
repositories {
mavenCentral()
}
dependencies {
compile 'org.imgscalr:imgscalr-lib:4.2'
}
If you were looking to do a wholesale conversion from Maven to Gradle, you'd have to convert your POMs over first and iron out any bugs that may have resulted due to the conversion.

Categories