Can gradle include only specific package and exclude everything else - java

From my project I'm building two jars, one a fat jar for server and the other a thin jar for client. Is there a way to NOT specify all the excludes and make the include mutually exclude everything else (the dependencies as well)
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'eclipse'
repositories { jcenter() }
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
dependencies {
compile(bootweb)
compile(bootundertow)
testCompile(boottest)
}
mainClassName = 'mordeth.sentinel.util.SentinelServer'
jar{
baseName='sentinel-service-boot'
version = version
}
task clientJar(type: Jar){
baseName='sentinel-service-client'
version = version
from sourceSets.main.output.classesDir
include 'mordeth/sentinel/dto/*.class'
}
artifacts{
archives jar, clientJar
}

apply plugin: 'spring-boot'
apply plugin: 'java'
apply plugin: 'eclipse'
repositories { jcenter() }
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
dependencies {
compile(bootweb)
compile(bootundertow)
testCompile(boottest)
}
mainClassName = 'mordeth.sentinel.util.SentinelServer'
jar {
baseName='sentinel-service-boot'
version = version
}
task clientJar(type: Jar, dependsOn: jar){
baseName = 'sentinel-service-client'
from sourceSets.main.output.classesDir
include 'mordeth/sentinel/dto/*'
}
bootRepackage.withJarTask = jar
artifacts{
archives jar, clientJar
}
include method in gradle is by definition mutually exclusive i.e it'll exclude everything not otherwise specified in the include. To avoid spring-boot dependencies getting added to the client jar, one can simply restrict the bootRePackage to a specific (in this case the default) jar task
bootRepackage.withJarTask = jar

Related

Spring Boot: can't load entities, services, etc. from jar

