Refreshing gradle in IntelliJ causes source folder structure to change - java

When I run refresh gradle in IntelliJ IDEA, my main folder is set to be the source root, but "java", which is my actual source root, is unmarked.
I have to change this manually every time after I do gradle refresh.
Do you know what the relevant gradle setting is?
Can it be in a common gradle file?
Should I change the main folder to be the source root?
How can I know where is the common gradle I might inherit from?
How can I override this in my local build.gradle?
My build.gradle:
apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
//
// configurations.all*.exclude(group: 'com.sun.jersey', module: 'jersey-bundle')
// configurations.all*.exclude(group: 'com.fasterxml.jackson.core', module:'jackson-databind')
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'com.google.inject:guice:4.0-beta5'
compile 'com.sun.jersey:jersey-core:1.18.3'
compile 'com.sun.jersey:jersey-client:1.18.3'
compile 'com.sun.jersey:jersey-json:1.18.3'
compile 'com.sun.jersey.contribs:jersey-apache-client:1.18.3'
compile group: 'junit', name: 'junit', version: '4.11'
compile 'com.vividsolutions:jts:1.13'
compile 'net.sf.opencsv:opencsv:2.3'
compile 'com.googlecode.json-simple:json-simple:1.1'
compile 'com.google.guava:guava:18.0'
//testCompile group: 'junit', name: 'junit', version: '4.11'
}
sourceSets {
main {
java {
srcDirs = ['src/main']
}
}
test {
java {
srcDirs = ['src/main']
}
}
}
test {
testLogging {
// Show that tests are run in the command-line output
events 'started', 'passed'
}
}
task run_BaselineGenerator(type: JavaExec) {
classpath sourceSets.main.runtimeClasspath
main = "com.waze.routing.automation.runners.BaselineGenerator"
}

Your sourceSets look incorrect to me. I would try:
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
}
test {
java {
srcDirs = ['src/test/java']
}
}
}

Related

How to 'gradle build'?

