How to setup JUnit testing in Gluon Project with Gradle - eclipse

I am trying to setup JUnit testing in my Gluon JavaFX Application. I am using the Gluon Eclipse Plugin with Gradle and Java 8.
My build.gradle file looks like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b10'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
}
dependencies {
compile 'com.gluonhq:ignite-dagger:1.0.0'
compile 'org.elasticsearch:elasticsearch:1.6.0'
compile 'ch.qos.logback:logback-classic:1.1.5'
testCompile 'junit:junit:4.12'
}
mainClassName = 'com.me.MyApplication'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
}
}
Resolving the dependency is no problem, but when running the 'test' task, gradle throws an error like this:
When running gradle with java 8, you must set the path to the old jdk, either with property retrolambda.oldJdk or environment variable JAVA6_HOME/JAVA7_HOME
Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.2.1-all.zip'.
I already tried to add the retrolambda plugin to gradle according to the plugin's README on GitHub, but it doesn't work so far. Could someone tell me what to do to configure my Gluon project so that I am able to run my JUnit tests with Gradle?
Some important addidtions:
For the plugin version it says: Gluon Tools 1.0.0.201508201514
I think I fogot to mention that I want to use Dagger dependency injection with Gluon Ignite which might be the real problem in my case as it requires Java 8 and might conflict with javafxports or something else. However, I'm not able to make full sense of the various error messages I've seen.
My tests are empty, but they aren't even run, because it fails before.

Your problem seems like a retroLambda configuration issue. If you go through the configuration page for the plugin, it states that if you don't have an environment variable set for JAVA6_HOME or JAVA7_HOME than you need to explicitly define oldJdk for the plugin to work properly.

Related

In Eclipse: Could not find or load main class org.testng.remote.RemoteTestNG

Configuration
I have version 2019-12 (4.14.0) of Eclipse (having recently upgraded from 2019-03) with the following plugins installed (amongst others):
TestNG 6.14.3.201902250526
TestNG M2E (Maven) Integration (Optional) 6.14.3.201902250526
TestNG P2 Features 6.14.3.r201802240500
Eclipse is running against jdk1.8.0_152.
In Eclipse, my Java Build Path includes testng-6.14.3. I've tried to match the Eclipse plugin version against the version using in the project.
Under Properties -> TestNG I have all the default settings. There is no setting here for "Use Project TestNG.jar".
What Works
When I run tests from the command line with Gradle they all pass with no issue.
There are no complile errors showing in Eclipse.
What Does Not Work
When I run a test from Eclipse (by right clicking on a class) the console shows:
Error: Could not find or load main class org.testng.remote.RemoteTestNG
It appears I need a copy of testng-remote.jar. Should this have been installed as part of the plug-in, or am I meant to have included that in my build.gradle as a dependency?
Why can't I run tests from Eclipse? Do I need to install something further to resolve this error?
What I have Tried
I have tried removing and re-installing the TestNG plugin.
It seems you hit the bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=558495 in Windows.
I reverted my version back to 2019-09 because of it.
The bug causes jars which should be on the classpath not to be on the classpath. This affects projects with long classpaths.
Can you try adding it in your build.gradle.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
classpath group: 'org.testng', name: 'testng', version: '6.8.+'
} }
Change the testng version as per your usage.
I don't remember such problem for gradle but it looks similar to the problem with testing on maven(with auto-build turned off). The soultion is to rebuild project (for maven it ia alt +f5) and because of such rebuilding - cache is updated.
You have detailed TestNG gradle tutorial
By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. When we specify useTestNG(), Gradle scans for all the methods annotated with #Test and execute them.
Example 1. Below is the basic configuration for the 'test' task
plugins {
id 'java'
}
group 'com.test'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.testng', name: 'testng', version: '6.14.3'
}
test {
useTestNG()
}
For Gradle use testCompile dependencies:
repositories {
jcenter()
}
dependencies {
testCompile 'org.testng:testng:6.10'
}
For specific version you have options,
Install from update site
Select Help / Install New Software...
Enter the update site URL in "Work with:" field:
Update site for release: http://dl.bintray.com/testng-team/testng-eclipse-release/.
Make sure the check box next to URL is checked and click Next.
Eclipse will then guide you through the process.
You can also install older versions of the plug-ins here. Note that the URL's on this page are update sites as well, not direct download links.
Build TestNG from source code
you can download the source and build the distribution yourself:
git clone git://github.com/cbeust/testng.git
cd testng
./build-with-gradle

How to build an Xtext Eclipse plug-in with Gradle?

