our spring-boot project looks like the following
project
|__ build.gradle
|__ settings.gradle
|__ module_a
|__ module_b
|__ ...
|__ module_a
|__ module_b
module_a contains only the SpringApplication class and a file application.properties
Running ./gradlew build works fine, but the problem is, for every module (about 10) gradle generates a fat jar including all dependencies.
We want to only have one far jar (in module a)
buildscript {
ext {
springBootVersion = '1.2.0.RELEASE'
springLoadedVersion = '1.2.0.RELEASE'
}
repositories {
// NOTE: You should declare only repositories that you need here
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/snapshot" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework:springloaded:${springLoadedVersion}")
}
}
allprojects {
group = "example.group"
version = "0.0.1"
repositories() {
mavenCentral()
}
}
subprojects {
apply plugin: "groovy"
apply plugin: "eclipse"
eclipse {
classpath {
containers.remove("org.eclipse.jdt.launching.JRE_CONTAINER")
containers "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"
}
}
apply plugin: "idea"
apply plugin: "java"
apply plugin: "spring-boot"
mainClassName = "MainClassName"
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
}
}
task wrapper(type: Wrapper) { gradleVersion = '2.2' }
We tried adding the options
jar.enabled = false
bootRepackage.enabled = false
in the subprojects section, but after that, the project wont comile any longer.
Rather than applying the Spring Boot plugin to every sub-project, you could apply it only to module_a:
project(':module_a') {
apply plugin: 'org.springframework.boot'
}
Related
I published a small library of mine to a free maven hosting service, and am using that package in another project. I've done this before without issue but something isn't working this time.
Gradle finds the file just fine, and downloads it I assume. I know this because any change to the URL, package name, or version, and Gradle throws the "can't find the dep in any of these places" error.
However, any import of the packages in this JAR error, saying it can't find the package. IntelliJ, when I refresh gradle deps, also doesn't show my library in the "External Libraries" section.
Here's my gradle.build for the project I'm using the library in:
apply plugin: 'java'
repositories {
maven { url = 'https://repo.repsy.io/mvn/viveleroi/tileowner' }
}
group = project.property("group")
version = project.property("version")
dependencies {
compileOnly 'network.darkhelmet.tileowner:tileowner:1.0.0'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
The test class:
import network.darkhelmet.tileowner.TileOwner;
public class Demo {}
Gradle says it's on the classpath:
compileClasspath - Compile classpath for source set 'main'.
\--- network.darkhelmet.tileowner:tileowner:1.0.0
compileOnly - Compile only dependencies for source set 'main'. (n)
\--- network.darkhelmet.tileowner:tileowner:1.0.0 (n)
I've downloaded the published jar of my library from that repository, unzipped it, and have confirmed all files are in there as expected.
Here's the build.gradle of my library project:
import org.apache.tools.ant.filters.ReplaceTokens
buildscript {
dependencies {
classpath group: 'com.github.rodionmoiseev.gradle.plugins', name: 'idea-utils', version: '0.2'
}
}
plugins {
id "com.github.johnrengelman.shadow" version "7.0.0"
id "xyz.jpenilla.run-paper" version "1.0.6"
id 'maven-publish'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'idea-utils'
apply plugin: 'checkstyle'
processResources {
filter ReplaceTokens, tokens: [
"apiversion": project.property("apiversion"),
"version" : project.property("version")
]
}
repositories {
mavenLocal()
mavenCentral()
maven { url = "https://repo.aikar.co/content/groups/aikar/" }
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
}
group = project.property("group")
version = project.property("version")
targetCompatibility = sourceCompatibility = JavaVersion.VERSION_17
dependencies {
compileOnly 'org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT'
implementation 'co.aikar:acf-bukkit:0.5.0-SNAPSHOT'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
jar {
actions = []
dependsOn = []
dependsOn('shadowJar')
}
shadowJar {
relocate 'co.aikar.commands', 'network.darkhelmet.tileowner.acf'
relocate 'co.aikar.locales', 'network.darkhelmet.tileowner.locales'
}
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
name = 'repsy'
url = 'https://repo.repsy.io/mvn/viveleroi/tileowner'
credentials(PasswordCredentials)
}
}
}
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.
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
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")
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.