I have root gradle project which needs to have a small Spring Boot subproject. This subproject will be deployed in the same pod as root project, so it needs to be built whenever root project is built. I tried by creating the following structure:
subproject/
├─ src/...
├─ build.gradle
settings.gradle
with the following contents:
settings.gradle:
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'root'
include 'subproject'
build.gradle:
plugins {
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.demo'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
But when I refresh the project from within the IDEA, it shows Task 'wrapper' not found in project ':subproject'. What did miss? This subproject cannot exist by itself and shouldn't have a 'wrapper', it should be build by the root project.
$ ./gradlew -q projects
Root project 'root'
+--- Project ':subproject'
Gradle version: 6.8.3, Java version: 11
The problem actually does not exist. Everything is correct, except that IDEA added subproject as separate gradle project (I guess). So when I hit refresh, it could not be performed because IDEA was trying to find wrapper in the subproject. Simply saying, if you have such problem, check how many projects you have in your IDEA's Gradle view. In my case, there was two - root and subproject, the latter is unneeded.
Related
I have 2 different project build on mvn. I am trying to replace to Gradle.
Project 1 is an SDK, and project 2 is using that sdk (example).
In the time of maven it creates artifact using mvn install which adds the whole project into local repository.
I like to work in gradle like that. I like project 1 build.gradle need to post it as a gradle local repository and then example project need to use it.
In maven we do mvn install which adds a project artifact into .m2 folder but how to do in gradle so what i can add a project artefact's into the local repository.
Any way that I can do so?
sdk/build.gradle:
apply plugin: "maven"
group = "foo"
version = "1.0"
example/build.gradle:
repositories {
mavenLocal()
}
dependencies {
compile "foo:sdk:1.0"
}
$sdk> gradle install
$example> gradle build
You may be looking for:
gradle publishToMavenLocal
build.gradle:
plugins {
// other plugins
id 'maven-publish'
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
}
See: Maven Publish Plugin
Check out Gradle's documentation on multi-project builds.
Here's an example, with some extra dependencies. Just call gradle install in the root folder, and all will be built and put to your local repo.
Folder structure:
root
+--> build.gradle
+--> settings.gradle
+--> sdk
| +--> build.gradle
+--> example
+--> build.gradle
root/build.gradle:
allprojects {
apply plugin: 'java'
apply plugin: 'maven'
group = 'myGroup'
version = '0.1-SNAPSHOT'
}
root/settings.gradle:
include 'sdk'
include 'example'
root/sdk/build.gradle:
dependencies {
// just an example external dep.
compile group:'commons-lang', name:'commons-lang', version:'2.3'
}
root/example/build.gradle:
dependencies {
compile project(':sdk')
compile group:'log4j', name:'log4j', version:'1.2.16'
}
You need to publish your own library to your local repository. You can do that in the following way:
Add maven-publish plugin:
plugins {
// your other plugins come here...
id 'maven-publish'
}
Add the publishing section to your build file:
publishing {
publications {
myCoolLibrary(MavenPublication) {
from components.java
}
}
}
Run gradle build publishToMavenLocal
Find more details in the documentation.
With the following gradle file within a multi project or multi module build, (lets call it web)
plugins {
id 'groovy'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
}
dependencies {
implementation "org.codehaus.groovy:groovy:2.5.14"
implementation(project(':persistence'))
}
I'm unable to resolve the class files present in the project 'persistence' and get the following error while compiling
unable to resolve class XXX
This is the gradle file of the 'persistence' module
plugins {
id 'groovy'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
}
dependencies {
implementation "org.codehaus.groovy:groovy:2.5.14"
}
What could be the problem?
Also, here is the settings.gradle and directory structure
// settings.gradle
pluginManagement {
plugins {
id 'org.springframework.boot' version "2.3.3.RELEASE"
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
}
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
rootProject.name = 'gradle-bug'
include 'commons'
include 'persistence'
include 'security'
include 'web'
When gradle sees a "implementation" dependency on a project, it uses the jar of that dependency for compilation.
When you add the org.springframework.boot plugin, it by default disables the jar creation task for that module, but gradle continues to depend on that jar for its compilation, even though the classes of that module have been generated in the build/classes folder.
To fix this, you have to enable the jar creation by adding the following snippet to the persistence module
jar {
enabled = true
}
In order to not manually manage, both, Maven and Gradle build configuration files, I wanted to let Gradle generate the Maven POMs for a multi-module build.
This is working so far. Below you find the settings.gradle
rootProject.name = 'parent'
include 'module-a'
include 'module-b'
and here follows build.gradle
allprojects {
apply plugin: 'maven'
group = 'com.example.project'
version = '1.0-SNAPSHOT'
}
subprojects {
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
task createPom << {
pom {
project {
parent {
groupId project.group
artifactId rootProject.name
version project.version
}
}
}.writeTo("pom.xml")
}
}
task createPom << {
pom {
project {
packaging 'pom'
modules {
module 'module-a'
module 'modula-b'
}
}
}.writeTo("pom.xml")
}
The problem is that I have to manually declare the modules in the createPom task of the root project. Also, I need two dedicated tasks; one for the root project and and for the subprojects.
How can I let Gradle figure out what the modules are? Or is there a way to programmatically determine and add the subprojects as modules? Furthermore, is it even necessary to have two distinct tasks?
With Gradle it is possible to build a .war archive from a Java project. I've always been using a project setup as follows to pack some JavaScript SPA (Angular, vue.js...) frontend and a Spring Boot backend together into one .war file:
Directory structure
/
build.gradle
settings.gradle
/server
/build/libs/server.war
/src ...
build.gradle
/client
/dist/** <== This is where e.g. webpack puts the SPA build
build.gradle
/src ...
Topmost settings.gradle
include 'client', 'server'
'server' build.gradle
plugins {
id 'java'
id 'idea'
id 'org.springframework.boot' version '2.0.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.5.RELEASE'
id 'war'
}
repositories {
mavenCentral()
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
compileOnly('org.projectlombok:lombok:1.16.16')
annotationProcessor('org.projectlombok:lombok:1.16.16')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
war.dependsOn(':client:build')
war {
from '../client/dist/'
}
'client' build.gradle
plugins {
id 'com.nickcharles.yarn-run' version '1.0.1'
}
This is working just fine. But I can't imagine that this is the cleanest solution for that kind of task.
Backdraws of my solution are:
Final result of build is stored in build/libs of the :server subproject, not in the root project where it semantically belongs
The built client must be refered to in a static way.
How would a clean solution/the contents of the topmost build.gradle file look like, in order to avoid those backdraws?
I'm facing some problems with my multimodule project gradle build in IntelliJ. My gradle build works correctly, but IntelliJ is not picking it up as it should causing my development cycle to slow down drastically.
I have this dependency setup between my projects (5 projects, 3 wars, shared has my model etc. common-web has the common web components)
TD (war) -> common-web
CP (war) -> common-web
CP (war) -> Shared
Web (war) -> common-web
Web (war) -> Shared
Shared
common-web
With the following structure:
|- TD
|- Web
|- Shared
|- common-web
|- CP
| settings.gradle
| build.gradle
I have one gradle build file and one gradle settings file.
include 'TD', 'CP', 'Web', 'Shared', 'common-web'
allprojects {
repositories {
mavenCentral()
}
sourceCompatibility = 8
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
dependencies {
// dependencies...
}
}
project(':CP') {
apply plugin: 'war'
dependencies {
// dependencies...
compile project(':shared')
compile project(':common-web')
}
}
project(':web') {
apply plugin: 'war'
dependencies {
// dependencies...
compile project(':common-web')
compile project(':shared')
}
}
project(':shared') {
dependencies {
// dependencies...
}
}
project(':TD') {
apply plugin: 'war'
dependencies {
// dependencies...
compile project(':common-web')
}
}
project(':common-web') {
apply plugin: 'war' // needed to allow the providedComp
dependencies {
// dependencies...
}
}
Is there a way I should configure the gradle idea plugin to allow this to be build correctly? have found the make button does not work like I think it should work, ignoring the compile errors in the modules etc. I see that in the root of the project, IntelliJ creates a folder classes/artifacts and does not use the (correct) build in the module/lib folder.
The errors I'm seeing is the wrong include of the jars. For example the web project has a dependency to the TD project, which is defined nowhere.
Is this a bug in the gradle plugin of IntelliJ or am I doing something I shouldn't?