Gradle + Java, multiproject build.gradle - java

This is my file hierarchy, 'Domain Module' has no code right now, basically a wrapper for DBController and Domain.
Domain Module
.gradle
.idea
build
DBController
build
src
main
java
interfaces
IDBController.java
DBController.java
res
some SQL files
test
java
some test files
build.gradle
Domain
.gradle
build
gradle
src
main
java
Server.java
build.gradle
gradlew
gradlew.bat
settings.gradle
gradle
build.gradle
gradlew
gradlew.bat
settings.gradle
This is my build.gradle in Domain Module/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
targetCompatibility = 1.8
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile project(':Domain')
compile project(':DBController')
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
this is build.gradle in DOmain Module/DBController/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.postgresql:postgresql:9.3-1103-jdbc3'
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
dependencies {
compile('com.googlecode.json-simple:json-simple:1.1.1')
compile files('libs/json-simple-1.1.1.jar')
compile('org.postgresql:postgresql:9.3-1103-jdbc3')
}
And finally, build.gradle in Domain Module/Domain/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
jcenter()
}
}
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
jcenter()
}
sourceCompatibility = 8
targetCompatibility = 8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
dependencies {
compile project(':DBController')
}
My main method is in Server.java, and it uses an instance of DBController. How do i assign this file in my java manifest? I've tried the simple
jar {
manifest {
attributes 'Main-Class': 'Domain.src.main.java.Server'
}
but whenever i try to execute java -jar -the generated jar in Domain Module/build/libs-
i get an error telling me it can't find the main file, and as the build gradles are now it gives me an error saying there's no reference to a main class at all.
The gist of my project is that DBController issues queries against a SQL server, and that Server.java will be a spring server. I decided to use gradle to do this so i would learn, and while i have learned alot about gradle, there is still much uncertainty.

I just figured out what was wrong.
jar {
manifest {
attributes 'Main-Class': '-insert main class here-'
}
}
This attribute assumes you're in the src/main/java dir to begin with, so if my filepath was
src/main/java/robert/util/Class.java
i would just have to say
jar {
manifest {
attributes 'Main-Class': 'robert.util.Class'
}
}
Spent so much time solving such a trivial error. My tip for anyone else is to not overlook the 'intro to gradle' sites and such. The solution was there all along.

Related

how to exclude a folder from checkstyle of Gradle on Intellij

I am new for IntelliJ and Gradle, but I have enough experience on Java and Eclipse. I generated some java classes from wsdl file. I put them under src/main/resources with a folder name - "generatedsources".
I try to prevent checkstyle for this folder and its subfolders like src/main/resources/generatedsources/* with gradle on IntelliJ.
I tried some lines such;
task checkstyle(type: Checkstyle) {
source 'src'
// include '**/*.java'
// exclude '**/gen/**'
// exclude '**/R.java'
// exclude '**/BuildConfig.java'
exclude 'src/main/resources/generatedsources/**'
}
But I'm failed again.
build.gradle;
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
sourceCompatibility = 1.7
version = '1.0'
...
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
task checkstyle(type: Checkstyle) {
source 'src'
// include '**/*.java'
// exclude '**/gen/**'
// exclude '**/R.java'
// exclude '**/BuildConfig.java'
exclude 'src/main/resources/generatedsources/**'
}
EDIT - After recommendations(but still failed!):
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
sourceCompatibility = 1.7
version = '1.0'
description = """BLABLABLA_application"""
war.archiveName = "BLABLABLA.war"
configurations{
deployerJars
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.3'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.3'
compile group: 'org.springframework', name: 'spring-core', version:'4.0.0.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version:'4.0.0.RELEASE'
compile 'log4j:log4j:1.2.17'
compile 'com.sun.xml.ws:jaxws-rt:2.2.8'
compile 'org.jvnet.jax-ws-commons.spring:jaxws-spring:1.9'
compile group: 'org.springframework', name: 'spring-webmvc', version:'4.0.0.RELEASE'
providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
deployerJars "org.apache.maven.wagon:wagon-http:2.2"
}
jar {
manifest {
attributes 'Implementation-Title': 'BLABLABLA', 'Implementation-Version': version
}
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
wsdl2java{
wsdlsToGenerate = [
["$projectDir/src/main/resources/BLABLABLA.wsdl"]
]
generatedWsdlDir = file("$projectDir/src/gen/java")
wsdlDir = file("$projectDir/src/main/resources")
}
wsdl2javaExt {
cxfVersion = "2.5.1"
}
tasks.withType(Checkstyle) {
exclude '**/your/generated/package/goes/here**'
}
checkstyleMain.exclude '**/your/generated/package/goes/here**'
"exclude" in tasks.withType and "checkstyleMain" causes an error such as "cannot resolve symbol"!
When checkstyle plugin is applied custom tasks are delivered along with it (see here), so instead of adding custom task with type Checkstyle configure the shipped one. This is how it should be done:
tasks.withType(Checkstyle) {
exclude '**/your/generated/package/goes/here**'
}
You can also configure a particular task, not all:
checkstyleMain.exclude '**/your/generated/package/goes/here**'
Also this is not good practice to put generated sources under src/main/resources. Typically such files goes to e.g. src/gen/java
I followed some comments and make it work add this in file build.gradle
checkstyle {
config = rootProject.resources.text.fromFile("${project.projectDir}/checkstyle-8.34_google_checks.xml")
toolVersion = '8.34'
ignoreFailures = false
maxWarnings = 0
}
checkstyleMain
.exclude('com/examples/gen/api/*.java')
.exclude('com/examples/gen/model/*.java')
.exclude('com/examples/gen/auth/*.java')
You can only exclude a folder that is in the source set. That is something that had not been mentioned and it caused me to struggle since I am using Intellij. The generated files are in the build/ folder which is on the same level as src/. I tried excluding the build folder for a while but there was no effect. The only solution was to specify the source folder. Since the build folder is not under src (the source folder), it worked immediately.
checkstyleMain.source = "src/main/java"
I found the solution here: Specify excludes to Checkstyle task
I had lots of issues with checkStyle & pmd + QueryDSL
A final solution for me was not to exclude generated sources, but clean them after assemble task: no sources - nothing to analyze! :)
assemble {
finalizedBy cleanQuerydslSourcesDir
}
For kotlin dsl use
tasks.withType<Checkstyle>() {
exclude("**/com/generated/*.java")
}

