When I try to gradlew build my project, I receive an error message from the compileKotlin task:
F:\IdeaProjects\walp.tinykotlintest>gradlew build
:compileKotlin
...
Only the Kotlin standard library is allowed to use the 'kotlin' package
...
BUILD FAILED
Total time: 6.214 secs
This happens because I've declared a class in a package that starts with: kotlin...
Is there a way to dissable this check by configuring my build.gradle script?
EDIT: I know for sure this is possible if I compile with the bare kotlin compiler...here is an example of using the kotlin compiler to compile a class in the kotlin package...So I think there should to be a way to do it in gradle too!....I hope...
What I've tried so far:
I tried to configure the gradle.build by configuring the KotlinCompile task:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
tasks.withType(KotlinCompile) {
it.kotlinOptions.allowKotlinPackage = true
}
The gradle script runs fine with these changes...but it seems like this is getting ignored...and I still encounter the error.
I tried to subclass the KotlinCompile class and override its beforeCompileHook() and replace the existing compileKotlin task with my version:
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
class MyKotlinCompile extends KotlinCompile {
#Override
public void beforeCompileHook(K2JVMCompilerArguments args) {
args.allowKotlinPackage = true
throw new RuntimeException("HELLO") // added to see if hook is run
}
}
tasks.create(name: "compileKotlin", type: MyKotlinCompile, overwrite: true)
Studying the source code...I was fairly certain this would work but evidently it didn't. I tried with and without throwing the RuntimeException but I always get the following error when I tried to gradle build again:
F:\IdeaProjects\walp.tinykotlintest>gradlew build --stacktrace
:compileKotlin UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:sourcesJar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestKotlin'.
> java.lang.NullPointerException (no error message)
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileTestKotlin'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
.
.
.
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Caused by: java.lang.NullPointerException
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile$populateTargetSpecificArgs$2.invoke(Tasks.kt:215)
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.populateTargetSpecificArgs(Tasks.kt:222)
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.populateTargetSpecificArgs(Tasks.kt:148)
at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.execute(Tasks.kt:105)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.doExecute(AnnotationProcessingTaskFactory.java:243)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:219)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.execute(AnnotationProcessingTaskFactory.java:230)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:208)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
... 60 more
I probably shouldn't be doing this anyways...but I really want to try it out.
thank you!
If the Kotlin standard library is incompatible with the JVM that you're using, it sounds like what you should actually do is fork Kotlin, make the changes required for compatibility with your JVM, and then use the standard Maven build script to build your fork. The script already provides the option to allow compiling files in the kotlin package.
The error has nothing to do with Gradle. The Kotlin compiler won't compile code in a kotlin package unless you are developing the Kotlin itself.
This was done for the same reason you can't write code in a java package.
Related
Gradle multi-module project build fails with an unclear error. I run this command:
gradle :module:processor:integrationTest
(module:processor depends on module:processor-core, integrationTest is a custom Gradle task for running tests. I'm using kapt plugin as an annotation processor for Spring Boot configuration properties)
And I get this result:
> Task :module:processor-core:kaptGenerateStubsKotlin UP-TO-DATE
> Task :module:processor-core:kaptKotlin UP-TO-DATE
...
> Task :module:processor-core:jar SKIPPED
> Task :module:processor:kaptGenerateStubsKotlin
> Task :module:processor:kaptKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':module:processor:kaptKotlin'.
> Could not resolve all files for configuration ':module:processor:_classStructurekaptKotlin'.
> Failed to transform processor-core-SNAPSHOT.jar to match attributes {artifactType=class-structure, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
> Execution failed for StructureTransformAction: D:\dev\backend-project\module\processor-core\build\libs\processor-core-SNAPSHOT.jar.
> D:\dev\backend-project\module\processor-core\build\libs\processor-core-SNAPSHOT.jar (The system cannot find the path specified)
I don't understand why.
it was fixed by adding the following code to the build.gradle of processor-core module:
jar {
archiveBaseName = 'processor-core'
}
without these lines, I guess this step is disabled by Spring Boot, so the JAR file was not produced, and this led to the error.
I have a Gradle project set up, which has a buildSrc module inside of it. Inside buildSrc, in build.gradle, I have the following:
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
...
}
When trying to do a build for the project, I receive the following error message:
2:07:13 PM: Executing external task 'build --stacktrace'...
:buildSrc:compileJava NO-SOURCE
:buildSrc:compileGroovy FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':buildSrc:compileGroovy'.
> java.lang.ExceptionInInitializerError (no error message)
In the stacktrace, I see the following error:
Caused by: groovy.lang.GroovyRuntimeException: Conflicting module versions. Module [groovy-all is loaded in version 2.4.11 and you are trying to load version 2.4.12
... 15 more
So, when I look at the project structure, I see that the groovy-all-2.4.11.jar is automatically being loaded into the buildSrc module.
If I remove the compile dependency for Groovy in build.gradle, it will work, but is there any way to force the module to use the version of Groovy that I want?
Gradle always applies the default build script to buildSrc project. This script contains following line:
compile localGroovy()
that is how groovy-all-2.4.11 gets in. To override this behaviour, try setting the resolutionStrategy, e.g.:
configurations.compile {
resolutionStrategy {
force 'org.codehaus.groovy:groovy-all:2.4.12'
}
}
but more importantly think, why would you want to build against a groovy version that differs from one available in your plugin's runtime...
I'm trying to upgrade some Gradle projects from Kotlin 1.0.6 to its latest version (1.1.0). However, whenever it reaches the compileKotlin task it fails:
:kiwi-common-kotlin:compileKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':kiwi-common-kotlin:compileKotlin'.
> Could not find Kotlin Compiler jar. Please specify compileKotlin.compilerJarFile
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
The project itself is divided in a bunch of subprojects. To avoid dupes, we have a separate file with the Kotlin definitions and import it on the projects using it:
File: gradle/kotlin.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
apply plugin: org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
compileKotlin {
kotlinOptions.jvmTarget = "1.6"
}
And we import it in the subprojects like this:
apply from: "$rootDir/gradle/kotlin.gradle"
Since we previously tried the early preview releases and it worked seamlessly, I changed the version a couple times to see in which version the plugin broke. It turns out it works up to 1.1-M03; beginning at 1.1-M04 it shows the exact same error as in the 1.1.0 release.
We're using Gradle 2.10 in the project. Can you help me figure out whether it is a problem in our configuration? or a known issue with the Kotlin plugin itself?
It turned out to be a bug with the Gradle plugin. As pointed out by hotkeys' comment, updating to v1.1.2 fixes it.
I have a gradle (1.8) project with 2 classes A and B where B import A. Both files are under /foo/bar/ package (myProjectFolder/test/foo/bar folder).
A.groovy class
package foo.bar
import org.junit.Test;
public class ATest{
#Test
public void run() {
System.out.println("CLASS A");
}
}
B.groovy class
package foo.bar
import org.junit.Test;
public class BTest{
#Test
public void run() {
ATest a = new ATest();
a.run();
System.out.println("CLASS B");
}
}
build.gradle
apply plugin: "groovy"
apply plugin: "java"
repositories {
mavenCentral()
}
dependencies {
groovy group: "org.codehaus.groovy", name:"groovy-all", version: "1.8.6"
testCompile "junit:junit:4.10"
}
test {
testLogging.showStandardStreams = true
}
sourceSets {
test { groovy {
srcDir 'test/foo/bar'
} }
}
buildscript {
repositories { mavenCentral() }
}
configurations{
addToClassLoader
}
dependencies {
addToClassLoader "junit:junit:4.10"
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.addToClassLoader.each {File file ->
loader.addURL(file.toURL())
}
task runA << {
new GroovyShell().run(file('test/foo/bar/ATest.groovy'));
}
task runB << {
new GroovyShell().run(file('test/foo/bar/BTest.groovy'));
}
Output console for gradle clean -Dtest.single=A test
[root#vm1]# gradle clean -Dtest.single=A test
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
:clean
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources
:classes
:compileTestJava UP-TO-DATE
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
foo.bar.ATest > run STANDARD_OUT
CLASS A
BUILD SUCCESSFUL
Total time: 13.205 secs
Output console for gradle clean -Dtest.single=B test
[root#vm1]# gradle clean -Dtest.single=B test
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
:clean
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources
:classes
:compileTestJava UP-TO-DATE
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
foo.bar.BTest > run STANDARD_OUT
CLASS A
CLASS B
BUILD SUCCESSFUL
Total time: 12.218 secs
Output console for gradle -q runA -i
[root#vm1]# gradle -q runA -i
Starting Build
Settings evaluated using empty settings script.
Projects loaded. Root project using build file '/opt/myProject/build.gradle'.
Included projects: [root project 'myProject']
Evaluating root project 'myProject' using build file '/opt/myProject/build.gradle'.
Starting file lock listener thread.
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
All projects evaluated.
Selected primary task 'runA'
Tasks to be executed: [task ':runA']
:runA (Thread[main,5,main]) started.
:runA
Executing task ':runA' (up-to-date check took 0.001 secs) due to:
Task has not declared any outputs.
CLASS A
JUnit 4 Runner, Tests: 1, Failures: 0, Time: 63
:runA (Thread[main,5,main]) completed. Took 0.739 secs.
BUILD SUCCESSFUL
Total time: 7.826 secs
Output console for gradle -q runB -i <-- THIS EXECUTION FAILS and I don't know why.
[root#vm1]# gradle -q runB -i
Starting Build
Settings evaluated using empty settings script.
Projects loaded. Root project using build file '/opt/myProject/build.gradle'.
Included projects: [root project 'myProject']
Evaluating root project 'myProject' using build file '/opt/myProject/build.gradle'.
Starting file lock listener thread.
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
All projects evaluated.
Selected primary task 'runB'
Tasks to be executed: [task ':runB']
:runB (Thread[main,5,main]) started.
:runB
Executing task ':runB' (up-to-date check took 0.001 secs) due to:
Task has not declared any outputs.
:runB FAILED
:runB (Thread[main,5,main]) completed. Took 0.237 secs.
FAILURE: Build failed with an exception.
* Where:
Build file '/opt/myProject/build.gradle' line: 46
* What went wrong:
Execution failed for task ':runB'.
> startup failed:
/opt/myProject/test/foo/bar/BTest.groovy: 8: unable to resolve class ATest
# line 8, column 9.
ATest a = new ATest();
^
/opt/myProject/test/foo/bar/BTest.groovy: 8: unable to resolve class ATest
# line 8, column 13.
ATest a = new ATest();
^
2 errors
* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.
BUILD FAILED
Total time: 7.872 secs
So, what am I missing to run the task runB correctly?
Note: the premise of the question is based on a truly unorthodox use-case, which should be corrected. That said, I can explain the behaviour that is observed.
When the command-line specifies the test task, then ~/build/classes/test is added to the classpath. In the case of running the runB task, it is not. So BTest.groovy can't find ATest.
To correct it (only as an illustration, not as a suggestion for 'real' code), consider:
// don't do this in a real project!
task runB(dependsOn: 'compileTestGroovy') << {
def testDirURL = new File("${projectDir}/build/classes/test").toURL()
loader.addURL(testDirURL)
new GroovyShell(loader).run(file('test/foo/bar/BTest.groovy'))
}
Now, runB requires that the Groovy test code is compiled, and then manually (!?) adds it to the classloader used by GroovyShell.
Using gradle with its spring-boot plugin (1.5.1) and a first spirit plugin together raises an error:
D:\Coden\WS\STS\fs-db-import>gradle build
:genJaxb
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:fsm UP-TO-DATE
:bootRepackage FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bootRepackage'.
> Unable to deduce layout for 'D:\Coden\WS\STS\fs-db-import\build\fsm\fs-db-import-0.0.1.fsm'
It seems that bootRepackage from the spring boot plugin can't handle or package the fsm file.
a) how can I exclude it?
Another approach would be to switch the 'fsm' and the 'bootRepackage' task, but adding the following lines to build.gradle
bootRepackage.dependsOn -= fsm
tasks.bootRepackage.dependsOn -= fsm
fsm.dependsOn bootRepackage
results in
Circular dependency between the following tasks:
:bootRepackage
\--- :fsm
\--- :bootRepackage (*)
(*) - details omitted (listed previously)
b) how do I remove 'fsm'`s dependency from 'bootRepackage'?
c) is it possible to show a gradle task dependency graph? (not project dependencies)
By default the Spring Boot plugin attempts to repackage all tasks of type Jar. Sine the FSM task extends from Jar the plugin attempts to repackage them both. You can explicitly tell the plugin which Jar task to use.
bootRepackage {
withJarTask jar
}
More information can be found in the Spring Documentation.