args4J doesn't work with gradle - java

I'm totally new to gradle so it may be a obvious problem:
I'm using eclipse with gradle and I actually have no problem adding dependencies for junit or stuff, it adds the junit lib to the gradle dependencies and there's no problem using junit, but if i try to use args4j (also with adding the dependency) it just doesn't work.
Just to make sure there's no problem with the build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'title',
'Implementation-Version': version,
'Main-Class':'path.to.main.Main'
}
}
repositories {
mavenCentral()
}
test {
systemProperties 'property': 'value'
}
dependencies{
compile 'args4j:args4j-site:2.0.25'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
and No I'm not using title or path.to.main ^^
Eclipse shows me that the import (args4j) cannot be resolved

You forgot main "args4j" module:
compile group: 'args4j', name: 'args4j', version: '2.0.25'
compile group: 'args4j', name: 'args4j-site', version: '2.0.25'

With gradle-7.1 this works for me:
// build.gradle
repositories {
mavenCentral()
}
dependencies {
implementation group: 'args4j', name: 'args4j', version: '2.33'
implementation group: 'args4j', name: 'args4j-site', version: '2.33'
}

Related

package sun.net.www.protocol.https is declared in module java.base, which does not export it

I am trying to build a java project using gradle6.6 and open jre 11.0.2, in eclipse 03-2020.
I am facing compilation issue, while building my project which uses the classes from jrt-fs.jar; e.g.'sun.net.www.protocol.http.Handler', which is in open jre 11.
public class Manager extends sun.net.www.protocol.https.Handler{
(package sun.net.www.protocol.https is declared in module java.base, which
does not export it)
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
Following is my build.gradle:
apply plugin: 'java'
apply plugin: 'application'
allprojects {
apply plugin: 'java'
sourceCompatibility = '11.0.2'
targetCompatibility = JavaVersion.VERSION_11
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'lib', include: '*.jar')
compile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
compile group: 'junit', name: 'junit', version: '4.10'
compile group: 'jmock', name: 'jmock', version: '1.0.1'
compile group: 'com.google.code.findbugs',name: 'findbugs', version: '1.3.9'
compile group: 'cglib', name: 'cglib', version: '2.1'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
also, how can I avoid building the test classes, through gradle?
As #dave_thompson_085 suggested, Issue is due to modular restriction (JDK-1.9 onwards) on sun.net.* packages , as they are internal to JDK. The above packages are accessible in JDK -1.8. We need to fix our code
If you are using gradle, add this compilerArgs in build.gradle
options.encoding = "UTF-8"
options.incremental = true
options.compilerArgs.addAll([
"--add-exports",
"java.base/sun.net.www.protocol.http=ALL-UNNAMED"
])
}

Gradle Spring Boot plugin broken on Linux

I have a multimodule gradle build with a single module containing a spring boot application that consumes the artifacts of the other modules. I have the spring boot plugin applied to the single spring boot module.
Everything works correctly in Windows, but once I try the build on a Linux machine if fails. I have tried multiple linux machines (Jenkins server, Ubuntu VM, even bitbucket pipeline) and it fails with this error:
Problems reading data from Binary store in /tmp/gradle2075404181876889604.bin (exist: false)
I am not entirely sure what gradle uses this binary store for, but after debugging I found that this file name is different than binary store that every other module is using while building.
I have tried multiple gradle versions(2, 3, and 4) and several spring boot versions and they all fail with the same error. The only thing that fixes this is removing the spring boot plugin.
Here is the parent build.gradle
repositories {
mavenCentral()
}
def javaLangVersion = '1.8'
def gradleDir = "${rootProject.rootDir}/gradle"
def realProjects = allprojects - [project(':external'), project(':delivery')]
def javaProjects = realProjects - rootProject
def integrationTestProjects = javaProjects
configure(realProjects) {
group 'com.packagename'
version '1.0-SNAPSHOT'
apply plugin: 'eclipse'
apply plugin: 'idea'
}
configure(javaProjects) {
apply plugin: 'java'
targetCompatibility = javaLangVersion
sourceCompatibility = javaLangVersion
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jVersion}"
compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: "${slf4jVersion}"
testCompile group: 'junit', name: 'junit', version: "${junitVersion}"
testCompile group: 'org.mockito', name: 'mockito-core', version: "${mockitoVersion}"
testCompile group: 'nl.jqno.equalsverifier', name: 'equalsverifier', version: "${equalsverifierVersion}"
}
}
configure(integrationTestProjects) {
apply from: "${gradleDir}/integrationTest.gradle"
}
task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}
Here is the spring boot module build.gradle file:
buildscript {
ext {
springBootVersion = '1.5.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootStarterVersion}")
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
war {
baseName = "${project.name}"
version = "${project.version}"
}
dependencies {
compile project(':module1')
compile project(':module2')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: "${httpCoreVersion}"
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: "${httpClientVersion}"
compile group: 'com.h2database', name: 'h2', version: "${h2Version}"
testCompile project(':test-utils')
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}
All other modules are plain java modules and only declare a dependencies block in the build file.
I have been working on this for 2 days now so any help or suggestions would be greatly appreciated. Thanks!
Well this is super embarrasing... I had a unit test using FileUtils.getTempDirectory() and then I was cleaning that directory using FileUtils.deleteQuietly after the test and it was deleting the gradle binary store that was in the /tmp folder. The reason it wasnt showing on windows is because windows locks files.
did you verify that your gradle.properties is OK on the Linux machine ?

Define compile dependencies in gradle that dont get packaged

I'm trying to configure gradle to use lombok to compile my project but I don't want the classes appearing in my jar. On the other side I need the mysql-connector dependency packages in the jar, but it's not needed for compiling. This is my build.gradle file:
group 'de.albritter'
version '1.0-SNAPSHOT'
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'de.albritter.main.Main'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
classpath group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
}
}
I've seen some solutions using compileOnly but if i try to use it I just gete an error that this method is not known.
My gradle version is 2.9
How do I tell gradle that I don't need lombok in my jar?
What you're asking for is variously known as compileOnly or in the maven world, provided dependency. The compileOnly configuration was introduced in gradle in version 2.12. I would strongly recommend moving to the latest version of gradle (2.14 at the time of writing this).
If you need to stick to the older version, there are some workarounds you can find by looking for "gradle provided dependency". One way to do this is to declare your own configuration, lets call it provided and adding its dependencies to compile time classpath. So in your build.gradle:
configurations{
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
dependencies {
...
provided 'group:module:version'
...
}
Or alternatively you can use the prodeps plugin which does most of this work for you.

gradle test classnotfoundexception for compileOnly dependency

I have a war project, and I am trying to run my unit tests. Unfortunately, I am receiving this ClassNotFoundException for a class that is a 'compileOnly' dependency. I have spent hours trying to add this class to the test classpath, without success. Any help is appreciated.
The Gradle build file is below:
buildscript {
repositories {
mavenCentral()
maven {
name = 'Sonatype Nexus Snapshots'
url = 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:1.1-SNAPSHOT'
}
}
plugins {
id 'eclipse-wtp'
id 'war'
id 'org.unbroken-dome.test-sets' version '1.2.0'
}
apply plugin: 'liberty'
compileJava.options.fork = true
compileJava.options.forkOptions.executable = project.property('JDKPath')
// 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()
flatDir {
dirs 'WebContent/WEB-INF/lib'
}
}
configurations {
testCompile.extendsFrom compileOnly
testRuntime.extendsFrom compileOnly
}
testSets {
integrationTest
endToEndTest
}
dependencies {
// Libraries that will be provided by WebSphere.
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
compileOnly group: 'com.ibm.websphere.appserver.api', name: 'com.ibm.websphere.appserver.api.json', version: '1.0.12'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.jayway.restassured', name: 'rest-assured', version: '2.9.0'
}
sourceSets {
test {
compileClasspath += [configurations.compileOnly]
runtimeClasspath += [configurations.compileOnly]
}
}
I had to dig deep into the exception stack, and when I did, I found that I was missing a dependency.

How to configure Quasar in Gradle

I'm new to Gradle and I don't know what to do.
Here is Quasar docs about how to install Quasar through Gradle: Quasar Docs
There is also a template project in the page: Template Gradle Project
Finally this is my build.gradle:
group 'TGAdminsBot'
version '0.1'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
mainClassName = "Launcher"
idea {
module
{
downloadJavadoc = true
downloadSources = true
}
}
dependencies {
compile 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.4'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.4'
//compile 'com.github.User:Repo:Tag'
//compile 'com.mashape.unirest:unirest-java:1.4.9'
compile 'co.paralleluniverse:comsat-httpclient:0.7.0'
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.2.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
configurations {
quasar
}
task runQuasar {
jvmArgs "-javaagent:${configurations.quasar.iterator().next()}"
}
run.dependsOn runQuasar
And I get this Error:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\Sobhan\Documents\IntelliJIDEAProjects\TGAdminsBot\build.gradle' line: 39
* What went wrong:
A problem occurred evaluating root project 'TGAdminsBot'.
> java.util.NoSuchElementException (no error message)
So What should I do? I'm again sorry to ask this question but I'm new to Gradle and I Googled so much before posting this question. Thanks
There were three problems.
configurations were defined before dependencies.
Two lines were needed in dependencies:
compile 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
quasar 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
Lack of this block:
tasks.withType(JavaExec){
jvmArgs "-javaagent:${configurations.quasar.iterator().next()}"
}
Finally this is final build.gradle:
group 'TGAdminsBot'
version '0.1'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
idea {
module
{
downloadJavadoc = true
downloadSources = true
}
}
configurations {
quasar
}
dependencies {
compile 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
quasar 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.4'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.4'
compile 'co.paralleluniverse:comsat-httpclient:0.7.0'
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.2.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
tasks.withType(JavaExec)
{
jvmArgs "-javaagent:${configurations.quasar.iterator().next()}"
}
task run(type: JavaExec) {
main = 'com.sunova.bot.Launcher'
classpath = sourceSets.main.runtimeClasspath
}
I think your problem lies mostly in the definition of runQuasar which is not a run task and thus has no jvmArgs property but, if you don't need it for other reasons I'm unaware of, just do as in the Gradle template project (agent configuration) rather than defining runQuasar and declaring that run depends on it:
applicationDefaultJvmArgs = [
"-javaagent:${configurations.quasar.singleFile}" // =v, =d
]
If you need a separate runQuasar I think you'll need to declare it as a JavaExec task (have a look here).

Categories

Resources