Adding a module export to the Eclipse .classpath file - eclipse

I have an older project which needs to have a module export in Eclipse's .classpath file, so that it can resolve some classes from this module. The classpath entry looks like this if I generate it via Eclipse's build path editor:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/">
<attributes>
<attribute name="module" value="true"/>
<attribute name="add-exports" value="java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED"/>
</attributes>
</classpathentry>
Naturally, I'd like to have that entry generated by Gradle and I have finally managed to do that:
eclipse.classpath.file {
whenMerged { // remove any JRE containers
entries.findAll{ it.path ==~ '.*JRE_CONTAINER.*' }.each { entries.remove(it) }
}
withXml { // add one with the required export
def node = it.asNode()
def cpe = new Node(node, 'classpathentry', [kind: 'con', path: 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/'])
def attrs = new Node(cpe, 'attributes')
new Node(attrs, 'attribute', [name: 'module', value: 'true'])
new Node(attrs, 'attribute', [name: 'add-exports', value: 'java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED'])
}
}
But this seems crude and overly verbose. Is there a simpler way to do something like this?
Update: Also, this only works when running gradle eclipse, but not when doing a "Refresh Gradle Project" with Buildship - as that doesn't consider withXml. So I'd need to create a Container in whenMerged and add attributes to it, which I haven't been able to do.

I've found the solution, the attribute(s) nodes are accessible via the entryAttributes field of the AbstractClassEntry class.
This way, I can just do...
eclipse.classpath.file {
whenMerged {
entries.find{ it.path ==~ '.*JRE_CONTAINER.*' }.each {
it.entryAttributes['module'] = true
it.entryAttributes['add-exports'] = 'java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED'
}
}
}
...and it will be applied by Buildship, too.

Related

Gradle-Scala Project Error - Could not load Main Class

I have created a gradle/scala project in intellij.
This is my project structure
build
gradle
- wrapper
META-INF
out
src
- main
-resources
-scala
- test
-resources
-scala
build.gradle
gradlew
gradlew.bat
settings.gradle
These are the contents of my build.gradle file
plugins {
id 'scala'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenLocal()
maven {
url = uri('<artifactory location>')
}
}
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'org.example.Hello'
)
}
}
sourceCompatibility = '12'
dependencies {
implementation 'org.scala-lang:scala-library:2.12.14'
testImplementation 'org.scalatest:scalatest_2.12:3.0.5'
testRuntimeOnly 'org.scala-lang.modules:scala-xml_2.12:1.1.1'
}
test {
useJUnitPlatform()
}
Main Class Content
package org.example
object Hello extends App {
println("hello!")
}
When I try to build the jar and execute it , it gives an error
Could not find or load main class org.example.Hello . What might be the mistake here?
I was able to resolve the issue by adding the following in the settings.gradle
rootProject.name = 'gradle-scala'
include('gradle-scala')
Basically , the include tag is used to specify the directory where the code resides .
I see out directory, can you check the gradle settings
ensure below are ticked

Is it possible to extend a gradle build script configured in a binary plugin?

I've created a Gradle plugin below:
class CommandServiceProjectPlugin implements Plugin<Project> {
public void apply(Project project) {
project.buildscript{
repositories {
maven: {
url: 'http://localhost:8081/artifactory/zailab-virtual-repo'
credentials: {
username = "admin"
password = "password"
}
}
}
/*Spring Boot Gradle plugin */
dependencies {
classpath: 'org.springframework.boot:spring-boot-gradle-plugin:1.1.6.RELEASE'
}
}
project.apply plugin: 'spring-boot'
project.apply plugin: 'java'
project.apply plugin: 'eclipse'
project.repositories {
maven: {
url: 'http://localhost:8081/artifactory/zailab-virtual-repo'
}
}
project.dependencies {
/*Spring Boot dependencies */
compile: 'org.springframework.boot:spring-boot-starter-test'
compile: 'org.springframework.boot:spring-boot-starter-aop'
compile: 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile: 'org.springframework.boot:spring-boot-starter-integration'
compile: 'org.springframework.boot:spring-boot-starter-amqp'
/*Axon dependencies */
compile: 'org.axonframework:axon-core:2.3.1'
compile: 'org.axonframework:axon-mongo:2.3.1'
}
}
}
I then apply the plugin within another project as below, but it seems the buildscript definitions override/conflict as the 'spring-boot' plugin cannot be found. Am I attempting the impossible or is there perhaps another way to achieve what I am trying to do?
buildscript {
repositories {
maven {
url 'http://localhost:8081/artifactory/zailab-virtual-repo'
credentials {
username = "admin"
password = "password"
}
}
}
dependencies {
classpath(group: 'com.zailab', name: 'zailab-command-service-build', version: '1.0.0- SNAPSHOT')
}
}
apply plugin: 'com.zailab.command.service.project'
Thanks,
Roscoe
As far as I know, it's not possible to add build script dependencies programmatically from a plugin.
Reason for this is build script life cycle - invocation of plugins' apply method happens after the project's classpath configuration had already been resolved.
You should either configure the buildscript in project's build script, or package classpath dependencies with the plugin.