This issue is widely commented in Stack Overflow, but none of the solutions seems to fit my project.
I have two independant projects (MainApplication and LibraryApplication), then I want to export LibraryApplication , and import it into main project (this is a reduced approach for keeping things simple).
I exported the library.jar to libs/ folder in the main project, then I added the compile files instruction to build.gradle.
The jar is indeed added to the classpath, and I can see the classes from MainApplication. So I added #ComponentScan, #EnableJpaRepositories, #EntityScan, etc. but none of those annotations seems to work, because only MAIN_ENTITY is created when I run the MainApplication project.
LibraryApplication project structure
MainApplication project structure
Only MAIN_ENTITY is created
I tried many of the solutions commented in other related questions, but none works for me:
I moved MainApplication.java to an upper package level without results.
I created an AppConfig.java in LibraryApplication project, and configured #ComponentScan("com.app"),#EntityScan("com.app"), #EnableJpaRepositories("com.app") and injected the class in the MainApplication without results.
I removed the .* from my annotations, so instead of #ComponentScan("com.app.*") I have #ComponentScan("com.app") without results.
How can I achieve the expected results?
I created two github repos so you can check/download the code.
https://github.com/ferdonof/main.git
https://github.com/ferdonof/library.git
Thanks in advance!
UPDATE
Sharing the build.gradle config of each project:
For library
buildscript {
ext {
springBootVersion = '2.0.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'
apply plugin: 'io.spring.dependency-management'
group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
bootRepackage {
classifier = 'exec'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
For main
buildscript {
ext {
springBootVersion = '2.0.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'
apply plugin: 'io.spring.dependency-management'
group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile files('libs/library.jar')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Add multi project itself as a dependency in another project

After a lot of searching, it was the last option to raise it here! In eclipse, I am designing such project structure using Gradle, as shown below...
Algorithms //Parent Project
-SubModuleCore //Child Project for common utilities & dependencies
-build.gradle
-SubModuleOne //Child project for any operation
-build.gradle //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
-SubModuleTwo //Child project for another operation
-build.gradle //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
-build.gradle
-settings.gradle
Services //Stand-Alone project
-build.gradle //Here I want to add 'Algorithms' as a single dependency
-settings.gradle
Project structures are same in eclipse work-space as shown above. I am able to generate individual .jar of Algorithms project. So the problem is I want to add this Algorithms project as a single dependency in project Services like compile project(':Algorithms'). But eclipse just saying 'shut-up!'. I don't want to publish it somewhere like maven central / jitpack etc. instead I want to do it locally. I'm trying this way...
Services/build.gradle
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
project.webAppDirName = 'WebContent'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(dir: 'lib', include: ['*.jar'])
compile project(':Algorithms')
}
Services/settings.gradle
rootProject.name = 'Services'
include 'Algorithms'
project(':Algorithms').projectDir = new File(settingsDir, '../Algorithms')
Algorithms/build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = "1.8";
targetCompatibility = "1.8";
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = "1.8";
targetCompatibility = "1.8";
buildscript {
dependencies {
repositories {
jcenter()
mavenCentral()
}
}
}
repositories {
jcenter()
mavenCentral()
}
}
subprojects.each { subproject ->
evaluationDependsOn(subproject.path)
}
jar.dependsOn subprojects.tasks['classes']
jar {
baseName = 'algorithms'
subprojects.each { subproject ->
from subproject.sourceSets.main.output.classesDir
}
from files('resources/log4j2.xml')
from files('resources/application.properties')
}
Algorithms/settings.gradle
rootProject.name = 'Algorithms'
include ':SubModuleCore', ':SubModuleOne', ':SubModuleTwo'
I tried several solutions from SO, still not succeeded. Somebody please help me, I got stuck badly here. It seems I am very close to this, but don't know what is missing!
Thanks
You can use the includeBuild feature.
Declare the included build in Services/settings.gradle
rootProject.name='Services'
includeBuild '../Algorithms'
and then express the dependency using
compile "${project.group}:${project.name}"
where project group and name the one from the Algorithms project.

Gradle build successful, error running jar

I am new to gradle.
I am building a project using gradle.
It build successfully without any error. While running the build jar file it is giving classNotFoundException.
I am building a simple spring project from spring.io
However question look similar to this but, could not find a solution. Please help.
edit: This is how my build.gradle looks
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
startScripts {
mainClassName = 'Application'
}
springBoot {
mainClass = "Application"
}
You'll need to start the application with the generated start scripts. They will automatically take care of setting up the proper classpath.

gradle: migrate spring app to a multiproject build

I followed this tutorial. This runs fine.
I would like to create a multiproject build with one project that contains this spring application. So I added this project to a subfolder called web of this multiproject build, added a settings.gradle file:
include 'web'
As well as a build.gradle file:
apply plugin: 'application'
mainClassName = "hello.Application"
jar {
baseName = 'VoPro'
}
dependencies {
compile project(':web')
}
However, when i run $ gradle build, i get the error:
Could not resolve all dependencies for configuration ':runtime'.
> Cannot resolve external dependency org.springframework.boot:spring-boot-starter-web: because no repositories are defined.
Required by:
:test:unspecified > test:web:unspecified
Why can't gradle find any repositories?
EDIT: The web subproject contains the following build.gradle file (like in the tutorial):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
You should add a build.gradle file under the web project itself and configure appropriate repositories there. Or add a global build.gradle and the following piece of code in it:
allprojects {
repositories {
mavenCentral()
mavenLocal()
}
}
Basically the multimodule project should have the following structure
-- build.gradle // all projects configuration
-- settings.gradle //includes all modules
-- module1
-- build.gradle
-- module2
-- build.gradle
-- modulen
-- build.gradle
..
After discussion in comments:
You need to specify the dependency along with version, no idea why:
compile("org.springframework.boot:spring-boot-starter-web:1.2.2.RELEASE")

Gradle doesn't compile classes in war project

I have following project setup with Eclipse:
project
build.gradle
libraries.gradle
settings.gradle
ear-project
build.gradle
ejb-project
build.gradle
war-project
build.gradle
This is build.gradle from root project:
apply from: "./libraries.gradle"
allprojects {
repositories {
mavenCentral()
}
}
buildscript {
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "findbugs"
group = "foo.bar"
version = "1.0.0-SNAPSHOT"
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
configurations {
provided {
description = "Non-exported compile-time dependencies"
}
}
sourceSets.main {
compileClasspath += configurations.provided
}
eclipse {
classpath {
plusConfigurations += configurations.provided
}
}
dependencies {
provided(libraries.javaee)
provided(libraries.ejb)
provided(libraries.cdi)
provided(libraries.jpa)
}
}
ear-project build.gradle:
apply plugin: "ear"
dependencies {
deploy project(path: ":ejb")
deploy project(path: ":war", configuration: "archives")
}
ejb-project build.gradle
apply plugin: "java"
war-project build.gradle
apply plugin: "war"
After running "gradle build" from command line the .ear-file is generated, but the .war-file contains only non-compiled .java classes. Classes in ejb project are compiled into .class. Why the classes in war-project are not compiled?
The Java files must be in src/main/java. Not src/main/webapp. src/main/webapp is the directory where the webapp static assets are located: JSP files, HTML files, JS files, etc.

Categories