Create external dependencies and add it to other project with gradle - gradle

I have two spring boot projet
A and B
would like to create a new projet to put common thing there
A
B
commons
how to add commons to A and B like a external dependencies?

Assume your project layout as below:
project
|- common
|- proja
|- projb
you need settings.gradle
rootProject.name = 'project'
include 'common'
include 'proja'
include 'projb'
then you need to update your build.gradle under project like this:
plugins {
id 'org.springframework.boot' version '1.5.4.RELEASE'
}
subprojects { // common configurations for all subprojects
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
repositories {
jcenter()
}
dependencies { // common dependencies for all subprojects
compile 'org.codehaus.groovy:groovy-all:2.4.10'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.spockframework:spock-spring:1.0-groovy-2.4'
}
}
project ("proja") {
dependencies { // proja-specific dependencies
compile 'com.google.guava:guava:21.0'
compile project(":common")
}
}
project ("projb") {
dependencies { // projb-specific dependencies
compile project(":common")
}
}
Let me know if this works.

external solution seem the way to go
my commons build.gradle file
apply plugin: 'java-library-distribution'
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.0.M2'
}
distributions {
main{
baseName = 'commons-code'
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.compilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation", "-parameters"]
}
In my main project
i have an build.gradle
in the dependencies section i have
compile project (':commons-code')
and in the settings.gradle file i have
include ":commons-code"
project(":commons-code").projectDir = file("/home/bob/dev/project/commons/")

Related

Gradle bootRun from parent folder in multi-module project

I have my Gradle project, which has 2 modules: project A and project B, where the latter depends on the former. Note that both projects are Spring Boot applications, so when I execute gradle bootRun from their respectives directories, they will start fine.
The problem is that I want to start, from parent directory, the service of project A when I execute gradle bootRun, and it is starting project B. It seems that I am missing some Gradle config.
build.gradle (project A)
group = 'com.oni'
version = '0.0.2-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile 'org.javassist:javassist:3.18.2-GA'
testCompile("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
}
build.gradle (project B)
group = 'com.oni'
version = '0.0.2-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
def withoutInflux = { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-mongodb' }
compile project(':projectA'), withoutInflux
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.7'
}
build.gradle(parent)
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
// mavenLocal()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter')
testImplementation('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.influxdb:influxdb-java')
compile('org.mockito:mockito-core')
compile('ma.glasnost.orika:orika-core:1.4.2')
compile 'com.google.guava:guava-annotations:r03'
}
}
settings.gradle(parent)
rootProject.name = 'project'
include 'projectA'
include 'projectB'
Thanks in advance.
You can run this by using
./gradlew :projectA:bootRun
./gradlew :projectB:bootRun

How can I generate JPA metamodel in spring-boot with gradle?(+ lombok)

I want add JPA metamodel to my project - Spring boot + gradle
I find a lot of examles how can i do it but all with Maven. Also I find this site: https://plugins.gradle.org/search?term=metamodel
and try first three plugins. With each plugins I get errors: error: cannot find symbol in classes marked lombok #Builder annotation and some classes is not entity. It is example some plugin:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.iboyko.gradle.plugins:jpamodelgen-plugin:1.0.1"
}
}
dependencies {
compile ('org.hibernate:hibernate-jpamodelgen')
}
1) Which plugin or method is the most official (correct) to create JPA metamodel in Spring boot + spring-data-jpa + gradle ?
2) How can I specify only the package with entities and not scan another classes?
3) how to make friends with it with lombok?
EDIT
I add this code to gradle file:
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
}
}
and it generate classes_ fine. After that I mark
generated/sources/java
folder ass root of generated classes(rigth clik to this folder and mark as)
After that I try import generated classes in my repository and IDE show this:
For each module I have 2 module - my_module and my_module_main(I don't understand why) and all classes generate in my_module but all code in my_module_main. If I add this dependency - I have this:
and in generated class I have this:
I was struggling through the same issue. Finally I got it working by adding both dependencies in both compileOnly and annotationProcessor forms. Don't ask me why, but it somehow works.
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
compileOnly 'org.hibernate:hibernate-jpamodelgen'
annotationProcessor('org.hibernate:hibernate-jpamodelgen')
Did you use Spring Initalizr to generate the project?
This is what you get from Initializr (expect the jpamodelgen that I added by myself):
buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
compileOnly ('org.hibernate:hibernate-jpamodelgen')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
// Add source set to define where the generated source code should go to
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
}
}
You can find a demo project on GitHub:
https://github.com/simasch/demo-gradle-jpa
In IntelliJ you can right click on generated/sources/java and choose