Package org.json does not exist while pushig spring-boot(gradle) project to heroku

I am trying to deploy spring boot gradle project to heroku. The app is running fine locally. But after executing git push heroku master the push crashes with the error error: package org.json does not exist
Here is my Procfile
web: java -Xmx384m -Xss512k -XX:+UseCompressedOops -jar target/*.jar
--server.port=$PORT
--spring.data.mongodb.uri=$MONGOLAB_URI
and here is my build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
jar {
baseName = 'planaroute'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile('org.springframework.boot:spring-boot-starter')
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile 'org.springframework.data:spring-data-mongodb:1.9.2.RELEASE'
compile group: 'org.json', name: 'json', version: '20160810'
compile group: 'org.json', name: 'org.json', version: 'chargebee-1.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
Even I have downloaded org.json jar and added the jar externally to the project. It still doesnot work.
I added the following dependency to my build.gradle file and it worked fine after.
dependencies {
compile 'org.json:json:20090211'
.
. all my other project dependecies
.
}
I hope it helps you as it helped me.
Credits to
Dependencies not copied into jar (in Gradle) where I got this hint from.
Best wishes,
Marcos.

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).

Adding dependencies to a custom gradle plugin

I'm creating a gradle plugin that uses gson, but when I use the plugin at my client it throws this java.lang.NoClassDefFoundError: com/google/gson/Gson
I expect I am linking my dependencies in the plugin in a wrong way, but i'm not quite sure so any help would be great.
The build.gradle in the plugin
group 'nl.daanluttik.gradle'
version '0.1'
apply plugin: 'java'
apply plugin: 'maven' // the plugin to distribute to maven
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '1.7.2'
compile gradleApi()/*The gradle plugin api*/
testCompile group: 'junit', name: 'junit', version: '4.11'
}
//To distribute to maven
uploadArchives {
repositories {
mavenLocal()
}
}
A segment of the buildgradle in the client project
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath group: 'nl.daanluttik.gradle', name: 'peach', version: '0.1'
}
}
Is this really the first error? I most often see NoClassDefFoundError (in contrast to ClassNotFoundException) if some static initializer threw some exception and because of that the class could not be loaded and is not available later on.
Your missing the pom file with your dependencies. If it's just java then you can easily use the maven-publish which will generate the pom for you correctly.
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
groupId 'nl.daanluttik.gradle'
artifactId 'peach'
version '0.1'
from components.java
}
}
}
Then you can publish to the repositories (default local only) with gradle publish
Reference: https://docs.gradle.org/current/userguide/publishing_maven.html

How can I add a generated source folder to my source path in Gradle and IntelliJ?

I use thrift and it generates some source java files(interfaces) under build directory (build/generated-sources/thrift/<package name>/<class>) but under my src/main/java I have my classes which has the same package definition as in the generated java files and my classes also implements the interfaces generated by the thrift so how can I configure this in my build.gradle so it works on intelliJ as well as the build
plugins {
id "org.jruyi.thrift" version "0.3.1"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "org.jruyi.thrift"
group 'com.hello'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.thrift', name: 'libthrift', version:'0.9.3'
compile 'com.datastax.cassandra:cassandra-driver-core:3.0.0'
compile 'com.datastax.cassandra:cassandra-driver-mapping:3.0.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
compileThrift {
thriftExecutable "/usr/local/hello/bin/thrift"
sourceDir "src/main/thrift"
createGenFolder false
}
task thrift(type: Exec) {
commandLine '/usr/local/hello/bin/thrift'
}
compileJava {
dependsOn 'compileThrift'
The gradle build should work automatically.
To make it work on Intellij, try adding the following to your build.gradle.
idea.module.sourceDirs += file("$buildDir/generated-sources/thrift")
Don't forget to refresh your gradle projects.

Categories

Resources