quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.86k stars 2.7k forks source link

Enable JDK preview features from Gradle-based build #10472

Open 38leinaD opened 4 years ago

38leinaD commented 4 years ago

Describe the bug It seems to be not possible to enable JDK preview features for the Gradle quarkusBuild task.

If i set

compileJava {
    options.compilerArgs += ["-parameters", "--enable-preview"]
}

it seems like compilation is triggered with --enable-preview, but whatever the quarkusBuild task does afterwards fails with this

Execution failed for task ':imgbooth:quarkusBuild'.
> Preview features are not enabled for de/dplatz/imgbooth/config/boundary/MutableConfigSource (class file version 58.65535). Try running with '--enable-preview'

Environment (please complete the following information):

quarkusbot commented 4 years ago

/cc @quarkusio/devtools

38leinaD commented 4 years ago

sorry for wrong classification; not really a bug but feature-request.

gsmet commented 4 years ago

/cc @glefloch

geoand commented 4 years ago

Just copying what I mentioned in chat that we already have this feature in Maven

glefloch commented 4 years ago

Thanks for reporting this. It's possible to set custom compileArgs for the quarkusDev task but not for the quarkudBuild. I will have a look on how this is handled in maven and push a commit to make it possible in Gradle

geoand commented 4 years ago

@glefloch IIRC, in Maven we check and see if the compiler flag is enabled, and if so we just add it to the JVM flags we launch the dev jar with

38leinaD commented 4 years ago

@glefloch actually, i am wondering if something is broken for quarkusDev as well. It worked in the past but complains now when i want to set --enable-preview.

I am quiet sure in the past i was able to set

quarkusDev {
    jvmArgs '--enable-preview'
}

jvmArgs seems to be a List by now; so i tried

quarkusDev {
    jvmArgs ['--enable-preview']
}

But i get

> Cannot execute null+[--enable-preview] Any idea if this should be working or is this something to put into the same issue?

glefloch commented 4 years ago

@38leinaD I just ran some tests on my machine. Regarding the quarkusDev task, you can either set the --enable-preview using the compilerArgs property, such as:

quarkusDev {
    compilerArgs = ["--enable-preview"]
}

or, if you don't set compilerArgs in quarkusDev task, then compileJava compiler args will be used by the task.

Regarding the quarkusBuild, I created a small project running in jdk13 with --enable-preview and I used text blocks. Everything is ok. I only configured the compileJava task as:

compileJava {
    options.encoding = 'UTF-8'
    options.compilerArgs +=[ "-parameters", "--enable-preview"]
}

Which feature are you trying? Could you create a small reproducer?

38leinaD commented 4 years ago

Thanks for all the explaination; it is really easy now to configure the preview features!

Indeed i was able to create a reproducer. I am also able to use text-blocks with the config you describe, but it starts failing when i add my own microprofile-config source as an example:

https://github.com/38leinaD/quarkus-config-source-preview-features-reproducer

> Task :compileJava
Note: /home/daniel/junk/code-with-quarkus/src/main/java/org/acme/ExampleResource.java uses preview language features.
Note: Recompile with -Xlint:preview for details.

> Task :quarkusBuild FAILED
building quarkus runner

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':quarkusBuild'.
> Preview features are not enabled for org/acme/MutableConfigSource (class file version 58.65535). Try running with '--enable-preview'

* 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 2s
5 actionable tasks: 5 executed
glefloch commented 4 years ago

@38leinaD your welcome, I will update the documentation to make this clearer.

Regarding your reproducer, I have the same error.

@geoand, @gsmet, MutableConfigSource implements org.eclipse.microprofile.config.spi.ConfigSource which, I think has not been compiled with jdk 14 could it be the problem ?

geoand commented 4 years ago

Hi,

I just tried the reproducer and it worked fine for me. So perhaps MutableConfigSource was compiled in a previous gradle run and the gradle cache just needs to be cleared?

38leinaD commented 4 years ago

hm, so @glefloch can reproduce the issue and @geoand cannot. very strange. I am quiet sure that i dont have any classes compiled from a previous run/compile; at least not in the places where i would expect these class-files to be. What i did:

rm -rf bin
rm -rf build
rm -rf .gradle
./gradlew clean quarkusBuild

Getting the issue/error every time.

I now even cloned my repdroducer to a different location and did a quarkusBuild. Same issue. I tested with java 13 and java 14. Getting essentially the same error. For java 13, the error is just saying that "(class file version 57.65535)".

geoand commented 4 years ago

Nevermind, I was able to reproduce the error

geoand commented 4 years ago

@geoand, @gsmet, MutableConfigSource implements org.eclipse.microprofile.config.spi.ConfigSource which, I think has not been compiled with jdk 14 could it be the problem ?

It shouldn't be a problem. Furthermore, I changed MutableConfigSource to not implement org.eclipse.microprofile.config.spi.ConfigSource.ConfigSource and Gradle is still complaining. I don't know what to make of it, other than perhaps a Gradle bug?

Or maybe there is something weird with the interaction of --enable-preview and Java's ServiceLoader (which seems likely as compilation works if you simply remove the service file)

38leinaD commented 4 years ago

Maybe i am totally wrong, but for me it looks like this (just idea; not verified in any way):

Also, I have not tested it, but I would assume that the problem is the same for Maven...

38leinaD commented 4 years ago

I have confirmed it by adding org.gradle.jvmargs=--enable-preview to ~/.gradle/gradle.properties. This sets the jvm args for the gradle daemon itself. And now the build passes. I assume the augmentation would needs to run in its own JVM processes for this to work. Same for Maven i assume.

dspangen commented 1 year ago

