gradle: migrate spring app to a multiproject build - java

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

Related

gradle not able to detect module

I have a multimodule project. there is 2 modules:
- server module
- domain module
in the build.gradle of the domain module I put :
group = 'com.xxx.yyyy.zzz'
version = '1.0.0-SNAPSHOT'
and the server module imports the domain module as follow :
dependencies {
compile group: 'com.xxx.yyyy.zzz', name: 'domain', version: '1.0.0-SNAPSHOT'
...
}
howerver he is not able to detect it!
the jar is published to the maven m2 repo and I am able to see it. and i am using mavenLocal to get dependecies from the m2
repositories {
mavenLocal()
mavenCentral()
}
I deleted the gradle cache, stopped the gradle daemon without success!
Any idea ?
the build.gradle of the server module:
group = 'com.xxx.yyy.zzz'
version = '1.0.0-SNAPSHOT'
repositories {
mavenLocal()
mavenCentral()
}
apply plugin: "org.springframework.boot"
apply plugin: "net.ltgt.apt"
apply plugin: "maven"
apply plugin: 'base'
apply plugin: 'maven-publish'
dependencies {
compile group: 'com.xxx.yyy.zzz', name: 'domaine', version: '1.0.0-SNAPSHOT'
// other dependecies ......
}
the build.gradle of the domain module:
group = 'com.desjardins.experiencecredit.gestiondemandes'
version = '1.0.0-SNAPSHOT'
repositories {
mavenLocal()
mavenCentral()
}
apply plugin: "maven"
apply plugin: 'base'
apply plugin: 'maven-publish'
uploadArchives {
repositories {
mavenDeployer {
repository(url: mavenrepo)
}
}
}
jar.finalizedBy uploadArchives
the settings.gradle of the main project :
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'XXX'
include 'server'
include 'domaine'
If you have multiple modules in the same project, you should instead declare the dependencies using the project object:
settings.gradle at root project level
rootProject.name = 'my-application'
include 'server'
include 'domain'
Then define the build files - root project build.gradle, server module build.gradle and domain module build.gradle.
In the server module's build.gradle, declare the dependency as follows:
dependencies {
compile project(':domain')
...
}
Refer: https://guides.gradle.org/creating-multi-project-builds/

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.

Can gradle include only specific package and exclude everything else

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

Gradle - multi project compiles but shared project classes not found

We are trying to setup a multi project build.
We have 2 projects:
1) PlayerManager
2) Shared
our project compiles and all the tests succeed when we run the gradle.test task
Problem is when we try to run the project on the tomcat from whithin eclipse we get class not found error for all the files in the Shared project.
Here is our gradle.build files:
PlayerManager (root)
=======================
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
apply plugin: 'eclipse'
apply from: 'dependencies.gradle'
apply from: 'xmlHandler.gradle'
apply plugin: 'base'
dependsOn(":NGShared:NGShared")
task wrapper(type: Wrapper) {
gradleVersion = '1.9'
}
repositories {
mavenCentral()
}
dependencies {
providedCompile 'javax.servlet:servlet-api:2.5'
compile 'org.springframework:spring-webmvc:3.2.2.RELEASE'
runtime 'javax.servlet:jstl:1.1.2'
}
/* Change context path (base url). otherwise defaults to name of project */
jettyRunWar.contextPath = ''
task cleanAndBuild(dependsOn: ['clean', 'assemble'])
assemble.mustRunAfter clean
tasks.withType(Test) {
testLogging {
events 'passed'
}
}
task testSanity(type: Test, dependsOn: testClasses) {
exclude '*/TimeBaseTest*'
}
Shared(sub project)
=======================
apply plugin: 'java'
apply plugin: 'eclipse'
apply from: 'dependencies.gradle'
task wrapper(type: Wrapper) {
gradleVersion = '1.9'
}
settings.gradle
include ":NGShared:NGShared"
======================================================
Our path for the project is:
/NGPlayerManager/
/NGPlayerManager/NGShared/NGShared
Any ideas why ?
Thanks
Have you tried to run 'gradle eclipseClean' and then 'gradle eclipse' in order for gradle to regenerate all internal eclipse config files? If you don't do this, Eclipse will not automatically regenerate its configuration and will not include your child projects into the .ear or .war package

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