eclipse and gradle: do not deploy test classes - eclipse

We have here a JEE Vaadin project in eclipse (neon) IDE and as build system we use gradle. But we have a problem with our test classes. If we build our war/ear with gradle all is fine. The test classes are not part of the war archive.
But if we want to deploy the ear project on the wildfly server from inside of eclipse it seems that the test classes are part of the war archive. Because while deploy the ear we getting some errors like:
...
Caused by: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: java.lang.LinkageError: Failed to link com/coco_on/docFlow/vaadin/admin/TestAdminRoles (Module "deployment.DFl_EAR.ear.DFl_Vaadin.war:main" from Service Module Loader)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)
at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
at org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue(LoadingCacheUtils.java:49)
at org.jboss.weld.resources.DefaultReflectionCache.getBackedAnnotatedTypeAnnotationSet(DefaultReflectionCache.java:169)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.getAnnotations(BackedAnnotatedType.java:112)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.getAnnotation(BackedAnnotatedType.java:98)
at org.jboss.weld.bootstrap.BeanDeployer.processPriority(BeanDeployer.java:105)
at org.jboss.weld.bootstrap.BeanDeployer.processAnnotatedTypes(BeanDeployer.java:174)
at org.jboss.weld.bootstrap.BeanDeployment.createTypes(BeanDeployment.java:224)
at org.jboss.weld.bootstrap.WeldStartup.startInitialization(WeldStartup.java:381)
at org.jboss.weld.bootstrap.WeldBootstrap.startInitialization(WeldBootstrap.java:76)
at org.jboss.as.weld.WeldStartService.start(WeldStartService.java:92)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
... 3 more
Caused by: java.lang.NoClassDefFoundError: com/vaadin/testbench/TestBenchTestCase
...
TestAdminRoles is one of our test classes and TestBenchTestCase is one of the needed classes in a test framework (Vaadin Testbench). But this test framework will be not found and this is also ok, because we don't want link this library into the ear/war.
We have also read this post: https://stackoverflow.com/a/9820317/1465758
So we have removed the src/test/ directories from the deployment and set a different bin directory for the test source directories. But without success. The error above is still there.
And so my question is: How can I configure eclipse so that eclipse don't put the compiled test classes to the war file?

You need to change the output-folder of the sourcefolders for test-sources and remove the test-directories from the deployment assembly.
This should be added to the build.gradle. (the eclipse-plugin needs to be also applied)
eclipse {
// change output dir of the sourcefolders
classpath.file.whenMerged {
entries.findAll { it instanceof org.gradle.plugins.ide.eclipse.model.SourceFolder }.each {
it.output = 'build/eclipse/' + (it.path.startsWith('src/test') ? 'output-test' : 'output')
}
}
// removes test dirs from deployment assemble
wtp.component.file.whenMerged {
wbModuleEntries.removeIf {
it instanceof org.gradle.plugins.ide.eclipse.model.WbResource && it.sourcePath.startsWith('/src/test')
}
}
}
Code Source (small changes where applied)
In this example the output-directory from non test-sources are also changed, that all output-directories are inside the build-directory from Gradle.

Related

eclipse ResolutionException: Modules A and B export package P to module C

