Spring boot makes it really easy to setup a simple app.
But it takes me longer to actually get a jar file which I can upload to a remote server.
I am using IntelliJ, no command line, and I use gradle. The application is running somehow out of Intellij. But where are the created files? Where is my jar from Bootjar?
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
// add spring data repos
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.postgresql:postgresql:42.2.4")
// REST interface
compile("org.springframework.boot:spring-boot-starter-data-rest")
// Security
compile("org.springframework.boot:spring-boot-starter-security")
}
Update: Added a picture of the project structure:
Update 2: Folder structure:
There will not be a jar created if you are just running this in your IDE. In order to do that, you need to run the gradle build (in your case) either from your IDE or the command line to get it to build it into a jar.
From the command line, go to your project directory and type this:
./gradlew build
This executes the gradle wrapper, which should download everything you need to run the build, and then executes the build.
You will then find your jar in build/lib
build/libs (if you've ran build to build the jar file)
Related
I am trying to build a Spring Boot/Gradle project and create a jar without a main class. My purpose is that this project is a library that will be pulled in by other projects therefore the library project does not require a main class to run. Unfortunately, no matter what kind of gradle config I write I keep getting errors when I try to build install about not having a main class or not being able to find the bootJar task.
Here's what my gradle file looks like:
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE' apply false
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
repositories {
mavenCentral()
}
jar {
enabled = true
}
bootJar.dependsOn fooTask
But when I run this I get the following error:
Could not get unknown property 'bootJar' for root project
'foo-library' of type org.gradle.api.Project.
What in my configuration needs to change?
Disable bootJar in your build.gradle
bootJar {
enabled = false
}
I just want to set the default profile when I run gradleRun, but this is failing with cannot find method run()
I'm first wondering:
What does the buildscript do for me and how can I successfully use the spring-boot plugin
Could not find method bootRun() for arguments [build_74d21ufxy8p9tyrqny7v4pkut$_run_closure1#389a9e15] on root project 'core' of type org.gradle.api.Project.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE"
}
}
task local {
run { systemProperty "spring.profiles.active", "development" }
}
bootRun.mustRunAfter local
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
apply plugin: 'spring-boot'
group = 'com.remindful'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
Remove
task local {
run { systemProperty "spring.profiles.active", "development" }
}
bootRun.mustRunAfter local
Add
bootRun {
systemProperty "spring.profiles.active", "development"
}
To answer your questions about build script and spring-boot plugin, build script contains the tasks needed to build a project using gradle, this is simplistic description, check the documentation. Spring boot plugin
allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies
You can refere to the documentation here
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 am attempting to work through the Spring Framework Restful Web Service creation tutorial(https://spring.io/guides/gs/rest-service/#scratch) using Gradle and IntelliJ. I have followed everything to the letter but being fairly new to Spring, IntelliJ, and Java in general I'm unsure how to go about further debugging my issue.
When I attempt to build my project I receive a few errors stating "Java: package org.springframework.web.bind.annotation does not exist." I'm guessing I'm missing a library reference but am unsure how to check and include it.
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework:spring-web:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'hello_springtest'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
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'
}
}
Just thought I'd add some additional information. I'm still seeing the errors and am unsure why but my project does report that the build was successful. When I attempt to make the project however that's when I receive the annotation does not exist error.
You have some dependency in your builds script, which seems to me redundant and causes Gradle to look up for additional dependencies.
Just remove this dependency from your buildscript dependencies
classpath("org.springframework:spring-web:${springBootVersion}")
I see no reason to use it within your buildscript.
I'm using Gradle as my build configuration tool. I'm developing on my Windows desktop but the actual distribution will be run on Linux.
When I run the 'distZip' task it generates a zip file which contains a .bat file which runs fine on Windows but not on Linux.
Is there a way to configure Gradle so that it will create a Linux compatible script?
Here is my build.gradle file:
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC3")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}
mainClassName="com.RestfulServer.Application"
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("com.fasterxml.jackson.core:jackson-databind")
compile 'org.slf4j:slf4j-api:1.7.6'
compile 'ch.qos.logback:logback-classic:1.1.1'
compile 'ch.qos.logback:logback-core:1.1.1'
// Local file dependencies
compile files('libraries/AWS/aws-java-sdk-1.7.2.jar',
'libraries/AWS/third-party/joda-time-2.2/joda-time-2.2.jar',
'libraries/AWS/third-party/httpcomponents-client-4.2.3/httpclient-4.2.3.jar',
'libraries/AWS/third-party/httpcomponents-client-4.2.3/httpcore-4.2.jar')
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
The distZip from the application plugin already creates shell scripts as well as .bat files. It should be in your zip file too.