Standalone Kotlin Gradle plugin not found when trying to apply to other project

I'm trying to make a custom standalone Kotlin plugin for Gradle and am trying to apply that to another project, but am getting a Plugin with id 'my.id.license-tools' not found error when trying to apply it to another project.
This is the structure of the plugin's project:
license-tools
+--src
+--main
| +--java
| |
| +--kotlin
| | +--my.id
| | +--LicenseTools.kt
| |
| +--resources
| +--META-INF.gradle-plugins
| +--my.id.license-tools.properties
+--test
+--build.gradle
This is my build.gradle file:
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'my.id'
version '0.1'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'idea'
apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile gradleApi()
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Then we have the Plugin class LicenseTools.kt, which I've held very simple to make sure the error didn't come from there:
package my.id
import org.gradle.api.Plugin
import org.gradle.api.Project
open class LicenseTools: Plugin<Project> {
override fun apply(project: Project?) {
println("License-Tools plugin has been applied")
}
}
And lastly my.id.license-tools.properties:
implementation-class=my.id.LicenseToolsKt
I've also tried
implementation-class=my.id.LicenseTools
After building everything with ./gradlew clean build I've run ./gradlew install to install the whole thing to the local Maven repository, which it did successfully.
Then I went ahead and added the following to my other project's buildscript block:
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "my.id:license-tools:0.1"
}
And after that added
apply plugin: 'my.id.license-tools'
After trying out many different things, like changing to the new plugins block or making sure the artifacts get correctly installed to the local maven repository I can't find the root of the problem, since according to the many guides I've read through, this should be the correct way to go about making a standalone plugin.
you probably figured by now, but your my.id.license-tools.properties should be named my.id.properties
the content should then be implementation-class=my.id.LicenseTools
then the classpath should be named
classpath "my.id:license-tools:0.1" -> classpath "my.id:LicenseTools:0.1"
and
apply plugin: 'my.id.license-tools' -> apply plugin: 'my.id.LicenseTools'
so that it always is:
package: my.id
name: LicenseTools
I haven't actually tested all of that, though.

Gradle Eclipse Classpath Exception: FAILURE: Build failed with an exception

I am getting the error as attached in the image above whenever I am trying to run gradle eclipse.
I keep on getting this eclipseClassPath exception.
The Gradle Version, I am using is 3.1
Someone suggested me to use gradle version 2.14 because it won't work with the latest version of gradle.
My build.gradle file is below:
buildscript {
ext {
springBootVersion = '1.2.3.RELEASE'
springCloudConnectorsVersion = '1.2.3.RELEASE'
jarName = 'comOrderAudit'
jarVersion = ' -jar build/libs/app-0.0.1-SNAPSHOT.jar'
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.0.RELEASE")
}
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
apply plugin: 'spring-boot'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'jacoco'
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator") {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
compile "org.springframework.boot:spring-boot-starter-test"
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
compile("org.springframework.boot:spring-boot-starter-aop") {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
"org.springframework.cloud:Spring-cloud-core:${springCloudConnectorsVersion}"
compile "org.springframework.cloud:spring-cloud-spring-service-connector:${springCloudConnectorsVersion}"
compile "org.springframework.cloud:spring-cloud-cloudfoundry-connector:${springCloudConnectorsVersion}"
compile 'org.codehaus.jettison:jettison:1.3.8'
compile 'com.datastax.cassandra:cassandra-driver-core:2.1.8'
compile 'com.google.code.gson:gson:2.3.1'
compile 'org.springframework.boot:spring-boot-starter-log4j2'
compile 'org.springframework:spring-oxm'
compile 'org.simpleframework:simple-xml:2.7.1'
compile 'io.springfox:springfox-swagger2:2.0.0'
compile 'io.springfox:springfox-swagger-ui:2.0.0'
compile 'com.wordnik:swagger-jersey2-jaxrs_2.10:1.3.8'
compile 'com.mangofactory:swagger-springmvc:1.0.2'
compile 'com.datastax.cassandra:cassandra-driver-core:2.1.8'
compile 'com.google.code.gson:gson:2.3.1'
testCompile "junit:junit:4.12"
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile 'commons-dbcp:commons-dbcp:1.4'
}
task updateVersion{
Properties props = new Properties()
File propsFile = new File("src/main/resources/application.properties")
props.load(propsFile.newDataInputStream())
println(props.getProperty("buildNumber")+"v")
Integer nextbuildnum = (((props.getProperty("buildNumber")) as Integer) + 1)
props.setProperty('buildNumber', nextbuildnum.toString())
def date = new Date()
def formattedDate = date.format('yyyyMMddHHmmss')
props.setProperty("buildTimeStamp", formattedDate)
props.store(propsFile.newWriter(), null)
props.load(propsFile.newDataInputStream())
}
test {
testLogging {
events 'started', 'passed'
}
jacocoTestReport{
group = "Reporting"
description = "Generate Jacoco coverage reports."
additionalSourceDirs = files(sourceSets.main.java)
reports {
xml.enabled = false
html.enabled = true
}
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it,
exclude: ['**/model/**'])
})
}
}
}
I found the answer through extensive search.
Looks like the issue was with Spring boot version used in the gradle.
With Gradle version 3.1, the recommended spring boot version is 1.4.x releases.
If I am to use spring boot version 1.2.3 the gradle version I should be using is 2.14.
just changed the spring boot version and the build was success.
For more answers you can take a look at this page here.

