I build my spring boot application to .jar file with:
./gradlew build
and when I want to run this app:
java -jar /build/libs/app.jar
I get error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.mongodb.host' in string value "${spring.data.mongodb.host}"
etc.
When I run this app in IntelliJ everything is ok.
My application.properties
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=newsAdminUser
spring.data.mongodb.password=abc123
spring.data.mongodb.database=newssystemservice
server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain
What's the problem?
Related
I am newbie with Springboot .I am unable to make jar file of Springboot with Mysql to deploy in AWS using Eclipse IDE.
I create an application which work perfectly fine in Localhost and when i want to deploy it into the AWS I comment all my application.properties file which has code like
#spring.jpa.hibernate.ddl-auto=update
#spring.datasource.url=jdbc:mysql://localhost:3306/db_digitalprofile?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
#spring.datasource.username=root
#spring.datasource.password=
#server.port=9090
#spring.jpa.show-sql = true
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
and make a new file named as application-prod.properties in src/main/resource with the code:
server.port=5000
spring.datasource.url=jdbc:mysql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
spring.datasource.username=${RDS_USERNAME}
spring.datasource.password=${RDS_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
Now when i want to make a jar file from right click on project> Run As>Maven install to create a jar file it throw a error like:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
java.lang.IllegalStateException: Failed to load ApplicationContext
I had also add following dependency in pom.xml
<configuration>
<finalName> digitalProfile</finalName>
</configuration>
Failed to determine a suitable driver class
You need to check the dependencies. No driver found meaning, driver jar is missing which will be used to connect to Database. mysql driver should be present in your pom file or whatever your build file is.
Add this on your application.properties and it should fix your issue.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
I would like to just use GitLab CI to run test only not to deploy my app. I manage to assembly this .yml file:
image: java:8
stages:
- build
- test
build:
stage: build
script: ./gradlew build
artifacts:
paths:
- build/libs/myApp-4.0.0-SNAPSHOT.jar
unitTests:
stage: test
script:
- ./gradlew test
And in the GitLab Pipeline I get the following error:
ar.com.sebasira.myApp.myAppApplicationTests > contextLoads FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException
I'm guessing this could be related to database, right? I need to provide that database with credentials in my Server where the runner is?
If so, how should I do it? I'currently using application.properties file to define the connection to the DB.
And one more question... in the .gitlab-ci.yml file I need to put the path to the .jar but that filename will change everytime I update the version of my app. Do I need to manually change it?
As you are starting the whole Spring context with this test (I think it's the generated standard test from Spring Boot) with the annotations #RunWith(SpringRunner.class) and #SpringBootTest you have to provide a datasource. You can do one of the following:
Specify the database credentials in your application.properties in src/test/resources (you could provide a test database on your database server as I wouldn't connect to your production database on every test)
Use an embedded H2 for this test
Use https://www.testcontainers.org/ to provide a real and fresh database for your Spring application test
Regarding your GitlabCI question: Just use * to match any .jar no matter which version: - build/libs/myApp-*.jar
I have 2 spring boot apps, and I want one of them to extend the configuration properties of the other. Everything works fine in Eclipse when I run the project as a maven spring boot app, however, when I run the jar or mvn spring-boot: run, I'm getting:
org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class [com.xyz.integration.app.my.Application]; nested exception is java.io.FileNotFoundException:
class path resource [com/xyz/integration/app/pdfthing/ConfigurationManager.class] cannot be opened because it does not exist
I've searched all over and tried many variations, including:
#ComponentScan (using both com.xyz.. and classpath)
#Import
Class-Path in Manifest to the external jar file (In Linux, how to execute Java jar file with external jar files?)
Here's part of the configuration class:
#Component
#ComponentScan("classpath:com/xyz/integration/app/pdfthing")
public class MyConfigurationManager extends ConfigurationManager{
I'm guessing running as a jar doesn't load the external jar in the configuration. Any thoughts/hints? Thanks!
in my Spring Boot project i have the following fragment in gradle which exports gradle properties to Spring Environment.
processResources {
filesMatching("**/application.properties") {
expand(project.properties)
}
}
My application.properties looks like this (snippet)
app.version=${jar.version}
Works pretty well. I can work with the gradle propeties in Spring classes with #value and even can access them in thymeleaf with
th:text="${#environment.getProperty('app.version')}
But now the issue: When i run the same project with "gradle bootRun". I am getting this:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jar.version' in string value "${jar.version}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
I assume its something about the way gradle's bootRun works by not using processResources or something like that?
The question would be: how can i get this to work. I dont want to give up using bootRun.
Specify a default value to use when the real one cannot be found?
#Value("${...:defaultValue}")
I updated a project that was using struts 2.2.3.1 to the 2.3.1.2 in the pom.xml file. After running maven it picked up the jar from the repo and all that and built the package just fine. But, when I run the webapp I am getting this error:
2012-04-26 13:42:06,300 ERROR org.apache.struts2.dispatcher.Dispatcher.error:38 - Dispatcher initialization faile Unable to load configuration. - bean - jar:file:/Library/Tomcat/apache-tomcat-6.0.35/webapps/idmadmin/WEB-INF/lib/struts2-core-2.3.1.2.jar!/struts-default.xml:29:72.....
I was thinking that maybe the old one was still stuck in the classpath but I don't see it.