build.gradle for eclipse project with multiple source folders - java

I have a eclipse project with source folders like:
/src/main/java/module1
/src/main/java/module2
/src/main/java/module3
inside these src folders i have packages like:
com.example.module1.xxx
com.example.module2.yyy
com.example.module3.zzz
I have made a gradle build using java and eclipse plugin with sourcesets like:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceSets {
main {
java {
srcDirs = ['src/main/java/module1']
srcDir 'src/main/resources'
srcDir 'src/main/java/module2'
srcDir 'src/main/java/module3'
}
}
}
jar{
manifest {
attributes 'Implementation-Title': 'Example Project',
'Implementation-Version': '1.0.0'
}
}
repositories {
mavenCentral()
}
dependencies{
compile group: 'com.zaxxer', name: 'HikariCP', version: '2.2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4j2Version
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4j2Version
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: log4j2Version
compile group: 'org.apache.logging.log4j', name: 'log4j-jul', version: log4j2Version
compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: log4j2Version
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.9'
}
The problem is when i generate eclipse project using gradle eclipse it correctly generates the 3 module source folders, a resources source folder but it also generates the super source folder src/main/java having all the nested source folders with packages like:
module1.com.exaple.module1.xxx
module2.com.exaple.module2.yyy
module3.com.exaple.module3.zzz
Thus i get error in eclipse "Cannot nest '${projectdir}/src/main/java/module1' inside '${projectdir}/src/main/java'. To enable the nesting exclude 'module1/' from '${projectdir}/src/main/java'"
How do i modify my tasks/sourcesets to get the right project structure?

Well finally figured out that the build.gradle is fine... what i was doing wrong is i was executing command gradle eclipse which was just appending entries into the existing .classpath file, To generate a fresh copy i had to execute gradle cleanEclipseClasspath eclipse which produced the correct .classpath file.

Related

Building JAR file with IntelliJ, OpenJFX and Gradle

What is in 2021 the best way to create an executable jar file with Gradle in IntelliJ using OpenJFX? Every solution I found is very complex and uses workarounds. This is my current build.gradle file
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
repositories {
mavenCentral()
}
javafx {
version = "15.0.1"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
mainClassName = 'com.company.crawl.main.Main'
group 'com.company'
version '1.0-SNAPSHOT'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '9.2.0.jre15'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
compile group: 'org.apache.commons', name: 'commons-csv', version: '1.8'
}
jar {
from configurations.compile.collect {zipTree it}
manifest.attributes "Main-Class": "com.company.crawl.main.Main"
}
If I run gradle :jar, the jar file includes the dependencies but not the openjfx sdk. What do I have to do to run the jar using the openjfx sdk?
UPDATE 1
changed the jar path to this:
jar {
manifest {
attributes 'Main-Class': 'com.company.crawl.main.Main'
}
from {
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
}
after rebuilding the jar, I get the error: "JavaFX runtime components are missing". I don't know why because in the jar file the javafx folders are included. After that I added the VM options:
--module-path /path/to/javafx-sdk/lib --add-modules=javafx.controls,javafx.fxml
and the jar file runs. So what am I doing wrong?

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"
])
}

ClassNotFoundException: /org/jdom2/JDOMException thrown after the project is compiled