Gradle - Exclude provided scope jars from War file

Gradle seems to claim that it doesnt include the providedCompile and providedRuntime scopes when building the war file.
But, when i do the build with below war config, a folder named "lib-provided" seems to be containing all the provided scoped jars. How do I limit this functionality to NOT include the provided scoped jars.
configure(rootProject) {
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
targetCompatibility = 1.8
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repository.jboss.org/nexus/content/groups/public-jboss/" }
}
// Import Spring Boot's bom, spring-boot-dependencies
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-dependencies:1.2.5.RELEASE'
}
}
// Override the spring-data-releasetrain.version property
ext['spring-data-releasetrain.version'] = 'Fowler-SR1'
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-security")
............ Other Spring Boot based projects
testCompile("org.springframework.boot:spring-boot-starter-test")
compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.4.6")
......... Below are the "Provided" Scoped packages
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
providedCompile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.23")
providedCompile("org.hibernate:hibernate-core:4.3.7.Final")
providedCompile("org.hibernate:hibernate-infinispan:4.3.7.Final")
providedCompile("org.hibernate:hibernate-entitymanager:4.3.7.Final")
providedCompile("org.hibernate:hibernate-validator:5.1.3.Final")
providedCompile("org.hibernate:hibernate-search-orm:5.0.1.Final")
providedCompile("org.hibernate.common:hibernate-commons-annotations:4.0.4.Final")
providedCompile("org.infinispan:infinispan-core:7.1.1.Final")
providedCompile("org.infinispan:infinispan-query:7.1.1.Final")
testCompile("com.microsoft.sqlserver:sqljdbc41:4.1")
}
configurations {
providedCompile
// replaced with jcl-over-slf4j
all*.exclude group: 'commons-logging', module: 'commons-logging'
// replaced with log4j-over-slf4j
all*.exclude group: 'log4j', module: 'log4j'
}
}
war {
baseName = 'abc'
version = '5.0.0-SNAPSHOT-' + + System.currentTimeMillis();
doFirst {
manifest {
attributes("Implementation-Title": project.name, "Implementation-Version": version, "Implementation-Timestamp": new Date())
}
}
webAppDirName = 'web'
includeEmptyDirs false
archiveName 'abc.war'
}
Thanks!
In the below configuration, Gradle 4.6
None of the below depndnecies will be in the War/WEB-INF lib.
If you have an Ear build.gradle can contain entry like below , which will ensure all depndence jar in the Ear/lib folder.
if the below entry is made, The generated Ear file will have .war file and the War/lib folder will not be having the specified dependent files.
apply plugin: 'war'
earlib project(path: ':MyWebProject', configuration: 'compile')
deploy project(path: ':MyWebProject', configuration: 'archives')
apply plugin: 'war'
dependencies {
providedCompile('org.apache.logging.log4j:log4j-web')
providedCompile('org.springframework.boot:spring-boot-starter-web') {
exclude module: "spring-boot-starter-tomcat"
}
}

Resources