I am trying to build some java 1.6 application with aspects after migrating the build from some older gradle version to gradle 6.3. Because of the newer gradle version, I am using java 9.
The aspect compiler throws ClassCastException (link to log below), which, if I understand correctly, is caused by compiler not finding java.lang.Object class. I am only guessing it might something to do with using java 9 to run the build, but targeting java 1.6 compatibility. If this is the case, how do I tell the compiler where to find it? I tried to pass the -1.6 argument to ajc, but nothing changed.
I also include my build.gradle contents - I apologize it is not fully in gradle 6.3, because I got stuck on this aspectj compilation problem, before migrating it fully.
Thanks in advance for every response.
build.gradle contents:
buildscript {
repositories {
maven {
url "${artifactoryURL}/libs-releases"
}
}
dependencies {
classpath "io.freefair.gradle:aspectj-plugin:5.0.0-rc6"
classpath "gradle.plugin.org.myire:quill:2.3.1"
}
}
apply plugin: 'java'
apply plugin: 'io.freefair.aspectj.post-compile-weaving'
apply plugin: 'org.myire.quill.cobertura'
apply from: 'https://localnet/public/gradle/trunk/repositories.gradle'
java {
sourceCompatibility = JavaVersion.VERSION_1_6
}
group = "cz.svt"
version = "${version}"
aspectj {
version = "1.6.8"
}
configurations.all {
transitive = false
}
dependencies {
compile('avalon:avalon:4.2.0')
compile('cglib:cglib-nodep:2.2')
compile('commons-beanutils:commons-beanutils:1.7.0')
compile('commons-codec:commons-codec:1.3')
compile('commons-collections:commons-collections:3.1')
compile('org.apache.commons:commons-compress:1.4.1')
compile('commons-digester:commons-digester:1.6')
compile('commons-fileupload:commons-fileupload:1.1')
compile('commons-io:commons-io:1.3.1')
compile('commons-lang:commons-lang:2.0')
compile('commons-logging:commons-logging:1.0.4')
compile('commons-net:commons-net:2.0')
compile('commons-pool:commons-pool:1.2')
compile('commons-validator:commons-validator:1.1.3')
compile('com.ibm.db2:jdbc3-driver:9.5')
compile('dom4j:dom4j:1.6.1')
compile('org.codehaus.groovy:groovy-all:1.5.7')
compile('org.apache.xmlgraphics:fop:0.94')
compile('com.google.guava:guava:13.0.1')
compile('org.codehaus.groovy:groovy-all:1.5.7')
compile('cz.svt:hibernate-svt:3.6.8.Final')
compile('com.itextpdf:itextpdf:5.0.2')
compile('joda-time:joda-time:2.1')
compile('javax.servlet:jstl:1.0.6')
compile('junit:junit:3.8.1')
compile('log4j:log4j:1.2.15')
compile('mail:mail:1.0.3')
compile('org.springframework:spring-core:1.2.9')
compile('org.springframework:spring-beans:1.2.9')
compile('org.springframework:spring-mock:1.2.9')
compile('taglibs:standard:1.0.6')
compile('struts:struts:1.2.4')
compile('struts:struts-el:1.2.4')
compile('strutstest:strutstest:2.1.3-1.2-2.3')
compile('velocity:velocity:1.4')
compile('org.apache.ant:ant:1.7.1')
compile('org.apache.tomcat:jsp-api:6.0.18')
compile('org.apache.tomcat:servlet-api:6.0.18')
compile('org.apache.tomcat:catalina:6.0.18')
compile('org.apache.tomcat:dbcp:6.0.18')
compile('org.easymock:easymock:3.0')
compile('cz.svt:easymock-propeq:1.3')
compile('org.testng:testng:5.11:jdk15')
testCompile('org.apache.ftpserver:ftplet-api:1.0.0')
testCompile('org.apache.ftpserver:ftpserver-core:1.0.0')
testCompile('org.apache.mina:mina-core:2.0.0-M4')
testRuntime('org.slf4j:slf4j-api:1.6.1')
testRuntime('org.slf4j:slf4j-log4j12:1.6.1')
testRuntime('org.uncommons:reportng:1.0')
testRuntime('asm:asm:3.0')
testRuntime('asm:asm-tree:3.0')
testRuntime('org.hibernate:hibernate-commons-annotations:3.2.0.Final')
testRuntime('org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final')
testRuntime('javax.transaction:jta:1.1')
testRuntime('antlr:antlr:2.7.6')
testRuntime("org.objenesis:objenesis:1.2")
testRuntime("oro:oro:2.0.7")
}
test {
useTestNG() {
suites 'src/test/resources/testng.xml'
listeners << 'org.uncommons.reportng.HTMLReporter'
listeners << 'org.testng.reporters.XMLReporter'
}
}
task reports {
dependsOn coberturaTestReport, javadoc
}
here is link to aspectj compiler output 1
Related
I have the following folder structure:
-bin
-build/build.gradle (the gradle script)
-lib/[*.jar] (libraries that the project is using)
-src/folder/folder/[*.java] (project's source code)
The following content for the build.gradle script:
plugins {
id 'java'
id 'groovy'
}
buildDir = new File('../bin')
sourceCompatibility = JavaVersion.VERSION_1_8
sourceSets {
main {
java {
allJava.srcDirs = [ '../src/folder/folder/ ']
compileClasspath = fileTree('../bin')
}
}
}
repositories {
flatDir {
dirs '../lib'
}
}
dependencies {
implementation fileTree('../lib')
}
tasks.register('javac', JavaCompile) {
println 'Call javac'
source.forEach { e -> println e}
classpath = sourceSets.main.compileClasspath
destinationDirectory = file('../bin')
source sourceSets.main.allJava.srcDirs
includes.add('*.java')
sourceCompatibility = JavaVersion.VERSION_1_8
}
When running gradle javac I got the error: error: cannot find symbol import com...
The documentations clearly says:
dependencies {
.
.
.
//putting all jars from 'libs' onto compile classpath
implementation fileTree('libs')
}
I'm using Gradle 7.3.1
Allow me to give you some general advice first.
I can strongly recommend using the Kotlin DSL instead of the Groovy DSL.
You instantly get strongly typed code in the build scripts and much better IDE support.
Also you should imho consider changing your project layout to be more like most other Java projects out there and especially not use a libs directory, but use normal dependencies in a repository where transitive dependencies are then handled automatically and so on.
But to answer your actual question, this is the build complete build script in Groovy DSL that you want:
plugins {
id 'java'
}
buildDir = '../bin'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
sourceSets {
main {
java {
srcDirs = ['../src/folder/folder']
}
}
}
dependencies {
implementation fileTree('../lib')
}
And this is the matching Kotlin DSL version:
plugins {
java
}
setBuildDir("../bin")
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
sourceSets {
main {
java {
setSrcDirs(listOf("../src/folder/folder"))
}
}
}
dependencies {
implementation(fileTree("../lib"))
}
I was trying to update to latest kotlin verion 1.4.30 an Apache Beam dataflow pipeline that is currently running with kotlin 1.4.21 but as soon as I upgrade build.gradle with version 1.4.30 the compilation fails with this exception:
java.lang.IllegalStateException: Could not read class: VirtualFile: /Users/stefanomassera/.gradle/caches/modules-2/files-2.1/org.apache.beam/beam-sdks-java-core/2.26.0/b57f8fa5ae66564c7ffafde34b690057f471bfa8/beam-sdks-java-core-2.26.0.jar!/org/apache/beam/sdk/options/PipelineOptions.class
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass.<init>(BinaryJavaClass.kt:120)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass.<init>(BinaryJavaClass.kt:34)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl.findClass(KotlinCliJavaFileManagerImpl.kt:115)
at org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade$CliFinder.findClass(KotlinJavaPsiFacade.java:484)
... omissis for brevity
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Wildcard mast have a bound for annotation of WILDCARD_BOUND position
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaAnnotation$Companion.computeTargetType$resolution_common_jvm(Annotations.kt:188)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.AnnotationsAndParameterCollectorMethodVisitor.visitTypeAnnotation$getTargetType(Annotations.kt:111)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.AnnotationsAndParameterCollectorMethodVisitor.visitTypeAnnotation(Annotations.kt:117)
at org.jetbrains.org.objectweb.asm.ClassReader.readMethod(ClassReader.java:1427)
at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:719)
at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:402)
at org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass.<init>(BinaryJavaClass.kt:115)
... 101 more
I've also tried with a blank dummy project just to verify if it is a compatibility error with current kotlin and apache beam and it seems a compatibility issue, here my dummy test
main.kt
package org.example
import org.apache.beam.sdk.Pipeline
import org.apache.beam.sdk.options.PipelineOptionsFactory
class DummyPipeline {
fun main(args: Array<String>) {
val options = PipelineOptionsFactory.fromArgs(*args)
.withValidation()
.`as`(DummyPipelineOptions::class.java)
val pipeline = Pipeline.create(options)
pipeline.run()
}
}
build.gradle
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.30' //<-- do not build
//id 'org.jetbrains.kotlin.jvm' version '1.4.21' // <-- correctly build
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.apache.beam:beam-sdks-java-io-google-cloud-platform:2.26.0"
runtimeOnly "org.apache.beam:beam-runners-direct-java:2.26.0"
}
Did anyone solved this compatibility issue?
It causes by the Kotlin compiler.
Sorry for nuisance, I'm currently fixing that on the compiler's side. The fix will be available in Kotlin 1.5-M1.
Unfortunately, there are no normal workarounds here, since the problem occurs when reading class files (it's impossible to exclude the problematic logic in the mechanism for reading class files).
I try to run a very simple gradle project which uses java 9 modules, but i receive the following error.
/home/vadim/IdeaProjects/test_modules/src/main/java/module-info.java:2: error: module not found: HdrHistogram
requires HdrHistogram;
^
Here is it https://github.com/vad0/test_modules.
The main class does basically nothing.
package app;
import org.HdrHistogram.Histogram;
public class RunHdr {
public static void main(String[] args) {
final Histogram histogram = new Histogram(5);
System.out.println(histogram);
}
}
It uses only one dependency: HdrHistogram. I included this magic command in build.gradle according to official gradle tutorial https://docs.gradle.org/current/samples/sample_java_modules_multi_project.html.
java {
modularity.inferModulePath = true
}
The whole build.gradle looks like this.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
java {
modularity.inferModulePath = true
}
dependencies {
compile group: 'org.hdrhistogram', name: 'HdrHistogram', version: '2.1.12'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
module.info looks like this
module test.modules.main {
requires HdrHistogram;
}
I have already read a number of tutorials on Jigsaw and a whole bunch of stackoverflow questions related to it, but still can't make this simple example work. How do i fix it?
Thank you
Unfortunately, gradle does not treat every jar as a module (in simple words). If you want to find out how exactly is gradle building the module-path (as opposed to class-path), you probably want to start from here, specifically at the isModuleJar method. It's pretty easy to understand (though it took me almost two days to set-up gradle and debug the problem out) that the dependency that you are trying to use : gradle says that it is not a module (it isn't wrong, but I am not sure it is correct either). To make it very correct, gradle will add your dependency to the CLASSPATH, but in the very next line: it will not add your dependency to the module-path, because if fails the filter in isModuleJar.
I do not know if this is a bug or not, or may be this is on purpose, but the solution is easy:
plugins.withType(JavaPlugin).configureEach {
java {
modularity.inferModulePath = true
}
tasks.withType(JavaCompile) {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files()
}
}
}
you add it to the path, on purpose. I will flag this as a defect and let's see what they have to say.
EDIT
Even better, use a plugin that is written by a gradle commiter:
plugins {
id 'java'
id 'de.jjohannes.extra-java-module-info' version "0.1"
}
And the easiest option on your case is to do :
extraJavaModuleInfo {
automaticModule("HdrHistogram-2.1.12.jar", "HdrHistogram")
}
I've been asked to implement some gRPC classes for a college course, and have run into some problems when generating the java classes from one source proto file.
Some background first: it's a fairly basic service, with a simple method that receives an id and returns a phone and an email. This is the proto file (BuscarData means FetchData, sorry for the leftover non translation!):
syntax = 'proto3';
option java_multiple_files=true;
option java_generic_services= true;
package uy.edu.um.sd20;
message DataRequest {
int32 id = 1;
}
message DataResponse {
string phone = 1;
string email = 2;
}
service DataRepo {
rpc BuscarData (DataRequest) returns (DataResponse);
}
The idea I had was to generate the classes with gradle plugins. My build.gradle:
plugins {
id 'java'
id "com.google.protobuf" version '0.8.8'
}
apply plugin: 'java'
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.11.4'
implementation 'io.grpc:grpc-netty-shaded:1.29.0'
implementation 'io.grpc:grpc-protobuf:1.29.0'
implementation 'io.grpc:grpc-stub:1.29.0'
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.11.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.29.0'
}
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
all().each { task ->
// Here you can configure the task
}
ofSourceSet('main')
}
}
From what I understood, everything's there: the grpc and protoc dependencies, and the plugin which enables protoc to compile grpc (protoc-gen-grpc), and where to deposit the generated files.
However, there are two problems:
the generated-sources are not marked as source or anything like that, meaning they cannot be imported from other classes
if I'm not mistaken, the generated-sources should generate a skeleton of DataRepoImpl so that I can add the code needed for BuscarData. However, it didn't. Or maybe I should create it, extending from DataRepo.java, but I couldn't test it, due to problem n°1.
I've added a screenshot of the project file structure:
img
As you can see, quite a lot (if not all) of the gradle settings are copy-pasted and scavenged from many different web-sites. I hope I was careful enough not to repeat any imports. There are similar questions, and I tried the solutions there, but no luck there. One example, with which I knew I had to include the gen grpc plugin: another SO question
Any tip regarding anything else is welcome! I'm new to stackoverflow question-asking, so I may have made mistakes regarding the question specificity or aim of the question.
Thanks!
Franri.
For 1), the plugin should put the generated files are the input for java compile tasks even if you do not explicitly add 'generated-sources/main/java' in the sourceSets configuration. Version 0.8.8 has been a while, you can try with newer versions. There might have been some minor fixes for things you may hit.
For 2), you did not add grpc plugin to each generateProto task. It should be
generateProtoTasks {
all().each { task ->
task.plugins { grpc{} }
}
ofSourceSet('main')
}
After updating to Gradle 5.2.1 my build is failing with this error:
Gradle DSL method not found: 'destination()'
I figured out that this error has something todo with my analysis.gradle
My analysis.gradle looks like that
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.7.201606060606"
}
check.dependsOn 'checkstyle', 'pmd', 'lint'
task checkstyle(type: Checkstyle) {
println "----- checkstyle -----"
configFile file(projectDir.getAbsolutePath() + '/analysis/checkstyle-ruleset.xml')
source 'src'
source '../domain/src'
source '../util/src'
include '**/*.java'
exclude '**/gen/**'
exclude '**/java-gen/**'
exclude '**/androidTest/**'
exclude '**/test/**'
ignoreFailures = true
classpath = files()
reports {
xml {
destination buildDir.absolutePath + "/outputs/reports/checkstyle_report.xml"
}
}
}
I think I have to replace the destination flag but I have no idea how to replace it.
Before Gradle 5.0 the method setDestination(Object file) was already deprecated, see here : setDestination(Object file)
In Gradle 5.x this method has been removed, you must now use setDestination(File file) which takes a File parameter (see setDestination(File file) )
So you need to change your code into:
reports {
xml {
destination file("$buildDir/outputs/reports/checkstyle_report.xml")
}
}
All adjustment was done in my quality.gradle. Check config folder for quality.gradle file and change all usage of
destination "$reportsDir/pmd/pmd.xml"
to
destination file("$reportsDir/pmd/pmd.html")