I have created a new Gradle project in Netbeans where I want to use java and groovy classes together. To make it work I have configured my build.gradle with the following code:
apply plugin: 'groovy'
sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDir 'src/main/java'
The build process works like a charm: groovy and java classes interoperate transparently. The only annoying thing is the Netbeans code completion. Code completion of java classes does not work from groovy. Let's say I have the following classes:
*my.package1.JavaClass.java
my.package2.GroovyClass.groovy*
From GroovyClass, if I type 'JavaC' and hit CTRL+space the IDE does not present me any help to import and use JavaClass. I have to manually create the import statement. Moreover when I type '.' after the JavaClass instance Netbeans does not show the methods I could call. This happens also for classes included as dependencies.
Related
I have two projects. One written fully in Kotlin that exports a Client artifact. I am trying to use the client in a second project that has a mix of java/kotlin code.
In the java classes I have no problem importing the kotlin files for use, but in any kotlin files, IntelliJ cannot resolve the imports. All the other kotlin code works fine and I can compile on the command line via maven with no problems. It's just an issue with IntelliJ not recognizing the package I'm looking for.
I see the package in both my maven toolbar as well as in the external libraries listed in the project. I've inspected the jars and sure enough the file I expect com/foo/bar/BazClient.class is present, but
import com.foo.bar.BazClient tells me the package bar (the code from the other project) does not exist. The very same import statement works just fine in java code.
Further adding to my problems the exact same java class that imports this client, if I convert to kotlin using IJ's builtin method, fails to compile in IntelliJ. The "build project" action completes successfully with no warnings/errors.
Kotlin client is defined as:
package com.foo.bar
import retrofit2.http.GET
interface BazClient {
#GET("/v1/fuzz")
fun getFuzz(): Call<FuzzResponse>
}
Working java code:
package com.whodat.wat;
import javax.inject.Singleton;
import com.foo.bar.BazClient;
#Singleton
public class CallTheService {
private final BazClient bazClient;
public CallTheService(BazClient bazClient) {
this.bazClient = bazClient;
}
public FuzzResponse callIt() throws IOException {
return bazClient.getFuzz().execute().body();
}
}
Failing kotlin code:
package com.whodat.wat
import javax.inject.Singleton
import com.foo.bar.BazClient // "bar" is red in editor
#Singleton
// Can't resolve "BazClient" here
class CallTheService(private val bazClient: BazClient) {
fun callIt(): FuzzResponse {
return bazClient.getFuzz().execute().body()!!
}
}
It turns out this was a problem in publishing the client artifact. We were using the maven shade and jar plugins to create the jars and was leading to many kotlin_modules in the jar which were confusing IJ
META-INF/client.kotlin_module
META-INF/descriptors.jvm.kotlin_module
META-INF/descriptors.kotlin_module
META-INF/descriptors.runtime.kotlin_module
META-INF/deserialization.kotlin_module
META-INF/kotlin-reflect-api.kotlin_module
META-INF/metadata.jvm.kotlin_module
META-INF/metadata.kotlin_module
META-INF/util.runtime.kotlin_module
META-INF/kotlin-stdlib.kotlin_module
META-INF/kotlin-stdlib_coroutinesExperimental.kotlin_module
META-INF/kotlin-stdlib-common.kotlin_module
META-INF/kotlin-stdlib-common-coroutines.kotlin_module
META-INF/kotlin-stdlib-jdk8.kotlin_module
META-INF/kotlin-stdlib-jdk7.kotlin_module
Removing those from the client build seems to have cleared things up, and now we just have the one client.kotlin_module.
This problem has a ticket in Jetbrains issue tracker. The ticket has not been resolved yet, but support said:
This is a known problem since the 1.2.50 release of the Kotlin plugin:
KT-25709. As a workaround, please stop bundling the Kotlin
runtime in the dependency jar (i.e. everything under the package
kotlin should be removed from the jar)
I've been using Eclipse for a while and I'm having trouble understanding what's going on with my first project in IntelliJ. I've read the documentation, and searched other questions, but I still can't seem to grasp it. I think there is something wrong with my project structure. This is what my structure currently looks like;
I'm trying to run the JavaForLoop class, but whenever I do, compilation fails because I have errors in the StringMethods class of the strings package. My question is why would that prevent compilation if the two classes are in separate packages? Neither class uses the other, and they both have the appropriate package declaration statements. With a similar structure in Eclipse, this would work. Should I be using a different project structure?
By default IDEA adds Build Configuration which is executed before launch and includes following steps (taken from here):
Compiling source code in the source path of a module and placing results to the output path.
Compiling source code in the test path of a module and placing results to the test output path.
Creating copies of the resource files in the output path.
Reporting problems in the Messages tool window.
check if it's your case in Edit Configuration screen and if so, remove it.
To use a class from a different package you must declare a import statement to the class.
In your JavaForLoop.java add the import before the class statement (and after package declaration where its the case)
//package ...
import strings.StringMethods;
//public class JavaForLoop { and the rest of the code
Intellij uses regular javac, which will fail to compile if you have errors anywhere in the code.
Eclipse has it's own compiler, that allows to compile and even run code that has compilation errors, causing a runtime exception if any part of the code that has errors is run. This allows you to run parts of the code that work even if other pieces of code are failing.
The simple solution is to resolve your compilation errors. You can also use the eclipse compiler with Intellij, but I've never done this so I can't comment on how well it works.
Running into problems extracting tasks from a build.gradle file to then be applied, back into the app/root build.gradle file. The compiler can resolve MarkupBuilder and JsonSlurper fine but cannot resolve the following: import org.apache.commons.lang.StringEscapeUtils.
I've tried adding it as a dependency within the newly created script and also within the app and project levels.
'org.apache.commons.lang:commons-lang:3.5'
The error is below
Could not compile script '/project/app/newscript.gradle'.
startup failed:
> script '/project/app/newscript.gradle': 18: unable to resolve class org.apache.commons.lang.StringEscapeUtils
# line 18, column 1.
import org.apache.commons.lang.StringEscapeUtils
^
1 error
Am I doing something wrong or is this not possible? Would I need to include the script in a different way than apply script: newscript.gradle or another plugin within the newscript.gradle?
A Gradle script is basically a Groovy file. Which in turn gets compiled into JVM bytecode, similar to Java classes. So when compiling a script with an import, the imported classes must be on the classpath. Some classes like the MarkupBuilder are available by default (included either by Groovy or Gradle).
You have to add something like this to be able to use the classes in your script:
import org.apache.commons.lang.StringEscapeUtils;
buildscript {
repositories {
mavenCentral();
}
dependencies {
classpath 'org.apache.commons.lang:commons-lang:3.5'
}
}
The buildscript closure will add the library on the classpath of the Gradle script and you should be able to use its classes.
The Groovy plugin for Gradle claims that it "supports joint compilation, which allows to freely mix and match Groovy and Java code, with dependencies in both directions".
However, I don't think this applies to test code.
I have a Java 'sample' test in src/test/java... which uses a class which is located in src/test/groovy.
When trying to build with Gradle, I get an error like this:
SwingJavaFXSampleAppTestInJava.java:23: error: cannot find symbol
SwingJavaFXSampleAppTest swingJavaFx = new SwingJavaFXSampleAppTest();
Notice that SwingJavaFXSampleAppTest is a Groovy class that has not been compiled yet (in the Gradle output I can see that it did not run the compileTestGroovy before it tried compileTestJava because the former depends on the latter).
I am able to build this same project with Maven using the groovy-eclipse plugin.
Why does it not work in Gradle when it claims to support compilation in any order, and how can I make it work?
As explained in the Gradle User Guide, only code passed to GroovyCompile tasks is joint-compiled. So either you put both Java and Groovy code into src/main/groovy, or you reconfigure the source sets:
sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDirs = ["src/main/java", "src/main/groovy"]
For tests, replace all occurrences of main with test.
You should be able to move your java tests into src/test/groovy.
i created a sample polyglot program. i have a sensor and a robot implemented in java and AI implemented in clojure. and i can't connect maven properly
--src/main/java/clojuretest
|
DistanceSensor.java
AI.clj (uses DistanceSensor)
Robot.java (uses AI)
DistanceSensor.java:
package clojuretest;
public class DistanceSensor {
public int getValue() {return 5;}
}
AI.clj:
(ns clojuretest.AI
(:gen-class :methods [[isObstacleAhead [] boolean]]))
(defn -isObstacleAhead [this] (< (.getValue (clojuretest.DistanceSensor.)) 10))
Robot.java:
package clojuretest;
public class Robot {
public boolean shouldStop() {
return new AI().isObstacleAhead();
}
}
i can even manually force maven to compile it:
mvn clean clojure:compile produces error - no DistanceSensor class (but for some reason creates AI.class). so then
mvn compile sees AI.class and compiles everything correctly and tests pass. but what can i do to make mvn clean compile pass? how should my pom.xml look like? also what can i do to make eclipse stop complaining about non existing AI.class?
You need to change layout of source code in your project. Clojure maven plugin requires, that clojure code went to separate directory, so you should have following layout:
src/
main/
java/
java-code
clojure/
clojure code
test/
java/
java tests code
clojure/
clojure tests code
More details you can find in following article
I think :gen-class is usually a code smell, as is trying to instantiate such a class from Java code with new AI().
Here's an alternative approach that can solve this problem of cyclic dependencies:
Define AI as a Java interface in your Java code
Write a Clojure function to create an instance conforming to the interface using reify
Dynamically invoke the Clojure function from Java (e.g. using the technique outlined in this blog post)
You now have an instance of the AI interface that you can use however you like in Java
The advantage is this approach is that everything will work smoothly, in particular:
The Java code base can be compiled independently of the Clojure code
The Clojure code base can access all the defined Java classes and interfaces
You don't need any special IDE / Maven config. In fact, you can treat is as just a regular Java app that happens to include clojure.jar as a dependency.
You have an inter-dependency between Java code and Clojure code. No matter which type of classes you'll compile first, you'll get an error.
Since it's not an actual cyclic dependency, you can still fix this by splitting the Java compilation part in two.
First, compile DistanceSensor, which doesn't depend on anything else.
Second, compile AI, which depends on DistanceSensor.
Finally, compile Robot which depends on AI.
To split the java compilation in two steps, you need to configure the default execution of the maven-compiler-plugin so that it excludes Robot, and add another execution after the clujure:compile goal that excludes DistanceSensor. You'll probably have to misuse the phases to properly order the three executions.