I have written a DSL with Xtext 2.12 and my Xtend 2.12 code generator produces Java 8 code from it. I am using Eclipse Oxygen.3a. I started by creating an Xtext project as an Eclipse plug-in and all works fine, but I would like to have everything built by Gradle now.
For that purpose, I have added the Gradle nature to my Xtext project and launched 'gradle init' to generate the build.gradle and settings.gradle files. I have discovered the existence of Xtext Gradle Plugins (Xtext Builder and Xtend Compiler), and used the documentation of these plugins to write the build.gradle script content, but I did not understand everything and, not surprisingly, my builder does not work. Could you help me please to set this builder up correctly?
plugins {
id 'org.xtext.builder' version '2.0.3'
id "org.xtext.xtend" version "2.0.3"
}
apply plugin: 'java'
apply plugin: 'org.xtext.xtend'
apply plugin: 'eclipse'
sourceSets {
main.java.srcDirs = ['src','xtend-gen']
main.xtendOutputDir = 'xtend-gen'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile group: 'org.eclipse.xtend', name: 'org.eclipse.xtend.lib', version: '2.16.0'
compile group: 'org.eclipse.xtext', name: 'org.eclipse.xtext', version: '2.16.0'
compile group: 'org.eclipse.emf', name: 'org.eclipse.emf.codegen.ecore', version: '2.15.0'
compile group: 'org.eclipse.xtext', name: 'org.eclipse.xtext.xtext.generator', version: '2.16.0'
testCompile group: 'org.eclipse.xtext', name: 'org.eclipse.xtext.xbase.testing', version: '2.16.0'
}
xtext {
languages {
mapy {
setup = 'com.mchp.mapy.MapyStandaloneSetup'
generator.outlet.producesJava = true
}
}
sourceSets {
main {
srcDirs 'src','xtend-gen'
output {
dir(xtext.languages.mapy.generator.outlet, 'src-gen')
}
}
}
}
xtend {
}
Where should the sourceSets block be located (inside or outside the
xtext block)?
Is the xtext block content correct?
What should I write into the xtend block? Should it even be declared?
What is the Gradle task to be run to start the MWE2 launcher and
then the code generator?
Following is a snapshot of my Eclipse project organization.
Thanks in advance for your time!
It is also possible to build the Xtext plugins using Eclipse PDE. I created a dedicated Eclipse deployment to keep the size down (minimal then install the PDE, JDT and Xtext projects) which is published to an artifact repository. It's then just a matter of writing Gradle tasks (or a plugin) to pull down the Eclipse, extract it to the build directory, copy in the Xtext Eclipse projects (use the Xtext Gradle plugin to build the DSL before copying) and finally call the Ant PDE task(s) using the Java command and the AntRunner inside the Eclipse. The output should be a basic update site from which you can either install the plugins from locally or publish to the Artifact repository to share.
A good understanding of Eclipse PDE build process is required. I found most of what I needed in the Eclipse help (https://help.eclipse.org/2020-06/index.jsp) under section "Plug-in Development Environment Guide > Tasks > PDE Build"
I have encountered some issues during Xtext version upgrades with various dependency conflicts and Eclipse bugs. These can usually be resolved with the help of the Xtext dev team and then forcing certain dependencies in Gradle.
Unfortunately I can't share any of the build as it's a proprietary code, but I hope the explanation above is enough for anyone who needs to automate the process. It's certainly not a simple thing to set up though.

importing dependencies into eclipse using gradle

I've got what appears to be an adequate gradle file but eclipse refuses to recognise the dependancy I'm trying to import (despite refreshing the gradle project after implementing the dependancy)
My gradle file lacks a buildscript{} block but I'm not familar enough with gradle to implement it. My build file was autogenerated by eclipse so I don't see why it'd be an issue.
I'm trying to import "com.intrinio:sdk:0.0.1"
build.gradle:
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
compile "com.intrinio:sdk:0.0.1"
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:21.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
I think this is the lib you want to use.
As it's not deployed in a public repository, you have to manually install it. Checkout the project from github and do as below :
Installation
To install the Intrinio Java SDK to your local Maven repository, simply execute:
mvn install
Then, in your gradle file, add mavenLocal() in the repository section and it should just work fine.

How to make gradle / intellij / play framework work together?

Here is a Gradle build as advised here for using play web framework.
plugins {
id 'play'
id 'idea'
}
repositories {
jcenter()
maven {
name "typesafe-maven-release"
url "https://repo.typesafe.com/typesafe/maven-releases"
}
ivy {
name "typesafe-ivy-release"
url "https://repo.typesafe.com/typesafe/ivy-releases"
layout "ivy"
}
}
It works fine when building, launching etc... from command line but once the project is imported in intellij (idea's project files generated with gradle idea), dependencies (from the play plugin) don't show up in the project view/external libraries (even after having hit "refresh all gradle projects" in the gradle panel).
Thanks :)
PS: intellij 15.0.2 / gradle 2.6 / play plugin
Answer found here. Apparently the gradle idea plugin needs to be told explicitely how to wire the dependencies.
To sum up :
Create a typical play layout
Add a build.gradle (as below)
Type gradle idea to generate idea's project files
Open the project in intellij
plugins {
id 'play'
id 'idea'
}
repositories {
jcenter()
maven {
name "typesafe-maven-release"
url "https://repo.typesafe.com/typesafe/maven-releases"
}
ivy {
name "typesafe-ivy-release"
url "https://repo.typesafe.com/typesafe/ivy-releases"
layout "ivy"
}
}
idea {
module {
sourceDirs += file("app")
testSourceDirs += file("test")
scopes.COMPILE = [plus: [configurations.play], minus: []]
scopes.RUNTIME = [plus: [configurations.playRun], minus:[configurations.play]]
scopes.TEST = [plus: [configurations.playTest], minus: [configurations.playRun]]
}
}
PS: tested with intellij 15.0.2 / gradle 2.10 / gradle play plugin
I'm using Intellij (2017 and 2018), Gradle (4.6), and Play (2.5). The three of these do not mix together out of the box.
Without help, IDEA won't recognize dependencies, and will be little more than Notepad -- no code completion, no navigation. This is because IDEA looks at the configuration model of the Java plugin, but the 'play' plugin uses the "new" software model. I don't think either side it going to change this to meet the other any time soon.
The 'idea' plugin writes file-based project files that do not work very well, and have to be refreshed manually. I would advise that you not use the 'idea' plugin at all.
Using 'java' and 'play' plugins, you can help IDEA understand what you want by:
(1)
Moving your Play source files around to follow the pattern expected by the 'java' plugin:
app -> src/main/java
test -> src/test/java
You can make a symbolic link from ./test to src/test/java, or muck with the play model with plugin code. The path ./test is hardcoded (!) in the 'play' plugin, but I find IDEA works better if it finds it in src/test/java.
(2)
Configure the play plugin to get its input from the Java plugin source directories. Something like this:
model {
components {
play {
sources {
java {
source.setSrcDirs files("src/main/java")
}
resources {
source.setSrcDirs files("src/main/resources")
}
routes {
// leave in ./conf
}
}
}
}
}
(3)
Configure the java plugin to put its output in the play plugin output directories. Something like this:
sourceSets {
main {
java.outputDir = file('build/playBinary/classes')
}
test {
java.outputDir = file('build/playBinary/testClasses')
}
}
(4)
Give IDEA a bit more help to figure this out:
configurations {
compile.extendsFrom(play)
testCompile.extendsFrom(playTest)
implementation.extendsFrom(playRun)
implementation.extendsFrom(playPlatform)
}
If you do this, you can then import the Gradle project directly into IDEA, use the Gradle runner for tests, and refresh the gradle project at any time.
I realize this answer comes years late, but there is not a lot of information out there, and this answer may help people still searching for this in the distant future.
+-+-+-+-+-+-+-+-
EDIT: I've stopped using the above approach as it is too unreliable. Instead, I wrote my own plugin to replicate the twirl and routes compiler steps of sbt, and then just use the traditional scala (which uses java) gradle plugin, and also groovy (for spock tests). I use the application plugin to build the distribution. I import it as a gradle build in Intellij and everything "just works". I recommend not using the 'play' plugin at all.
Use the straight-forward gradle plugins. Avoid the 'idea' and 'play' plugins. Import the project into Intellij as a gradle build -- even the traditional JUnit runner works fine. Stay away from the "new" gradle software model in your plugins. Run and debug directly from Intellij as for any other Java application.
This means giving up the "development mode" of sbt, and always running the "ProdServerStart" main (but you can still run in dev or test "mode" of Play). But I'm not missing sbt. :^) When running ProdServerStart, for example, you always run out of a jar with a traditional classpath. You'll need to change the application secret from "changeme", and you will have to put your asset jar on the classpath. This is all straight-forward JVM and no sbt secret sauce (other then twirl and route compilers).
I'll come back with more details but for now:
#TaskAction
public void generate() {
getSource().forEach(sourceFile -> {
TwirlCompiler.compile(sourceFile,
getAppDir(),
getDestinationDir(),
FORMATTER,
ADDITIONAL_IMPORTS,
Codec.UTF8(),
false,
false);
});
}
and
#TaskAction
public void generate() {
getSource().forEach(sourceFile -> {
RoutesCompiler.RoutesCompilerTask routesCompilerTask = new RoutesCompiler.RoutesCompilerTask(sourceFile,
ADDITIONAL_IMPORTS,
true,
true,
false);
RoutesCompiler.compile(routesCompilerTask, INJECTED_ROUTES_GENERATOR, getDestinationDir());
});
}
should get you started if you are comfortable writing gradle plugins in Java.
First of all you have to change your project according to gradle play plugins. here is the links, how to change your sbt project to gradle and make sure that you can execute all the necessary commands(build, clean, runPlayBinary, etc) from command prompt or terminal. When you are able to build successfully then you can import in Intellj IDE. Here is the steps you have to follow. Add idea plugins and following config for idea source configuration in build.gradle file.
plugins {
....
id 'idea'
}
..
idea {
module {
sourceDirs += file("app")
testSourceDirs += file("test")
scopes.COMPILE = [plus: [configurations.play], minus: []]
scopes.RUNTIME = [plus: [configurations.playRun], minus:[configurations.play]]
scopes.TEST = [plus: [configurations.playTest], minus: [configurations.playRun]]
}
}
...
Type "./gradlew idea" to generate idea's necessary project files
Open the project in intellj(Don't import)
Setup sdk(java) from project structure
In IDE at the bottom, you will see message like "Unlinked Gradle project? Import Gradle project, this will also enable Gradle Tool Window. Don't want to see the message for the project again: press here."
click on this message an enable gradle fort this project and setup gradle home and JVM(not project jvm) for gradle
To Run(Run config)
Open run configuration window
Click on(+) and Select Gradle
Name: as you need
Gradle project: your root project or location of your root project
Tasks: runPlayBinary
Go to intellj preference
Go to [Build, Execution and Deployment] section
Select build tool as gradle
Set "Service diretory path" ex: "/Users/.../gradle/gradle-4.10.2"
uncheck "offline work"`
Tested with Grade 4.10.2, IntelliJ IDEA 2018.3.2 (Ultimate Edition) snd Play 2.5.2

Writing a buildSrc Gradle plugin that can be published

I'm writing a custom Gradle plugin for my company to assist with integration tests of our product. My team wants to have the plugin be built with and used in the main product build (like a 'buildSrc' plugin would), but also need the plugin to be published as an artifact for other teams to use in integration with our product.
If I try and include it as a standalone plugin in the settings.gradle file and then also include it in the buildscript as a dependency, it obviously does not work because the buildscript block is interpreted first.
I also tried running another build from within the buildscript like so:
buildscript {
def connection = GradleConnector.newConnector()
.forProjectDirectory(file("${project.projectDir}/theplugin"))
.connect()
try {
connection.newBuild()
.forTasks('clean', 'build', 'install')
.run()
} finally {
connection.close()
}
repositories {
mavenLocal()
...
}
dependencies {
classpath 'com.company.product.gradle.theplugin'
}
}
This causes the plugin to be built and placed in the local Maven repo, but then the initial Gradle build fails directly afterward because it can't resolve the newly built archive. If I run it again, it works. I don't understand this behavior.
I'm probably going down a rabbit hole with this approach. Is there a way to make this work and in a less 'hacky' way?
I discovered a hacky way to accomplish this: symlink the plugins to the buildSrc (on *nix at least).
project directory
project/
buildSrc/ -> gradle_plugins/
gradle_plugins/
pluginA/
pluginB/
...
build.gradle
settings.gradle
...
build.gradle
settings.gradle
project/settings.gradle
include 'gradle_plugins:pluginA'
include 'gradle_plugins:pluginB'
...
project/gradle_plugins/settings.gradle
include 'pluginA'
include 'pluginB'
...
project/gradle_plugins/build.gradle
...
rootProject.dependencies {
runtime project(path)
}
...
The way I'm solving this is the following:
Regular multi project build with buildSrc/myPlugin/.. Within my build process I call ./gradlew -b buildSrc/myPlugin/build.gradle uploadArchives (or whichever task you use to publish your maven artifact).
Due to the "official" hack of having to add the gradle plugin to the runtime dependencies of the root project this step would fail. So I surround it with a try catch. I feel that is not perfect but it seems to work.