I try to migrate my java8 spring project to java11. Now I get the following exception when I try to run it from eclipse:
Error occurred during initialization of boot layer
java.lang.module.ResolutionException: Modules java.activation and jakarta.activation export package javax.activation to module spring.boot.starter.web
Under Referenced Libraries I only found jakarta.activation-api-1.2.2.jar which exports the package javax.activation. The other module java.activation I have no clue where it comes from. From the name it should be inside JavaSE-11/JDK ? I checked the entry JRE System Library but I don't see that package there.
Now the curios thing is with Gradle 6.5 I can run the project using "gradlew bootRun" and it executes nicely. However in eclipse it fails with the errror.
So in eclipse I just tried to remove Jakarta.activation by Right-click remove from build path. Trying to import anything from javax.activation then gives me "import can't be resolved", fine so far. However running still complains with the above ResolutionException.
So to fix the issue:
Where is the other sourcecode that exports javax.activation package? And how do I find that?
How can I prevent eclipse from having those two modules at runtime?
Can I exclude the module in gradle, such that the project works after running "gradlew eclipse" like always?
Thanks for your help! I spent hours searching and didn't find anything useful so far.
The build.gradle looks as following:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'eclipse'
}
group = 'example'
version = '0.3.0'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
configurations.all {
// fix multiple slf4j dependencies are present
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
//TODO: 1st approach to fix ResolutionException
//exclude group: 'jakarta.activation', module: 'jakarta.activation-api'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:2.3.1.RELEASE'
implementation ('org.springframework.boot:spring-boot-starter-web:2.3.1.RELEASE') {
//TODO: 2nd approach to fix ResolutionException
exclude group: 'jakarta.activation', module: 'jakarta.activation-api'
}
implementation 'org.springframework.boot:spring-boot-starter-security:2.3.1.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.3.1.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.3.1.RELEASE'
implementation 'mysql:mysql-connector-java:5.1.46'
implementation 'com.querydsl:querydsl-jpa:4.1.4'
implementation 'com.querydsl:querydsl-apt:4.1.4:jpa'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.flywaydb:flyway-core:5.2.4'
compileOnly 'org.springframework.boot:spring-boot-devtools:2.3.1.RELEASE'
// https://mvnrepository.com/artifact/org.apache.odftoolkit/simple-odf
implementation 'org.apache.odftoolkit:simple-odf:0.8.2-incubating'
// https://mvnrepository.com/artifact/org.apache.commons/commons-text
implementation 'org.apache.commons:commons-text:1.1'
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation 'org.apache.commons:commons-lang3:3.7'
// https://mvnrepository.com/artifact/org.apache.commons/commons-csv
implementation 'org.apache.commons:commons-csv:1.5'
// https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.3
implementation 'org.apache.commons:commons-collections4:4.3'
implementation 'javax.validation:validation-api:2.0.0.Final'
// Dependencies that are no longer in java11
// implementation 'javax.xml.bind:jaxb-api:2.3.0'
// implementation 'com.sun.xml.bind:jaxb-core:2.3.0'
// implementation 'com.sun.xml.bind:jaxb-impl:2.3.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.5.2'
}
eclipse {
classpath {
downloadJavadoc = true
downloadSources = true
}
}
Other links I found but didn't help me so far:
https://dba-presents.com/index.php/jvm/java/159-error-java-module-xyz-reads-package-org-apache-commons-logging-from-both-commons-logging-and-jcl-over-slf4j
Modules A and B export package some.package to module C in Java 9
Two Modules exports the same package (Spring)
https://forum.byte-welt.net/t/resolutionexception-module-a-module-b-to-module-c/20843/2
Ok I found part of an answer while trying to create an example to reproduce the issue.
First I tried to clean the project to make sure nothing bad is cached in eclipse:
gradlew clean eclipse
Still the problem occured.
Now I went full ham and removed all build files, .project, .classpath and reran gradlew eclipse and while adding a new run configuration the project now starts fine.
So probably blame the cache of the run-configuration.
Maybe this or the cross-links to other posts about the same issue still helps someone.

Running Vert.x (w/ES4X) via Eclipse

This question is a follow-up to:
Running Vuetify on Vert.x (w/ES4X)
I would like to be able to run ES4X via Eclipse (instead of NPM). I'm not exactly sure if it's possible or how to wire it in.
Let's say I have the following build.gradle.file
plugins {
id 'java'
id 'application'
id 'com.johnrengleman.shadow' version "5.0.0"
}
sourceCompatibility='1.8'
mainClassName='io.vertx.core.Launcher'
repositories {
mavenCentral()
}
dependencies {
implementation 'io.vertx:vertx-core:3.7.1'
implementation 'io.vertx:vertx-web:3.7.1'
implementation 'io.vertx:vertx-lang-js:3.7.1'
// implementation 'io.reactiverse:es4x:0.8.0'
// implementation 'io.reactiverse:es4x-pm:0.8.0'
}
processResources {
from '/src/main/js'
}
shadowJar {
classifier = 'fat'
manifest {
attributes 'Main-Verticle' : 'index.js'
}
mergeServiceFiles {
include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
}
}
and my src/main/js/index.js looks like the one from the other referenced post:
import { Router, StaticHandler } from '#vertx/web';
const app = Router.router(vertx);
app.get().handler(StaticHandler.create("dist"));
vertx.createHttpServer().requestHandler(app).listen(8080);
If I create an executable jar via shadowJar, I get javax.script.ScriptExceptions due to Nashorn choking on the index.js contents (as expected).
If I uncomment the es4x implementation in the gradle build, I get ClassNotFound exceptions for org.graalvm.polyglot.io.FileSystem
So how would I correctly modify this app to take advantage of ES4X? I guess the equivalent of what the 'es4x init' would do?
Say that you have your es4x application and a package.json, when you execute:
npm install
You will get inside node_modules a few extra folders:
.bin
.lib
In the .bin directory there is a es4x-launcher.jar file you can use to start your application from Eclipse. It will refer to the dependencies which are unpacked to the .lib dir. In order to make things work fine the same JVM you have when running the npm install command should be used in eclipse. Otherwise you might end up missing dependencies. This is the case when running graalvm which will not require graaljs dependencies or when running on jdk8 which will not require jvmci dependencies.

