Integrating groovy in java in gradle - java

Im trying to integrate groovy in java with build tools as gradle,
There are several examples with maven build, but with gradle its still missing.
I tried of adding plugin of groovy, but for compiler plugin like gmavenplus.
I try to add it as dependency but not sure how i can add it in gradle

You can mix Java classes into a Groovy project.
Create a Groovy project using gradle init.
Your Groovy source will be in a source/main/groovy folder.
Add a source/main/java folder (but see the comment, below, from Jeff Scott Brown)
Add your java files in the java folder, in the usual way.
Do the same for your src/test folder
Example src folder structure
src
└───main
├───resources
├───groovy
│ └───mixedbuildtest
│ App.groovy
│
├───java
│ └───mixedbuildtest
│ JavaApp.java
Example source code
Groovy Source Code
package mixedbuildtest
class App {
String getGreeting() {
return 'Hello from Groovy!'
}
static void main(String[] args) {
println new App().greeting
println new JavaApp().greeting
}
}
Java Source Code
package mixedbuildtest;
class JavaApp {
String getGreeting() {
return "Hello from Java!";
}
}
Example output
F:\projects\sx\MixedBuild>gradle run
> Task :app:run
Hello from Groovy!
Hello from Java!
BUILD SUCCESSFUL in 4s
3 actionable tasks: 3 executed
F:\projects\sx\MixedBuild>

Related

Transitive dependencies with Gradle & OpenJFX

I am in the process of setting up JavaFX-based project with Gradle 7.3.
The project has the following structure:
project/
├─ boot/
├─ gui/
├─ other_module1/
├─ other_moduleN/
All JavaFX related code belongs into the GUI module, while boot defines the entry point.
The gui/build.gradle.kts for the module GUI uses the OpenJFX plugin:
plugins {
`java-library`
id("org.openjfx.javafxplugin") version "0.0.10"
}
// ...
javafx {
version = "17"
modules("javafx.controls", "javafx.fxml")
}
whereas the boot package only imports GUI:
plugins {
application
}
dependencies {
implementation(project(":gui"))
implementation(project(":network"))
}
application {
mainClass.set("project.boot.Boot")
}
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "project.boot.Boot"
}
}
Using this setup, my project wont compile and crashes while working on boot:
error: cannot access Application
Game.execute(args);
^
class file for javafx.application.Application not found
This can be fixed by also applying the OpenJFX Plugin - the same way as seen above - to boot (boot/build.gradle.kts).
QUESTION:
How can I apply JavaFX transitively, so that it remains an implementation detail within the GUI module?
As I am new to Gradle and Java, I am probably missing something small yet crucial.
Also, I tried to read the docs, but the OpenJFX Plugin docs are quite outdated and often do not work as they are.

How to specify the Protobuf path using protobuf-gradle-plugin

I'm trying to generate Protobufs in a Java project that are defined in another Git repository that I'd like to add as a Git submodule. My build.gradle contains
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.0.0-rc-2"
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
and I've included the protobufs repository (called my-protobufs) in the src/main/proto directory. The Protobufs are in turn located in a proto subdirectory of my-protobufs. A partial directory structure looks like this:
src/main/proto/edm-grpc-protobufs/proto
├── mypackage
│   └── v1
│   ├── bar.proto
│   └── foo.proto
The foo.proto file has an import statement like this:
import "mypackage/v1/bar.proto";
That is because in that repository, the Protobuf path is the proto directory. The problem is that when I try to ./gradlew build, I get an error like the following:
> Task :generateProto FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':generateProto'.
> protoc: stdout: . stderr: mypackage/v1/bar.proto: File not found.
my-protobufs/proto/mypackage/v1/foo.proto:5:1: Import "axmorg/v1/bar.proto" was not found or had errors.
my-protobufs/proto/mypackage/v1/foo.proto:10:5: "SourceType" is not defined.
The problem is basically that the --proto_path (in the parlance of protoc) or the directory in which to search for imports is not correctly defined, so protobuf-gradle-plugin doesn't know where to find them. Is is possible to update the build.gradle to specify this path?
I found this in die documentation:
https://github.com/google/protobuf-gradle-plugin#customizing-source-directories
sourceSets {
main {
proto {
// In addition to the default 'src/main/proto'
srcDir 'src/main/protobuf'
srcDir 'src/main/protocolbuffers'
// In addition to the default '**/*.proto' (use with caution).
// Using an extension other than 'proto' is NOT recommended,
// because when proto files are published along with class files, we can
// only tell the type of a file from its extension.
include '**/*.protodevel'
}
java {
...
}
}
test {
proto {
// In addition to the default 'src/test/proto'
srcDir 'src/test/protocolbuffers'
}
}
}
I ended up working around this problem: the packages containing relative Protobuf imports actually weren't needed for the Java project whereas the ones that were needed did not contain relative imports, so I modified the sourceSets in build.gradle to be like
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
proto {
exclude '**/*.proto'
include 'my-protobufs/proto/mypackage/**/*.proto'
}
}
}
which circumvented the issue with the Protobuf path as there are no longer any imports. I'm still curious how I would specify the Protobuf path, though.

