I have created a Spring Boot application with the following build.gradle file (Gradle version 5.4.1):
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
bootJar {
launchScript()
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '2.1.6.RELEASE', ext: 'pom'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.6.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-orm', version: '5.1.7.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.hibernate', name: 'hibernate-hikaricp', version: '5.3.10.Final'
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
}
When I try creating a JAR using ./gradlew bootJar and running it using the following command sudo service project start, the following error message shows up:
Failed to restart project.service: Unit project.service not found.
I have added the symlink to the JAR file in /etc/init.d, still it displays the above error when starting. What am I missing here?
Try add :
springBoot {
executable = true
}
I am using maven with :
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
Check if jar file permission is set executable e.g. (755)
Try to start ./$YOURAPP.jar to test if your build was correct
first check if the project.service exists
sudo vim /etc/systemd/system/project.service
if it doesnt you may need to make one.
The contents of this file looks something like:
https://www.baeldung.com/spring-boot-app-as-a-service
dont forget to make the script executable with chmod +x
after you make the service you need to run the folowing comantds to get it to work:
sudo systemctl restart rsyslog
sudo systemctl enable project.service
sudo service project start
sudo service project status
Related
I have created a relatively new project with 4 modules inside one project (created with intelij). However, I have now got to the stage where I want to import one module (:domain) from another (app-updater). The issue I'm facing is when I do this from within intelij it gives me errors in the "Build: Sync" tab. This is strange since my module app-updater is able to reference to module domain when I add the line "compile project(':domain')" into build.gradle and when I remove the line I get issues with the class being missing so the build: sync seems to be working it's just running it from inside intelij seems to give an error saying "Project with path ':domain' could not be found in root project 'app-updater'." I have attached my gradle config below, hopefully I have given enough info but if not feel free to ask for more, thanks for any help.
Main project settings.gradle file
rootProject.name = 'rGuide'
include 'domain'
include 'service'
include 'app-updater'
include 'app-rest'
:domain build.gradle
group 'domain'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.9'
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
app-updater build.gradle file
group 'app-updater'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
google()
maven {
url 'https://repo.spring.io/plugins-release/'
}
maven {
url 'https://repo.spring.io/milestone'
}
}
dependencies {
compile project(':domain')
// https://mvnrepository.com
compile group: 'net.ser1', name: 'gozirra-client', version: '0.4.1'
compile group: 'io.netty', name: 'netty-all', version: '4.1.36.Final'
compile group: 'io.projectreactor.netty', name: 'reactor-netty', version: '0.9.0.M2'
compile group: 'org.springframework.integration', name: 'spring-integration-stomp', version: '5.1.6.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-core', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-websocket', version: '5.1.6.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.9'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
TLDR - Trying to import domain into app-updater within intelij, it "works fine" and seems to import domain however when I do this with the Build: sync tab in intelij it gives an error saying "Project with path ':domain' could not be found in root project 'app-updater'.". Thanks for any help!!
Migrating from Spring Boot 1.5.8 to 2.1.5, getting the error message:
Could not set unknown property 'sourceCompatibility' for project ':api' of type org.gradle.api.Project.
If I comment that line, I get the error message:
Could not set unknown property 'targetCompatibility' for project ':api' of type org.gradle.api.Project.
If I comment that line, I get the error message:
Could not find method jar() for arguments [build_4wobgm6qykoy29e0in3cntga8$_run_closure2#1fc9b06d] on project ':api' of type org.gradle.api.Project.
So something is just not right here. I have two build.gradle files, one in my root, one in api:
/build.gradle
plugins {
id 'idea'
id 'java'
id 'com.jfrog.bintray' version '1.8.4'
}
apply from: "$rootDir/gradle/git-version.gradle"
version getVersionFromGit()
group 'com.my_org.my_proj'
apply from: "$rootDir/gradle/bintray-vars.gradle"
subprojects {
repositories {
jcenter()
mavenCentral()
}
}
wrapper {
gradleVersion = '4.8.1'
}
/api/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}
group = rootProject.group
version = rootProject.version
repositories {
maven { url 'https://dl.bintray.com/my-org/spring-utils' }
}
apply from: "$rootDir/gradle/checkstyle.gradle"
sourceCompatibility = 1.8
targetCompatibility = 1.8
jar {
archiveName = 'api.jar'
baseName = project.name
version = project.version
}
test {
doFirst {
environment "BUILD_NUMBER", "1"
}
}
dependencies {
def springBoot = '2.1.5.RELEASE'
runtime group: 'org.springframework.boot', name: 'spring-boot-properties-migrator', version: springBoot
compile group: 'org.flywaydb', name: 'flyway-core', version: '5.2.4'
compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.4.3.Final'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker', version: springBoot
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBoot
testCompile group: 'org.flywaydb.flyway-test-extensions', name: 'flyway-spring-test', version: '4.2.0.2'
}
I'm clearly missing something here. I've been following the migration guide as well as Googling, of course. Suggestions?
You haven't applied the java plugin to your api sub-project so the sourceCompatibility and targetCompatibility properties that it adds to the project are missing as is the jar task that it defines.
Adding id java to the plugins block at the top of api/build.gradle should fix the problem.
I am trying to build fat jar, but the jar is giving me error. I have built fat jar before with javaFx 11 and java 11. Now I am using Java 8 and Spring I am doing it with intellij from File>Project Structure>Artifacts, there I choose ProjectRoot main module and then also add my resources directory content and I build the jar.
[main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.boo t.enableautoconfiguration' in any property source
21:31:42.635
[main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
at org.springframework.util.Assert.notEmpty(Assert.java:450)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:160)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:96)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.process(AutoConfigurationImportSelector.java:386)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:828)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:563)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at com.tick42.QuicksilverApplication.main(QuicksilverApplication.java:13)
My project is:
>ProjectRoot
>src
>main
>resources
>java
>com
>projectId
>restaurant
RestaurantApplication.class
In restaurant folder are all my config and project classes.
My gradle build is at the project root:
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
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'
apply plugin: 'jacoco'
group = 'com.projectId'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-security")
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.2.RELEASE'
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.6.0'
compile group: 'com.mchange', name: 'c3p0', version: '0.9.5.2'
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.0.8.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '5.3.5.Final'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
compile group: 'com.googlecode.log4jdbc', name: 'log4jdbc', version: '1.2'
compile "org.springframework.boot:spring-boot-configuration-processor"
compile 'org.passay:passay:1.3.1'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.kohsuke', name: 'github-api', version: '1.93'
}
jacocoTestReport {
reports {
xml.enabled = true
html.enabled = true
}
}
check.dependsOn jacocoTestReport
settings.gradle:
rootProject.name = 'server-side'
My RestaurantAplication.class is was in my restaurant folder, but I read somewhere that it should be above my package with my classes, so I moved it to the projectId folder, but it didn't work.
My main:
#SpringBootApplication
#EnableScheduling
#EnableConfigurationProperties
public class RestaurantApplication {
public static void main(String[] args) {
SpringApplication.run(RestaurantApplication.class, args);
}
}
Edit: in the jars's spring.factories in the META-INF folder there is only one line:
org.springframework.beans.BeanInfoFactory=org.springframework.beans.ExtendedBeanInfoFactory
I tried adding them to the file also adding some of the ones listed in the error and also my config files:
org.springframework.beans.BeanInfoFactory=org.springframework.beans.ExtendedBeanInfoFactory
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector
org.springframework.context.annotation.ConfigurationClassParser
com.projectId.restaurant.config.AppConfig
com.projectId.restaurant.config.PasswordEncoderConfig
com.projectId.restaurant.config.ScheduleConfig
com.projectId.restaurant.config.Scheduler
com.projectId.restaurant.config.SecurityConfig
But even then the lines in the error still exist for the exact configurations I have added:
...
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:160)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:96)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.process(AutoConfigurationImportSelector.java:386)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:828)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:563)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)
...
I'm using Spring Boot for a school project as a personal choice but the automated tester uses ./gradlew appRun to start the whole thing up.
This was fine before when they were using servlets but after moving to Spring Boot, I'm getting several exceptions when trying to do so.
Execution failed for task ':appRun'.
Could not get unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension.
and this is my current build.gradle.
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath "gradle.plugin.org.akhikhl.gretty:gretty:2.0.0"
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'org.gretty' version '2.2.0'
id 'war'
}
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "org.akhikhl.gretty"
apply plugin: 'application'
sourceCompatibility = 8
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
runtimeOnly('com.h2database:h2')
runtimeOnly('mysql:mysql-connector-java')
runtimeOnly('org.hsqldb:hsqldb')
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.7'
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile group: 'org.apache.ibatis', name: 'ibatis-core', version: '3.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
compile group: 'org.apache.ibatis', name: 'ibatis-core', version: '3.0'
compile group: 'org.gretty', name: 'gretty-runner-jetty94', version: '2.2.0'
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
providedRuntime ('org.springframework.boot:spring-boot-starter-tomcat')
runtimeOnly('mysql:mysql-connector-java')
compileOnly('org.projectlombok:lombok')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
gretty {
contextPath = '/'
}
This all works fine when using ./gradlew bootRun but that doesn't pass the automated tester.
Is there a way to make running ./gradlew appRun start up the SpringBoot main class similarly or directly ./gradlew bootRun?
This is a gretty question. Add this line to gretty config:
gretty {
contextPath = '/'
springBoot = true
}
Here's the doc: http://akhikhl.github.io/gretty-doc/spring-boot-support.html
In build.gradle I am building WAR for tomcat and wildfly by single script. Wildfly has provided dependency to "javax.mail:mail:1.4.7'. But tomcat is missing this jar. So I have always added this jar to ${CATALINA_HOME}/lib/ . Currently I am trying to migrate from both of them to Amazon AWS Elastic Beanstalk and I don't want to mess with ${CATALINA_HOME}/lib/. How to write universal gradle script for wildfly with:
dependencies {
....
providedCompile group: 'javax.mail', name: 'mail', version: '1.4.7'
providedCompile group: 'javax.activation', name: 'activation', version: '1.1.1'
...
}
and for tomcat with:
dependencies {
...
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
compile group: 'javax.activation', name: 'activation', version: '1.1.1'
...
}
I am not an expert with gradle.
#RaGe solved my problem. Code below is an ultimate solution as "42" number.
configurations {
tomcatLibs
}
dependencies {
...
providedCompile group: 'javax.mail', name: 'mail', version: '1.4.7' //provided for wildfly
providedCompile group: 'javax.activation', name: 'activation', version: '1.1.1' //provided for wildfly
tomcatLibs group: 'javax.mail', name: 'mail', version: '1.4.7' //only for tomcat
tomcatLibs group: 'javax.activation', name: 'activation', version: '1.1.1' //only for tomcat
providedCompile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.0'
....
}
//default war for wildfly
war {
....
}
task createTomcatWar(type: War, dependsOn: classes) {
archiveName = 'app-tomcat.war';
classpath configurations.tomcatLibs // adds a configuration to the WEB-INF/lib dir
}
....
Add an additional config to hold the dependency who's scope changes:
configurations {
optLibs
}
add the dependency to the config just created:
dependencies{
...
optLibs 'javax.mail:mail:1.4.7'
optLibs 'javax.activation:activation:1.1.1'
compile 'foo'
runtime 'bar'
...
providedCompile.extendsFrom(optLibs)
}
Now for a task that builds a war with optLibas as compile:\
task createTomcatWar(type: War, dependsOn: classes) {
baseName = 'app-wildfly'
destinationDir = file("$buildDir/dist")
classpath = configurations.optLibs //This should include optLibs jars in WEB-INF/lib
}
The standard war task builds a war without the optLibs included, so that can be your wildfly war, you don't need another explicit task. If you want your custom task to run automatically everytime you build, you can also add to the rootlevel of your build.gradle:
assemble.dependsOn createTomcatWar