Gradle doesn't compile classes in war project - java

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.

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/

Intellij IDE not see generated classes in gradle project

I have this gradle config:
buildscript {
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 'mygroup'
version '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
...
}
bootJar {
mainClassName = 'mypakeg.Application'
archiveName = 'my-server.jar'
}
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$projectDir/generated/sources/java")
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
implementation ('org.hibernate:hibernate-jpamodelgen')
annotationProcessor ('org.hibernate:hibernate-jpamodelgen')
implementation('org.projectlombok:lombok')
annotationProcessor 'org.projectlombok:lombok'
}
After build I have folder generated/sources/java with generated files. But Intellij IDE not see this classes. I try click rigth button and marck this folder as root generated classes. But It not helpet becaus:
I have separated module my_server and my_server_main and all classes generated in my_server but my code in my_server_main and if I add dependency to my_server_main I have circular dependency.
How can I generate classes and set path to them?

Why Gradle is compiling .java files but not creating my .jar?

I recently started using Gradle but i'm having an issue compiling my application.
This is my build.gradle:
group 'net.zentya'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
maven { url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/groups/public" }
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
compile 'org.bukkit:bukkit:1.11.2-R0.1-SNAPSHOT'
compile 'net.md-5:bungeecord-chat:1.11-SNAPSHOT'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
When building with the IntelliJ's pre-made button i get my project compiled's class files into the path {project}/gradle/classes/main . But i don't have the final JAR.
How i can fix?
gradle jar or gradle shadowJar commands can be used to create the jar file of gradle based applications.

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

Categories