How to to tell Eclipse/Buildship/Gradle to add dependencies to the Modulepath

I am trying to use Eclipse, Buildship, Gradle to develop java 9 applications.
Eclipse: Oxygen
Buildship: 2.2.0
Gradle: 4.3.1
I am looking for a way to tell Buildship/Gradle to add Project and External Dependencies to the Modulepath rather than the Classpath.
Here's a representation of what I see when I configure my Eclipse project Java Build Path.
Properties for TestMain
Java Build path
Source | Projects | Libraries | Order and Export
---------
Modulepath
- JRE System Library [JavaSe-9]
Classpath
- Project and External Dependencies
- Access rules: No rules defined
- External annotations: (None)
- Native library location: (None)
- coreutil-9.4.1.jar
- slf4j-api-1.7.2.1.jar
- ...
When I try to reference the automatic module coreutil in module-info.java I get the error coreutil cannot be resolved to a module.
If I manually add coreutil-9.4.1.jar to the Modulepath then the coreutil module becomes visible. This is a problem, however, since it is impractical to manually add over 60 libraries in some cases. Moreover, each time I Refresh Gradle Project they are all removed from the Modulepath.
Thanks for help.
Gaëtan
After talking to Donát Csikós at gradle (thanks Donát) adding the following to the build.gradle file solves the problem:
apply plugin: 'eclipse'
eclipse.classpath.file {
whenMerged {
entries.findAll { isModule(it) }.each { it.entryAttributes['module'] = 'true' }
}
}
boolean isModule(entry) {
// filter java 9 modules
entry.kind == 'lib' // Only libraries can be modules
}

Debugging OfBiz in Eclipse gives MissingResourceException

I downloaded the OfBiz Java application and the following line throws an MissingResourceException:
ResourceBundle res = ResourceBundle.getBundle(settingsResourceName);
The value of settingsResourceName is "cache", but I cannot find any file called cache.properties or cache_en.properties.
Where should I be looking? I'm new to Java. All my research on SO says there should be such a file.
I imported OfBiz in Eclipse using the Import menu option and selecting Existing Project from File System (I'm not in front of my dev machine so I don't remember the exact wording). But I chose the root folder of the downloaded OfBiz.
I then added the appropriate VM Arguments in the Run Configuration to get it to run properly at least. And that's it, on the first Run I got the above error. I think it has to do with a missing class path but I don't know what to add to class path.
Here is the stack trace:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.ofbiz.base.util.Debug.<clinit>(Debug.java:68)
at org.apache.ofbiz.base.container.ContainerLoader.load(ContainerLoader.java:61)
at org.apache.ofbiz.base.start.StartupControlPanel.loadStartupLoaders(StartupControlPanel.java:202)
at org.apache.ofbiz.base.start.StartupControlPanel.start(StartupControlPanel.java:69)
at org.apache.ofbiz.base.start.Start.main(Start.java:84)
Caused by: java.util.MissingResourceException: Can't find bundle for base name cache, locale en
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:773)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:174)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:170)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:166)
at org.apache.ofbiz.base.util.cache.UtilCache.<init>(UtilCache.java:124)
at org.apache.ofbiz.base.util.cache.UtilCache.createUtilCache(UtilCache.java:769)
at org.apache.ofbiz.base.util.UtilProperties.<clinit>(UtilProperties.java:75)
... 5 more
UPDATE:
My mistake, I found two files both called cache.properties in the following folders:
ofbiz-trunk/build/resources/main
ofbiz-trunk/framework/base/config
But these are folders, not packages. I tried putting them in the .classpath but that did not work, I still kept getting the same error.
As suspected, I knew it was because of a missing reference to a class path. After looking at a section on this page: http://www.opensourcestrategies.com/ofbiz/ofbiz_eclipse.php, I learned that I was supposed to go to the Java Build Path and in the Libraries tab, click on Add Class Folder, then point that to ofbiz-trunk/framework/base/config. Which is where I have one of the cache.properties files.

Custom Gradle Plugin ID not found