Problem is that the quarkus build relies on a Gradle worker so you need to set jvmArgs for the for subprocess(es) that will do the work:

quarkus {
    buildForkOptions {
        jvmArgs += ['--enable-preview']
    }
}

This doesn't appear to be inherited from compilerArgs even in Quarkus 3.0.3 (and setting org.gradle.jvmargs doesn't affect these processes either, as previously suggested here).

kpagratis commented 1 year ago

Hello there, I'm running into this problem but when running quarkusRun quarkusBuild and quarkusDev both work and I'm able to use my app, but quarkusRun gives an error like

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at io.quarkus.bootstrap.runner.QuarkusEntryPoint.doRun(QuarkusEntryPoint.java:61)
        at io.quarkus.bootstrap.runner.QuarkusEntryPoint.main(QuarkusEntryPoint.java:32)
Caused by: java.lang.ExceptionInInitializerError
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:70)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:44)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:124)
        at io.quarkus.runner.GeneratedMain.main(Unknown Source)
        ... 6 more
Caused by: java.lang.RuntimeException: Failed to start quarkus
        at io.quarkus.runner.ApplicationImpl.<clinit>(Unknown Source)
        ... 15 more
Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for <MY CLASS> (class file version 61.65535). Try running with '--enable-preview'
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
        at io.quarkus.bootstrap.runner.RunnerClassLoader.loadClass(RunnerClassLoader.java:105)
        at io.quarkus.bootstrap.runner.RunnerClassLoader.loadClass(RunnerClassLoader.java:65)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:467)
        at <MY CLASS>_Bean.<init>(Unknown Source)
        at io.quarkus.arc.setup.Default_ComponentsProvider.addBeans2(Unknown Source)
        at io.quarkus.arc.setup.Default_ComponentsProvider.getComponents(Unknown Source)
        at io.quarkus.arc.impl.ArcContainerImpl.<init>(ArcContainerImpl.java:125)
        at io.quarkus.arc.Arc.initialize(Arc.java:39)
        at io.quarkus.arc.runtime.ArcRecorder.initContainer(ArcRecorder.java:47)
        at io.quarkus.deployment.steps.ArcProcessor$generateResources844392269.deploy_0(Unknown Source)
        at io.quarkus.deployment.steps.ArcProcessor$generateResources844392269.deploy(Unknown Source)
aloubyansky commented 1 year ago

@kpagratis could you help me with a reproducer? The one referenced in this issue appears to work with quarkusRun.

kpagratis commented 1 year ago

@aloubyansky sorry for the delay, but here's a simple reproducer code-with-quarkus.zip

quarkusDev works fine, but when running quarkusBuild followed by quarkusRun the error above is shown. Thanks!

aloubyansky commented 1 year ago

The following PR adds jvmArgs option to quarkusRun https://github.com/quarkusio/quarkus/pull/36793

t1 commented 1 month ago

I'm using Maven, so I'm not sure, if I have the same issue discussed here, or if it's a new one.

I'm playing around with JDK 23 preview features, and I got preview features working in unit-/quarkus-tests and dev-mode (even after a hot reload). But when running the integration tests, if fails after ~1min, throwing:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at io.quarkus.bootstrap.runner.QuarkusEntryPoint.doRun(QuarkusEntryPoint.java:62)
    at io.quarkus.bootstrap.runner.QuarkusEntryPoint.main(QuarkusEntryPoint.java:33)
Caused by: java.lang.ExceptionInInitializerError
    at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
    at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1161)
    at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.ensureClassInitialized(MethodHandleAccessorFactory.java:340)
    at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.newConstructorAccessor(MethodHandleAccessorFactory.java:103)
    at java.base/jdk.internal.reflect.ReflectionFactory.newConstructorAccessor(ReflectionFactory.java:173)
    at java.base/java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:548)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:498)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:485)
    at io.quarkus.runtime.Quarkus.run(Quarkus.java:70)
    at io.quarkus.runtime.Quarkus.run(Quarkus.java:44)
    at io.quarkus.runtime.Quarkus.run(Quarkus.java:124)
    at io.quarkus.runner.GeneratedMain.main(Unknown Source)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    ... 3 more
Caused by: java.lang.RuntimeException: Failed to start quarkus
    at io.quarkus.runner.ApplicationImpl.<clinit>(Unknown Source)
    ... 16 more
Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for com/github/t1/Hellos (class file version 67.65535). Try running with '--enable-preview'
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1026)
    at io.quarkus.bootstrap.runner.RunnerClassLoader.defineClass(RunnerClassLoader.java:152)
    at io.quarkus.bootstrap.runner.RunnerClassLoader.loadClass(RunnerClassLoader.java:111)
    at io.quarkus.bootstrap.runner.RunnerClassLoader.loadClass(RunnerClassLoader.java:72)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:578)
    at java.base/java.lang.Class.forName(Class.java:557)
    at com.github.t1.Hellos_Bean.<init>(Unknown Source)
    at io.quarkus.arc.setup.Default_ComponentsProvider.addBeans1(Unknown Source)
    at io.quarkus.arc.setup.Default_ComponentsProvider.getComponents(Unknown Source)
    at io.quarkus.arc.impl.ArcContainerImpl.<init>(ArcContainerImpl.java:128)
    at io.quarkus.arc.Arc.initialize(Arc.java:38)
    at io.quarkus.arc.runtime.ArcRecorder.initContainer(ArcRecorder.java:49)
    at io.quarkus.deployment.steps.ArcProcessor$initializeContainer1770303700.deploy_0(Unknown Source)
    at io.quarkus.deployment.steps.ArcProcessor$initializeContainer1770303700.deploy(Unknown Source)
    ... 17 more

I have a reproducer here.