Try to use "GameHelperUtils" package in my Project :Error when building with gradlew.bat : Package does not exist

I'm soo damn desperate,and totaly stuck since 2 DAYS with simply adding 2 Projects to my project and compile a android Debuging version with the console.
I set up the Project with LibGDX gradle and everything worked fine until i addet 2 Projects to the Workspace and used them as librarys :
-BaseGameUtils
-google-play-service
Setting them up in eclipse worked also fine, but compiling a androidDebug.apk with the console gives this error:
error: package com.google.example.games.basegameutils.GameHelper does not exist
import com.google.example.games.basegameutils.GameHelper;
ยด`
My build.gradle looks like this
buildscript {
repositories {
mavenCentral()
mavenLocal()
maven { url 'https://github.com/steffenschaefer/gwt-gradle-plugin/raw/maven-repo/' }
}
dependencies {
classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.4'
classpath 'com.android.tools.build:gradle:0.10+'
classpath 'com.github.jtakakura:gradle-robovm-plugin:0.0.8'
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = 'FlappysSpaceAdventure'
gdxVersion = '1.0.1'
roboVMVersion = '0.0.12'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
compile fileTree(dir: '../libs', include: '*.jar')
}
}
project(":ios") {
apply plugin: "java"
apply plugin: "robovm"
configurations { natives }
dependencies {
compile project(":core")
compile "org.robovm:robovm-rt:${roboVMVersion}"
compile "org.robovm:robovm-cocoatouch:${roboVMVersion}"
compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"
compile fileTree(dir: '../libs', include: '*.jar')
}
}
project(":html") {
apply plugin: "gwt"
apply plugin: "war"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "aurelienribon:tweenengine:6.3.3"
compile "aurelienribon:tweenengine:6.3.3:sources"
}
}
tasks.eclipse.doLast {
delete ".project"
}
and the classpath file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="src" path="/FlappysSpaceAdventure-core"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
<classpathentry combineaccessrules="false" kind="src" path="/BaseGameUtils"/>
<classpathentry combineaccessrules="false" kind="src" path="/google-play-services_lib"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
I really don't know what to do, i feel so stupid...i cant understand why this is happening.

Eclipse plugin development: error logging in log4j to error view

I'm trying to make my logging from log4j to go to the Eclipse Error Log view in my plugin.
I've got two external bundles:
Bundle 1: created a Plugin Development project based on existing JARs, containing the log4j library.
Bundle 2: created an empty Plugin Development project. Added a log4j.properties to it and a new class "VirtualConsole", which extends "ConsoleAppender".
This is how my log4j.properties looks like:
# Set root logger level to debug and its only appender to default.
log4j.rootLogger=debug, default
# default is set to be a ConsoleAppender.
log4j.appender.default=VirtualConsole
# default uses PatternLayout.
log4j.appender.default.layout=org.apache.log4j.PatternLayout
log4j.appender.default.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
And this is how my VirtualConsole looks like:
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.statushandlers.StatusManager;
public class VirtualConsole extends ConsoleAppender {
#Override
public void append(LoggingEvent event) {
int level = IStatus.INFO;
if (event.getLevel().equals(Level.ERROR))
level = IStatus.ERROR;
IStatus status = new Status(level, "myplugin",
event.getMessage().toString());
StatusManager.getManager().handle(status, StatusManager.LOG);
//and the normal logging
super.append(event);
}
}
This works. I've got my first bundle included as a dependency in the other bundles and when I run my plugin from within Eclipse by "right click > run as > Eclipse Application", I can run my plugin and when I (purposedly) do something where I now it is logging, the logs appear where they should - in the Error Log view.
As soon as I export my plugin, run a clean and fresh install of Eclipse and install the plugin via the "install software" system, it doesn't work anymore... nothing appears anywhere. I cannot get it to work and I honestly can't see where I'm going wrong.
Anyone able to give me some pointers?
Thanks.
Update So this is how everything looks like now:
Manifest.MF from the bundle "bundle.log4jProperties":
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Log4jProperties
Fragment-Host: bundle.slf4j
Bundle-SymbolicName: bundle.log4jProperties
Bundle-Version: 1.0.1
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.7.0",
bundle.slf4j
Eclipse-RegisterBuddy: bundle.slf4j
log4j.properties file in the bundle "bundle.log4jProperties":
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
log4j.logger.org.hibernate.type=ERROR
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=bundle.log4jProperties.ErrorLogAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
ErrorLogAppender.java in the bundle "bundle.log4jProperties" (in a package "bundle.log4jProperties" as well, in the src folder):
package bundle.log4jProperties;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
public class ErrorLogAppender extends AppenderSkeleton {
#Override
public void close() {}
#Override
public boolean requiresLayout() {
return false;
}
#Override
protected void append(LoggingEvent event) {
//get the platform log
ILog log = Platform.getLog(Platform.getBundle
("bundle.log4jProperties"));
//create an IStatus status
IStatus status = new Status(getLevel(event.getLevel()),
"myPlugin",
getCode(event),
getMessage(event),
getThrowable(event));
//log the status
log.log(status);
}
private int getLevel(Level level) {
int severity;
if (level.equals(Level.ALL) ||
level.equals(Level.ERROR) ||
level.equals(Level.FATAL))
severity = IStatus.ERROR;
else if (level.equals(Level.WARN))
severity = IStatus.WARNING;
else if (level.equals(Level.INFO))
severity = IStatus.INFO;
else severity = IStatus.INFO;
return severity;
}
private int getCode(LoggingEvent event) {
return (int) event.getTimeStamp();
}
private String getMessage(LoggingEvent event) {
return event.getMessage().toString();
}
private Throwable getThrowable(LoggingEvent event) {
ThrowableInformation info = event.getThrowableInformation();
if (info != null)
return info.getThrowable();
else return null;
}
}
Over to the bundle "bundle.slf4j", which contains 3 jar files: Manifest.MF:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Slf4j
Bundle-SymbolicName: bundle.slf4j
Bundle-Version: 1.0.1
Bundle-ClassPath: slf4j-api-1.7.5.jar,
slf4j-log4j12-1.7.5.jar,
log4j-1.2.17.jar
Bundle-Vendor: apache
Export-Package: org.apache.log4j,
org.apache.log4j.chainsaw,
org.apache.log4j.config,
org.apache.log4j.helpers,
org.apache.log4j.jdbc,
org.apache.log4j.jmx,
org.apache.log4j.lf5,
org.apache.log4j.lf5.util,
org.apache.log4j.lf5.viewer,
org.apache.log4j.lf5.viewer.categoryexplorer,
org.apache.log4j.lf5.viewer.configure,
org.apache.log4j.net,
org.apache.log4j.nt,
org.apache.log4j.or,
org.apache.log4j.or.jms,
org.apache.log4j.or.sax,
org.apache.log4j.pattern,
org.apache.log4j.rewrite,
org.apache.log4j.spi,
org.apache.log4j.varia,
org.apache.log4j.xml,
org.slf4j,
org.slf4j.helpers,
org.slf4j.impl,
org.slf4j.spi
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Eclipse-BuddyPolicy: registered
Still all working when running from inside Eclipse as an Eclipse application, but not working when deploying to an update site and installing from there. I reckon this might have to do with the properties file not being found, but what do I have to change if I want the file to be found??
Note: feature.xml, which I'm exporting to the updatesite, contains this as well (truncated and left out what's not necessary):
<requires>
<import plugin="bundle.slf4j" version="1.0.1" match="greaterOrEqual"/>
</requires>
<plugin
id="bundle.slf4j"
download-size="0"
install-size="0"
version="0.0.0"/>
<plugin
id="bundle.log4jProperties"
download-size="0"
install-size="0"
version="0.0.0"
fragment="true"
unpack="false"/>
I couldn't add the bundle.log4jProperties to the required plugins as that gives me the warning that that plugin could not be resolved. Somehow I think this is related to my problem but how on earth am I supposed to make my feature.xml (in the bundle "feature", separate) "resolve" another bundle which is in the same workspace and working? Especially weird because that same feature.xml does revolve the plugin in the plugin-statement.
Please, anyone. Help.
It's pretty fantastic that I'm struggling with something that's done in almost every plugin out there, but I can't manage to find a solution anywhere. Bottom line: I want to log errors to the error log view. That's gotta be possible, right?

how to copy the dependencies libraries JARs in gradle

I got a runnable jar with this build.gradle
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes("Main-Class" : "com.test.HelloWorld")
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
but it run failed, because the dependencies jars can't find.
and then I add this code:
task copyToLib(type: Copy) {
into "$buildDir/output/libs"
from configurations.runtime
}
but nothing change. I can't find the folder output/libs.
how can I copy the dependencies libs jars to a specified folder or path?
Add:
build.dependsOn(copyToLib)
When gradle build runs, Gradle builds tasks and whatever tasks depend on it (declared by dependsOn). Without setting build.dependsOn(copyToLib), Gradle will not associate the copy task with the build task.
So:
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes('Main-Class': 'com.test.HelloWorld')
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
task copyToLib(type: Copy) {
into "${buildDir}/output/libs"
from configurations.runtime
}
build.dependsOn(copyToLib)
I find the application plugin way too cumbersome and too verbose in its output. Here's how I finally got a setup I was happy with, i.e., create a distribution zip file with dependency jars in subdirectory /lib and add all dependencies to Class-Path entry in the manifest file:
apply plugin: 'java'
apply plugin: 'java-library-distribution'
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.commons:commons-lang3:3.3.2'
}
// Task "distZip" added by plugin "java-library-distribution":
distZip.shouldRunAfter(build)
jar {
// Keep jar clean:
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
manifest {
attributes 'Main-Class': 'com.somepackage.MainClass',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
}
// How-to add class path:
// http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle
// https://gist.github.com/simon04/6865179
}
Hosted as a gist here.
The result can be found in build/distributions and the unzipped contents look like this:
lib/commons-lang3-3.3.2.jar
MyJarFile.jar
Contents of MyJarFile.jar#META-INF/MANIFEST.mf:
Manifest-Version: 1.0
Main-Class: com.somepackage.MainClass
Class-Path: lib/commons-lang3-3.3.2.jar
Since Gradle 6.0 it is:
tasks {
val deps by registering(Copy::class) {
from(configurations.runtimeClasspath)
into("build/deps")
}
}
The problem with all the previous answers is that they only collect dependencies from one configuration. To get ALL of the dependencies, you should use this:
task saveDependencies(type: Copy){
configurations.each {
if (it.isCanBeResolved())
from it into "gradle_dependencies"
}
from buildscript.configurations.classpath into "gradle_dependencies"
}
The application plugin requires you to set the main class name like this:
mainClassName = "com.test.HelloWorld"
You will need to add that to your build script. Keep in mind that if you try to run your application with the java command you will also need to set the classpath with -cp.
The application plugin simplifies this process by providing the task distZip. If you run that task you a full distribution is created for you under build/distributions. The distribution contains start scripts and all dependencies. The generated start scripts already set the classpath for you so you don't have to deal with it anymore.
The java plugin can pack a jar with dependencies and there's no need for the application plugin. A task like the following would do:
task buildWithDeps(type: Jar) {
manifest {
attributes "Main-Class": "com.test.HelloWorld"
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
As of at least Gradle 5.6.4 you'll want to do something closer to this.
dependencies {
implementation 'my.group1:my-module1:0.0.1'
implementation 'my.group2:my-module2:0.0.1'
}
jar {
from {
configurations.compileClasspath.filter { it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
}
}
For Gradle 7.4 with Groovy:
configurations {
externalLib.extendsFrom(implementation)
}
task copyLibs(type: Copy){
from configurations.externalLib{
into '<dest-dir-name>'
exclude('<if any jars need to be excluded>')
}
}