Edit: This question is a duplicate to this question, which got incorrectly marked as duplicate, forcing me to restate my question.
To preface this, yes, I have seen this thread, but it did not help me. Specificly, while multiple people mentioned that different class structures between the building branch and the environment branch can exists, noone said anything about how to test for that, let alone fix it. And even then, I am not certain that this is really root cause.
I am working on a gradle project and I wanted to compile the project I had, to test it outside of my IDE, Netbeans 8.2, in which it works fine.
When I try to run the program with the usual java -jar command, it initally works fine, until the programs comes across a class that can throw a JDOMException. Then I get the error described in the title, followed by a NoClassDefFoundError.
In Netbeans, the JDOMException.class is included under Dependencies>Compile for main>jdom2-2.0.6.jar>org.jdom2
And the build.gradle script reads:
apply plugin: 'java'
sourceCompatibility = 1.8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty('mainClass')) {
ext.mainClass = 'bikesys.hda.BikeSys'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile files('libs/jcalendar-1.4.jar')
compile group: 'org.jdom', name: 'jdom2', version: '2.0.6'
compile ("com.byteowls:jopencage:1.2.0")
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
compile group: 'org.slf4j', name:'slf4j-api', version: '1.7.2'
compile group: 'ch.qos.logback', name:'logback-classic', version: '1.0.9'
compile group: 'ch.qos.logback', name:'logback-core', version: '1.0.9'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
compile group: 'org.json', name: 'json', version: '20180813'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'me.atlis', name: 'atlis-location-base', version: '1.0.0'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
compile group: 'me.atlis', name: 'nominatim-api', version: '1.0.0'
}
jar {
manifest {
attributes(
'Main-Class': 'bikesys.hda.BikeSys'
)
}
}
I am using java jdk 1.8.0_202 in Netbeans and jre 1.8.0_211-b12 as runtime environment. I have found answers along the lines of "You have to add jdom2 to your runtime environment", however I neither know how to do that, nor if that is a general solution to the problem.
I'd appreciate the help!
The problem was that, by default, gradle does not include external dependencies when building a normal .jar file. I had to instruct gradle to build a "fat jar" file instead.
I did this by altering the
jar {
...
}
part of my gradle script into the following instruction:
jar {
manifest {
attributes(
'Main-Class': 'bikesys.hda.BikeSys'
)
}
from {
configurations.compileClasspath.filter{ it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Be sure to edit the class path, which in my case was bikesys.hda.BikeSys, to your main class.

SpringBoot test module doesn't search log4j2 configuration in the right classpath

I'm having some problems running tests in my SpringBoot project.
The project-structure is the following:
Project Structure Image
I can start the resourceService without issues, but if i even try to run the standard test of SpringBoot projects.....
package com.pcsystem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class ResourceserviceApplicationTests {
#Test
public void contextLoads() {
}
}
the program respond with this error:
Logging system failed to initialize using configuration from 'resourceservice/log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\resourceservice\log4j2.xml (Unable to find the specified classpath)
So i tried to change the application.properties specific property from
logging.config=resourceservice/log4j2.xml
to
logging.config=log4j2.xml
After the change i've noticed that resourceserviceApplication will not start because it can't find the log4j2.xml:
Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml (Unable to find the specified classpath)
I've tried to resolve in many ways and doing a lot of researches but at the moment i'm still stuck here.
Any idea?
ps: it seems that the Authorizationservice module doesn't suffer the same problem, but simply because i haven't set the logging.config property in Authorizationservice's application.properties (there is no need for now)
Thanks in advance and have a great day.
-UPDATE 1-
Configuration file is about all the resourceService module, so i've done as you said Kostiantyn ( thanks for you response ) but the problem still persist.
Actual situation:
Project structure after your reply
Now resourceServiceApplication won't start , saying :
Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml
and the contextLoads() method from the test package says:
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\log4j2.xml
Let me show you configuration file:
server.port=8888
logging.config=log4j2.xml
spring.data.mongodb.host=localhost
spring.data.mongodb.database=jogging
#spring.data.mongodb.username=admin
#spring.data.mongodb.password=pass
spring.data.mongodb.port=27017
As requested from user1615664 , below you can see my gradle file
(that's the gradle file about the resourceService module;
AuthorizationService have one specific gradle file and lastly there is
a root gradle file, that i will show to you at the end of this update)
Exscuse me for the lenght, i'm using a large number of libraries here.
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
compile.exclude group:'ch.qos.logback'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude module: "logback-classic"
}
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile group: 'org.apache.tika', name: 'tika', version: '1.16', ext: 'pom'
compile group: 'org.apache.tika', name: 'tika-parsers', version: '1.16'
compileOnly('org.projectlombok:lombok')
compile("org.springframework.boot:spring-boot-starter-actuator")
//compile project(':authorizationservice')
// https://mvnrepository.com/artifact/org.springframework.hateoas/spring-hateoas
compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version: '0.23.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.1.1.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.8.RELEASE'
// https://mvnrepository.com/artifact/org.apache.axis/axis
compile group: 'org.apache.axis', name: 'axis', version: '1.4'
// https://mvnrepository.com/artifact/axis/axis-jaxrpc
compile group: 'axis', name: 'axis-jaxrpc', version: '1.4'
// https://mvnrepository.com/artifact/commons-discovery/commons-discovery
compile group: 'commons-discovery', name: 'commons-discovery', version: '0.5'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
// https://mvnrepository.com/artifact/javax.xml/jaxrpc-api
compile group: 'javax.xml', name: 'jaxrpc-api', version: '1.1.1'
// https://mvnrepository.com/artifact/org.apache.xmlrpc/xmlrpc
compile group: 'org.apache.xmlrpc', name: 'xmlrpc', version: '3.1.3', ext: 'pom'
// https://mvnrepository.com/artifact/javax.activation/activation
compile group: 'javax.activation', name: 'activation', version: '1.1.1'
// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
// https://mvnrepository.com/artifact/wsdl4j/wsdl4j
compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.3'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
PS: maybe is worthless , but in the root module ( baseProjectOauth2 )
we can admire this root gradle file
group 'com.pcsystem'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
You've missing src/test/resources folder in your project structure, and looks like current location of log4j2.xml in the project root is not under classpath at all
Rely on Gradle project convention and place your config files accordingly, i.e. create either src/test/resources and put file in there, if your logging configuration is test specific:
src
test
java
...
resources
log4j2.xml
Or, if logging configuration inside your log4j2.xml will be used by your application during live run (included into .jar deliverable), move this file to src/main/resources instead
that will bring your logging.config=log4j2.xml working.
Further reading about Gradle project structure & resources folders is here
I feel greedy replying to my own question, but i think i've solved.
I've done these things:
-> created resource folder under test module
src
main
test
java
resources
application.properties
In that properties file i've only put this setup
logging.config=log4j2.xml
And all i have to say now is
"explicative image here"
Simply i have to deep study spring structure (and even gradle one) about auto detect of properties files
You can try to put value classpath:log4j2.xml instead of just log4j2.xml into your application.properties (or application.y[a]ml) file.
All the resources, including this file, should be placed inside the src/main/resources (where application.properties file is also placed). Regardless of a few possible exceptions.
If you want a different configuration of logging for tests, than you should put adapted file inside of tests' resources directory (src/test/resources), but if you want these to be the same, then you can leave it as is (inside src/man/resources only)

Gradle distZip - adds excluded dependency

My project uses
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
I added Google Sheets API to dependencies. Now
gradle dependencies says Google Sheets API uses org.mortbay.jetty:servlet-api:2.5-20081211 which I assume to exclude, because classes are loaded from the wrong jar:
compile (group: 'com.google.apis', name: 'google-api-services-sheets', version: 'v4-rev25-1.22.0') {
exclude(module: 'servlet-api')
}
compile (group: 'com.google.gdata', name: 'core', version: '1.47.1') {
exclude(module: 'servlet-api')
}
Now gradle dependencies does not show servlet-api:2.5 to be used anywhere. But after gradle clean distZip I see that archive contains servlet-api-2.5-20081211.jar! How can I exclude it from the build?

Categories

Resources