How to get gradle to download libraries

I am learning Java "the hard way", meaning without any IDE. Instead, I rely on gradle and my text editor. A this moment, my project looks like this:
├── build.gradle
└── src
├── main
│   └── java
│   └── CliOptionsTryout.java
Contents of build.gradle:
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile 'commons-cli:commons-cli:1.4'
compile 'org.slf4j:slf4j-api:1.7.22'
testCompile 'junit:junit:4.12'
}
Contents of CliOptionsTryout.java:
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class CliOptionsTryout {
public static void main(String[] args) {
Options options = new Options(); // <=== FAILED HERE
options.addOption("h", "help", false, "show help.");
options.addOption("v", "var", true, "Here you can set parameter .");
}
}
The project built successfully with gradle build, but when I executed java CliOptionsTryout -v 100, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/Options
at CliOptionsTryout.main(CliOptionsTryout.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.Options
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I know that the reason is it could not find the commons-cli library. So, the question is, how do I tell gradle to download and install this library?
As you can read the in the stack trace, the Java executable cannot find the class org.apache.commons.cli.Options. This class is part of a dependency you are using.
When you are executing java CliOptionsTryout, the Java executable looks for binaries (.class files in the current folder). You get an error because Gradle does not fetch dependencies (JAR files) in the folder where your CliOptionsTryout.class file is.
If you want to run your class with success, you need to tell the Java executable where to find the JAR file that contains the .class files of the library you are using.
By default, Gradle fetches all JAR dependencies in $HOME/.gradle/.... Your missing dependency is Apache commons CLI in version 1.4. Below is the command to locate the exact path:
find $HOME/.gradle -name "commons-cli-1.4.jar"
For instance, I get the following:
$HOME/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.4/c51c00206bb913cd8612b24abd9fa98ae89719b1/commons-cli-1.4.jar
Once you have the path to your dependency JAR file, you can use the java command with the -cp option for extending the classpath. The classpath is used to tell where to find third-party binaries:
java -cp $HOME/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.4/c51c00206bb913cd8612b24abd9fa98ae89719b1/commons-cli-1.4.jar:. CliOptionsTryout
In the real world, I would recommend generating a JAR file (including all dependencies) for your app and executing directly this file with java -jar. If you are interested in learning how to do, please have a look at the Gradle application plugin.

java scala cross compliation same module error java: cannot find symbol

I get this error when trying to reference a class under the /scala src directory from a class that exists in /java src directory.
(If the scala file is in the /java src file it doesn't compile either).
I am using Gradle to build the project.
Scala class:
class Order {
def foo(): Unit =
{
}
}
Java class:
common.Order o = new common.Order();
As described in the documentation of the Scala plugin, for Joint Compilation (Java uses Scala and / or Scala uses Java), put both, Scala and Java files that take part in the Joint Compilation in the src/main/scala directory.

gradle: compile android junit test apk file

Can I compile Android JUnit test apk file by using gradle script? Now my test class is:
public class main extends ActivityInstrumentationTestCase2<LoginWindow> {
public main() {
super("com.tecomgroup.handifox", LoginWindow.class);
}
...
}
and gradle says that he cannot find class LoginWindow. Should I add the LoginWindow.java to dependencies {} block? Will such test work? Or may be there is another way to compile test apk file?
When using Gradle Android plugin, you no longer need to have a separate project for testing. Production sources should be put into src/main/java directory, test sources should be in src/instrumentTest/java. The same applies to resources.
From Android Gradle plugin User Guide on project structure.
Project Structure
The basic build files above expect a default folder structure. Gradle follows the concept of convention over configuration, providing sensible default option values when possible.
The basic project starts with two components called “source sets”. The main source code and the test code. These live respectively in:
src/main/
src/instrumentTest/
Inside each of these folders exists folder for each source components.
For both the Java and Android plugin, the location of the Java source code and the Java resources:
java/
resources/
For the Android plugin, extra files and folders specific to Android:
AndroidManifest.xml
res/
assets/
aidl/
rs/
jni/
Note: src/instrumentTest/AndroidManifest.xml is not needed as it is created automatically.
You can change the standard project layout
sourceSets {
instrumentTest {
java {
srcDir '../other/src/java'
}
resources {
srcDir '../other/src/resources'
}
}
}

Categories

Resources