Could not find method application() for arguments
i tried gradle build but got this error
then i want to build the OneDrive client
i do not want to GET making a returnurl or get it by other means.
plugins {
id 'application'
}
application {
mainClassName = 'com.mycompany.app.App'
}
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src'
}
}
dependencies {
compile 'org.apache.httpcomponents:httpclient:4.5.10'
compile 'org.apache.httpcomponents:httpcore:4.4.12'
compile group: 'org.apache.httpcomponents', name: 'httpclient',
version: '4.5.10'
compile group: 'org.apache.httpcomponents', name: 'httpcore',
version: '4.4.12'
}
repositories {
jcenter()
maven {
url "https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient"
}
maven {
url

Gradle jar does not include Guava Preconditions

I have the following build.gradle file for my project
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:23.6-jre';
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
manifest {
attributes 'Main-Class': 'Runner.ClientRunner'
}
}
However, when I run "gradle jar" and attempt to run the given jar, I get the error:
java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
I can't seem to nail down what I've done wrong here, guava is included in the dependencies for gradle and the jar file appears to build fine otherwise (it only crashes when it gets to the first class that depends on guava). Any assistance appreciated.
If it helps I'm doing this from IntelliJ.
I solved my problem using the solution at https://discuss.gradle.org/t/how-to-include-dependencies-in-jar/19571
My build.gradle now looks like
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
// configuration that holds jars to include in the jar
extraLibs
}
dependencies {
compile 'com.google.guava:guava:23.6-jre';
extraLibs group: 'com.google.guava', name: 'guava', version: '23.6-jre'
testCompile group: 'junit', name: 'junit', version: '4.12'
configurations.compile.extendsFrom(configurations.extraLibs)
}
jar {
manifest {
attributes 'Main-Class': 'Runner.ClientRunner'
}
from {
configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
}

Java - Gradle jar created by FatJar task won't work

I've been looking for a solution for this for 4 days now with no success,
first doing the normal Gradle build task produced a tiny 7kB jar files which obviously didn't work, then i added the jar configuration for the main class:
jar {
manifest {
attributes 'Main-Class': 'Main'
}
}
It's 2MB now!
Still not good enough,
then i found the fatJar/uberJar/shadow tasks, said to be synonymous to the same thing so i tried clean then fatJar tasks on another small RMI app that will run on the server and it worked great, copied it to the main app, it produces and 60MB jar file which looks good but it won't work for some reason,
The app runs perfectly from my IDE (IntelliJIDEA) but the jar produced just does nothing.
So my last option is to consult experts so here we go:
This's the result of the clean task:
6:11:10 PM: Executing external task 'clean'...
:clean
BUILD SUCCESSFUL
Total time: 0.774 secs
6:11:11 PM: External task execution finished
'clean'.
This's the result of the fatJar task:
6:13:13 PM: Executing external task 'fatJar'...
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:compileJava
:processResources
:classes
:fatJar
BUILD SUCCESSFUL
Total time: 24.831 secs
6:13:37 PM: External task execution finished 'fatJar'.
This's my build.gradle file
group 'Yasmeena'
version '1.0-SNAPSHOT'
apply plugin: 'java'
compileJava.options.encoding = 'UTF-8'
sourceCompatibility = 1.8
jar {
manifest {
attributes 'Main-Class': 'Main'
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar',
'Implementation-Version': version,
'Main-Class': 'Main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.mchange:c3p0:0.9.5.2'
compile 'commons-beanutils:commons-beanutils:1.9.3'
// compile 'org.apache.commons:commons-collections3:2.2-SNAPSHOT'
compile 'commons-digester:commons-digester:2.1'
compile 'commons-logging:commons-logging:1.2'
compile 'commons-validator:commons-validator:1.6'
compile 'org.controlsfx:controlsfx:8.40.13'
compile 'com.jfoenix:jfoenix:1.7.0'
compile 'com.mchange:mchange-commons-java:0.2.11'
compile 'com.github.PlusHaze:TrayNotification:-SNAPSHOT'
compile 'com.google.firebase:firebase-admin:5.3.0'
compile 'org.jetbrains:annotations:15.0'
compile 'org.slf4j:slf4j-simple:1.7.25'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
compile group: 'com.impossibl.pgjdbc-ng', name: 'pgjdbc-ng', version: '0.6'
compile group: 'org.eclipse.jdt.core.compiler', name: 'ecj', version: '4.6.1'
compile group: 'de.jensd', name: 'fontawesomefx', version: '8.9'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
compile group: 'com.lowagie', name: 'itext', version: '2.1.7'
compile group: 'org.olap4j', name: 'olap4j', version: '1.2.0'
compile fileTree(dir: 'lib', include: 'ReportUtilities.jar')
compile('net.sf.jasperreports:jasperreports:6.4.1') {
exclude group: 'com.itextpdf'
}
runtime 'com.itextpdf:itextpdf:5.5.0'
runtime 'com.itextpdf:itext-pdfa:5.5.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
my Main.java class which holds my main method exists directly in the main -> java directory
This's my settings.gradle file:
rootProject.name = 'Yasmeena'
My main method is a simple method that launches the JavaFX application:
public static void main(String[] args) {
launch(args);
}
This's my project structure
UPDATE:
Current state of build.gradle file:
group 'Yasmeena'
version '1.0-SNAPSHOT'
apply plugin: 'java'
compileJava.options.encoding = 'UTF-8'
sourceCompatibility = 1.8
jar {
manifest {
attributes 'Main-Class': 'activities.Main'
}
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.mchange:c3p0:0.9.5.2'
compile 'commons-beanutils:commons-beanutils:1.9.3'
compile 'commons-digester:commons-digester:2.1'
compile 'commons-logging:commons-logging:1.2'
compile 'commons-validator:commons-validator:1.6'
compile 'org.controlsfx:controlsfx:8.40.13'
compile 'com.jfoenix:jfoenix:1.7.0'
compile 'com.mchange:mchange-commons-java:0.2.11'
compile 'com.github.PlusHaze:TrayNotification:-SNAPSHOT'
compile 'com.google.firebase:firebase-admin:5.3.0'
compile 'org.jetbrains:annotations:15.0'
compile 'org.slf4j:slf4j-simple:1.7.25'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
compile group: 'com.impossibl.pgjdbc-ng', name: 'pgjdbc-ng', version: '0.6'
compile group: 'org.eclipse.jdt.core.compiler', name: 'ecj', version: '4.6.1'
compile group: 'de.jensd', name: 'fontawesomefx', version: '8.9'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
compile group: 'com.lowagie', name: 'itext', version: '2.1.7'
compile group: 'org.olap4j', name: 'olap4j', version: '1.2.0'
compile fileTree(dir: 'lib', include: 'ReportUtilities.jar')
compile('net.sf.jasperreports:jasperreports:6.4.1') {
exclude group: 'com.itextpdf'
}
runtime 'com.itextpdf:itextpdf:5.5.0'
runtime 'com.itextpdf:itext-pdfa:5.5.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'eu.appsatori:gradle-fatjar-plugin:0.3'
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar',
'Implementation-Version': version,
'Main-Class': 'activities.Main',
'Class-Path': ". ${configurations.compile.collect { it.getName() }.join(' ')}"
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Current state of project structure
Inside the jar file there's an activities folder which contains a Main.class file
UPDATE:
jar file -> META-INF -> MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: Gradle Jar
Implementation-Version: 1.0-SNAPSHOT
Class-Path: . ReportUtilities.jar c3p0-0.9.5.2.jar commons-beanutils-1
.9.3.jar commons-digester-2.1.jar commons-logging-1.2.jar commons-val
idator-1.6.jar controlsfx-8.40.13.jar jfoenix-1.7.0.jar mchange-commo
ns-java-0.2.11.jar TrayNotification--SNAPSHOT.jar firebase-admin-5.3.
0.jar annotations-15.0.jar slf4j-simple-1.7.25.jar postgresql-42.1.4.
jar pgjdbc-ng-0.6.jar ecj-4.6.1.jar fontawesomefx-8.9.jar commons-col
lections-3.2.2.jar itext-2.1.7.jar olap4j-1.2.0.jar jasperreports-6.4
.1.jar google-api-client-1.22.0.jar google-api-client-gson-1.22.0.jar
google-http-client-1.22.0.jar json-20160810.jar guava-20.0.jar googl
e-cloud-storage-1.2.1.jar slf4j-api-1.7.25.jar netty-all-4.0.32.Final
.jar bcmail-jdk14-138.jar bcprov-jdk14-138.jar xercesImpl-2.11.0.jar
jcommon-1.0.23.jar jfreechart-1.0.19.jar castor-xml-1.3.3.jar jackson
-core-2.1.4.jar jackson-databind-2.1.4.jar jackson-annotations-2.1.4.
jar lucene-core-4.5.1.jar lucene-analyzers-common-4.5.1.jar lucene-qu
eryparser-4.5.1.jar core-3.2.1.jar icu4j-57.1.jar google-oauth-client
-1.22.0.jar google-http-client-jackson2-1.22.0.jar google-http-client
-gson-1.22.0.jar httpclient-4.0.1.jar google-cloud-core-1.2.1.jar goo
gle-cloud-core-http-1.2.1.jar google-api-services-storage-v1-rev100-1
.22.0.jar castor-core-1.3.3.jar commons-lang-2.6.jar javax.inject-1.j
ar stax-1.2.0.jar stax-api-1.0-2.jar lucene-queries-4.5.1.jar lucene-
sandbox-4.5.1.jar guava-jdk5-17.0.jar httpcore-4.0.1.jar commons-code
c-1.3.jar joda-time-2.9.2.jar api-common-1.1.0.jar gax-1.4.1.jar prot
obuf-java-util-3.3.0.jar proto-google-common-protos-0.1.12.jar proto-
google-iam-v1-0.1.12.jar google-auth-library-credentials-0.7.0.jar go
ogle-auth-library-oauth2-http-0.7.0.jar google-http-client-appengine-
1.21.0.jar google-http-client-jackson-1.21.0.jar stax-api-1.0.1.jar j
akarta-regexp-1.4.jar auto-value-1.2.jar threetenbp-1.3.3.jar protobu
f-java-3.3.0.jar jackson-core-asl-1.9.11.jar bctsp-jdk14-1.38.jar bcp
rov-jdk14-1.38.jar bcmail-jdk14-1.38.jar xml-apis-1.4.01.jar jsr305-3
.0.0.jar gson-2.7.jar
Main-Class: activities.Main
As per the comments here is what was tried.
Making sure a main class is set
Making sure the main class has the same full class name as the one defined
Checking the manifest is valid
Checking the class exists
JavaFX may need a plugin
In the end the last one fixed the issue this was the plugin that fixed the issue.
This was the last comment in the chain:
#jrtapsell Well, this's embarrassing but i'v found the problem, i was unaware that JavaFX apps need a special plugin for Gradle, Using the FiberFox Plugin solved my issue, Thanks for your efforts and may you post answers so that i could upvote you if you like

How to use jUnit 5 with Gradle

Running the IDEA IDE I want to add a gradle dependency for the jUnit v5.
Here is my build.gradle file, I used this answer as a guide.
apply plugin: 'java'
sourceCompatibility = 1.8
repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'
dependencies {
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")
// Enable use of the JUnitPlatform Runner within the IDE
testCompile("org.junit.platform:junit-platform-runner:1.0.0-M4")
compile ("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
junitPlatform {
details 'tree'
}
The problem here is that the jUnit4 annotations are resolved by import but all the v5 annotations are not resolved.
One example:
#ParameterizedTest
public void testExample() {
// My annotations is not resolved
}
What is the right way to add a jUnit5 dependency using gradle?
EDIT
I started a new gradle java project from scratch to get to the bottom of this.
Here is my current build.gradle.
group 'com.iay0361'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'
dependencies {
testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.0-RC3'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-RC3'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-RC3'
compile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0-RC3'
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-RC3'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
junitPlatform {
details 'tree'
}
I wrote the #Test annotation in a new class file under test after which it asked me to "add the 'jUnit5' to classpath
which I did and this time selected the Copy 'jUnit5' libraries to... instead of using the IDEA distributor.
Now it added these files in module:
The file is still RC2 but in build.gradle it is RC3.
There are also no jUnit jars in "External Library" directory
What am I missing, the problem is still that the IDE cannot resolve some jUnit5 annotations like #ParamiterizedTest.
Here is a quick sample on how to configure gradle with junit5. In your dependencies, remove the compile statement for the junit:4.12 artifact verison.
// If you also want to support JUnit 3 and JUnit 4 tests
testCompile("junit:junit:4.12")
In the buildscript() method, include these:
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3' }
}

Adding an additional test suite to Gradle

I am attempting to add Gradle (1.4) to an existing project that has multiple test suites. The standard unit test located in src/test/java ran successfully, but I am having trouble setting up a task to run the JUnit test located in src/integration-test/java.
When I run gradle intTest I get several cannot find symbol errors for classes in src/main. This leads me to believe that the dependencies are not set up correctly. How do I setup intTest so that it will run my JUnit integration tests?
build.gradle
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_6
sourceSets {
integration {
java {
srcDir 'src/integration-test/java'
}
resources {
srcDir 'src/integration-test/resources'
}
}
}
dependencies {
compile(group: 'org.springframework', name: 'spring', version: '3.0.7')
testCompile(group: 'junit', name: 'junit', version: '4.+')
testCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+')
testCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+')
testCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')
integrationCompile(group: 'junit', name: 'junit', version: '4.+')
integrationCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+')
integrationCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+')
integrationCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')
}
task intTest(type: Test) {
testClassesDir = sourceSets.integration.output.classesDir
classpath += sourceSets.integration.runtimeClasspath
}
Details: Gradle 1.4
Solution: I had not set the compile classpath for the integration test source set (see below). In my I code I set the compile class path to sourceSets.test.runtimeClasspath so that I don't have the duplicate dependencies for "integrationCompile"
sourceSets {
integrationTest {
java {
srcDir 'src/integration-test/java'
}
resources {
srcDir 'src/integration-test/resources'
}
compileClasspath += sourceSets.main.runtimeClasspath
}
}
the "integration" sourceSet has not configured its compile and runtime classpath. That's why it can't find the classes from your main sourceset. you can configure the compile and runtime classpath in the following way:
sourceSets {
integTest {
java.srcDir file('src/integration-test/java')
resources.srcDir file('src/integration-test/resources')
compileClasspath = sourceSets.main.output + configurations.integTest
runtimeClasspath = output + compileClasspath
}
}
In most cases you want to use the same dependencies as your unit tests as well as some new ones. This will add the dependencies of your unit tests on top of the existing ones for integration tests (if any).
sourceSets {
integrationTest {
compileClasspath += sourceSets.test.compileClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}
Another way:
test {
exclude '**/*IntegrationTest*'
...
}
task testIntegration(type: Test) {
include '**/*IntegrationTest*'
...
}

Categories