I'm writing a Gradle plugin and I'm failing to get the apply plugin: command to work in the Gradle script that uses the plugin. I'm using Gradle 1.1.
I've build the plugin with clean build and I'm attempting to add it to the Gradle build via a flat repo for now. That seems to be working but Gradle isn't picking up that there is a plugin with the ID test-plugin. The project name in the plugin's settings.gradle is test-plugin and the properties file in META-INF/gradle-plugins is also test-plugin.properties. I'm not sure where else I can specify the plugin ID.
The build.gradle file in the project that is using the test-plugin:
repositories {
flatDir name: 'libs', dirs: "../build/libs"
}
dependencies {
compile 'test:test-plugin:0.1'
}
apply plugin: 'test-plugin'
Error from Gradle:
What went wrong:
A problem occurred evaluating root project 'tmp'.
Plugin with id 'test-plugin' not found.
The plugin Jar has to be added as a build script dependency:
buildscript {
repositories { flatDir name: 'libs', dirs: "../build/libs" }
dependencies { classpath 'test:test-plugin:0.1' }
}
apply plugin: "test-plugin"
If you want to implement a plugin into your buildscript, then you have two options.
Option 1
apply plugin: YourCustomPluginClassName
Option 2
plugins {
id 'your.custom.plugin.id'
}
apply plugin: is used when specifying your plugin by its class name (ex. apply plugin: JavaPlugin)
plugins { } is used when specifying your plugin by its id (ex. plugins { id 'java' })
See Gradle Plugins by tutorialspoint for reference
If you choose Option 1, the your custom plugin will need to be brought into your build script by 1 of 3 ways.
You can code it directly within your Gradle build script.
You can put it under buildSrc (ex. buildSrc/src/main/groovy/MyCustomPlugin).
You can import your custom plugin as a jar in your buildscript method.
See Gradle Goodness by Mr. Haki for information about the buildscript method.
If you choose Option 2, then you need to create a plugin id. Create the following file buildSrc/src/main/resources/META-INF/gradle-plugins/[desired.plugin.id].properties.
Copy and paste implementation-class=package.namespace.YourCustomPluginClassName into your newly created .properties file. Replace package.namespace.YourCustomPluginClassName with the fully-qualified class name of your desired plugin class.
See Custom Plugins by Gradle for more info.
I also had the same problem with a custom plugin id not being found. In my case, I simply forgot to add the 'src/main/resources/META-INF/gradle-plugins' properties file. The name of the properties file must match the name of the plugin id with a '.properties' extension.
The file must contain a the line:
implementation-class=(your fully qualified plugin classpath)
That's the complete mechanism on how plugin id's get resolved to class names.
In addition the plugin needs to be added as a dependency as pointed out in the previous answer. The android documentation states that you should use a name associated with your unique domain name. I.e.: the name 'test-plugin' is not really in good form, but an id like 'com.foo.gradle.test-plugin' would be better.
Ensure that your top-level build.gradle uses the correct classpath to refer to the path to the built *.jar file.
Some plugins, like maven-publish, will build and save the jar to a specific location in mavenLocal, but the path may not be clear to see.
You should look at the file path of the jar file, and ensure it matches your classpath, but the mapping is not immediately obvious:
buildscript {
dependencies {
// This *MUST* match the local file path of the jar file in mavenLocal, which is:
// ~/.m2/repository/com/company/product/plugin/product-gradle-plugin/1.0/product-gradle-plugin-1.0.jar
classpath 'com.company.product.plugin:product-gradle-plugin:1.0'
}
}
Be careful not to use the wrong classpath, which can refer to a directory instead of the actual jar file; like this:
buildscript {
dependencies {
// This is wrong, because it refers to the mavenLocal FOLDER "product-gradle-plugin",
// instead of the jar FILE "product-gradle-plugin-1.0.jar"
// However, a gradle sync will still resolve it as a valid classpath!!
classpath 'com.company.product:product-gradle-plugin:1.0'
}
}
More info:
https://discuss.gradle.org/t/what-is-the-preferred-gradle-approach-to-locally-install-an-artifact-equivalent-to-mavens-install/5592
https://docs.gradle.org/current/userguide/publishing_maven.html
https://blog.codefx.org/tools/snapshots-gradle-maven-publish-plugin/
https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project
Adding to what #Bonifacio2 wrote this is a path META-INF/gradle-plugins and shows in IntelliJ as META-INF.gradle-plugins. At all costs don't make the stupid mistake I did creating this directly as a directory META-INF.gradle-plugins because you are based on another sample, and never works. Another tip is copying also from another intelliJ project as this is what is added: gradle-plugins.
hmm perhaps try;
configure <org.jsonschema2pojo.gradle.JsonSchemaExtension> {
this.sourceFiles = files("${project.rootDir}/schemas")
}