Maven keeps overwriting my spring boot application.properties file? - maven

I am using the open source messenger4j project to create a spring boog based Facebook bot:
https://github.com/messenger4j/messenger4j-spring-boot-quickstart-template
I am running on Ubuntu 12.x and using Maven 3.5.0.
I need to use nano to edit the application.properties file of the Maven project. I make my edits, which include changing the server port and a few other SSL related items and the application.properties file looks like this:
messenger4j.appSecret = ${MESSENGER_APP_SECRET}
messenger4j.verifyToken = ${MESSENGER_VERIFY_TOKEN}
messenger4j.pageAccessToken = ${MESSENGER_PAGE_ACCESS_TOKEN}
logging.level.com.github.messenger4j=INFO
server.port=1919
... <other edits> ...
I then save the file, exit nano, and "cat" that file to make sure the edits are still there. They are. However, when I compile and run the messenger4j app with this maven wrapper command line:
./mvnw spring-boot:run
I notice that the status messages tell me that the embedded Tomcat server is not listening on port 1919 but instead on the default port 8080. I reopen the application.properties file and I see only the default content again and see this:
messenger4j.appSecret = ${MESSENGER_APP_SECRET}
messenger4j.verifyToken = ${MESSENGER_VERIFY_TOKEN}
messenger4j.pageAccessToken = ${MESSENGER_PAGE_ACCESS_TOKEN}
logging.level.com.github.messenger4j=INFO
Does anyone know what is causing this and how to fix this? It appears that Maven is overwriting the application.properties file from some other source each time it compiles and runs (I'm guessing here)?
I saw a stack overflow post talking about a command line flag that tells maven not to overwrite the application.properties file. But if there is another source file that Maven is using (or should use) to auto-generate the application.properties file, I would like to edit that file instead of using that flag and stay with the usual expected workflow of a Maven project. I also would rather not pass a boat load of parameters on the mvnw command line either if I can help it.
UPDATE: Added pom.xml file.
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.messenger4j</groupId>
<artifactId>messenger4j-spring-boot-quickstart-template</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>Facebook Messenger Chatbot Template using Java, Spring Boot, and messenger4j</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.messenger4j</groupId>
<artifactId>messenger4j</artifactId>
<version>0.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UPDATE: It turns out I was editing the ./target/classes/application.properties file instead of the file that Maven copies from to create that file. I'm referring to the true source file named ./src/main/resources/application.properties. Thank you dunni & Kyle.

Related

ApplicationContext not being recognized even after adding Maven dependency

I started a course on spring boot and I am at dependency injection.
I ran into a problem with ApplicationContext which I can't seem to resolve:
Screenshot of the problem
I added the maven dependency as you can see in the pom file:
pom file screenshot
But it still doesn't recognize the ApplicationContext and I cannot find a solution anywhere.
EDIT: whole pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>DependencyInjectionDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DependencyInjectionDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
You don't need to explicitly add spring-context dependency, since it is a transitive dependency of spring-boot-starter. Other than that it seems fine to me, but we can do three things:
Check that you are actually importing org.springframework.context.ApplicationContext and not some other class;
Force the reload of all your dependencies in IntelliJ (right click your project --> Maven --> Reload project);
Clear your .m2 folder (or at least org/springframework sub-path folder inside it). You might have some corrupted JARs in there.
From given screenshots I assume, you are using a Maven-project in IntelliJ.
Adding imports from classpath
Try to import the class org.springframework.context.ApplicationContext in the java class where your main method is (as shown in screenshot).
Usually IntelliJ should find the class ApplicationContext in your classpath. From your given POM and dependency spring-boot-starter the classpath should contain the class.
See:
ApplicationContext import in spring
Re-importing Maven dependencies
As João suggests try reimporting by context-action on your POM file (CTRL + SHIFT + O).
See:
Import Maven dependencies in IntelliJ IDEA
Maven dependencies | IntelliJ IDEA
Repair actions
In case nothing of the above work, either your IntelliJ project notconfigured correctly, or something got out of sync between IntelliJ and Maven. Then try following repair-actions (in order listed below):
Run mvn clean install -U (see --update-snapshots), either from IntelliJ's Maven tools or terminal, as explained in
Force re-download of release dependency using Maven
In IntelliJ's menu select Invalidate Caches / Restart... to reindex your project. This can help if IntelliJ and Maven got out of sync.
Delete your local Maven repository, usually the (hidden) folder .m2 in your user's home folder (see Clear the Maven Cache) and reload/reimport your POM in IntelliJ.

How to attach Spring source for java 11

I am having a difficult time attaching Spring 5.3.x source code in eclipse.
I tried multiple ways one of them was to add a source to Java build path. However, I am not able to find the spring 5.3.x source code itself.
I have searched the official spring site - https://spring.io/blog/2018/10/30/spring-boot-2-1-0
on git too https://github.com/spring-projects
none of them has source code.
Another was to Right-click on the project -> maven -> Download Sources. It started updating the project however I still don't see the source code.
Can anyone help me solve this?
Your current java runtime in eclipse seems to be jdk8.
Go to Eclipse -> Preferences -> Java -> Installed JREs and switch to JDK11.
Then delete the project and reimport it again.
There might be some other issues in your project, after you switch to JDK11, but in this case I would need more information, including your pom.xml.
Also, check your project outside the eclipse, with command-line: "mvn clean install". Make sure that it compiles successfully, before importing it to eclipse. Make sure your JAVA_HOME points to JDK11
Here is the example of pom.xml for JDK11 Spring Boot 2.4.2 project. Compare it with your own pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>skyglass</groupId>
<artifactId>notepad</artifactId>
<version>1.1.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>notepad</name>
<description>Notepad demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
.....
If nothing else helps, try to clean your local maven repository.
Deleting folder m2/repository/org/springframework should be enough

IDEA import of Maven GitHub projects not working/running -> "Cannot resolve symbol"

#### UPDATE v2 ####
Ok I found out the issue, it was in fact an IDEA bug. More precisely, it's the git extension, gitflowincrementalbuilder, which from 3.8+ breaks Idea. Changing version to 3.7 solves it, for now.
https://github.com/vackosar/gitflow-incremental-builder/issues/91
Intellij/git, please fix it
------ Old Update v1 -----
I just tried running the project with Eclipse... works perfectly without any issues whatsoever, at first try... So it's kind of a Intellij-IDEA bug/problem (...)
I am trying to run some examples from Github, the spring spring-boot ones from baeldung.com; more specifically this one (no one works in idea): https://github.com/eugenp/tutorials/tree/master/spring-mvc-simple-2
While it works using Maven commands, "mvn clean install"
and then "mvn spring-boot:run", wont work in Idea (it does clean and install ok, but no run). Project is imported using "New"->"Project from Existing Sources" (check images below for settings).
I think there is some problem with the pom imported configuration, especially since there is a multi module structure (parent tag); cannot even resolve #SpringBootApplication.
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-mvc-simple-2</artifactId>
<packaging>war</packaging>
<name>spring-mvc-simple-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
<finalName>spring-mvc-simple2</finalName>
</build>
I have been trying to add a SpringBoot configuration manually using IDEA gui but it doesnt recognize the application class (?). What partially works though is replacing the parent pom with:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Tests still wont work, but I dont think u're supposed to change/edit the pom file manually to make things works...
I have been trying alot already: invalidate cache, maven reimport & Generate sources and update folders, using mvn first, checking all jdk configuration... nothing works.
You didn't build the parent module so IntelliJ does not find this in your local Maven repository.
You should run mvn install in the project:
https://github.com/eugenp/tutorials/blob/master/parent-boot-2/pom.xml
But also exchanging the parent helps like you described yourself.
To make the tests run you have to add the test dependency from the parent:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>

How to deploy Multi module maven spring boot project in pivotal Cloud foundry?

I have a multi maven module spring boot project. Lets say, there are 2 modules in the parent 'service' and 'config'. Service depends on config. config module contains classes for DB configuration code ( cassandra db).
I added config module as dependency in my service module's pom. Sprinngboot application runs fine in local.
Parent pom has packaging as pom. submodules ie. Service and config have packaging as jar. I don't need to create any war as there is no webapp required. its just rest microservices.
To package the project, I executed'mvn package' at root pom level. It created jars for individual modules. service module is the one where main method is defined.
I created manifest file at service module level and deployed the jar of service module in cloud. App ran fine on cloud as well. I believe jar of service module implicitly contains required dependencies from config module as i have already defined config as dependency in service module's pom.
Next, in case I want to make some modification in config module only, would it still require creating the jar of service and deploy that in cloud even if there is no change in service?
Or
Is there a way i can deploy jar of config module only in cloud? if yes, how would I bind my service module with that in cloud to keep it functioning as before?
What is the recommended way for cloud deployment of multimaven project?
Edit:1
To make the question more clear and precise, I am looking for standard way of deployment in cloud. i am putting up the steps how I am doing the deployment at present and with reference to that what betterment i should do in the current steps.
I am having 3 pom.xml files. 1 parent and 2 childs. Also, config module is defined as dependency in service module's pom.xml
For deployment below steps i am taking-
Going to path of parent pom and running 'mvn package'
It creates respective jars in child modules ( 1 jar in service and, 1 jar in config)
creating a manisfest.yml file in service module and putting up path of service jar along with other required configuration in manifest file.
Logging into cloud foundry, navigating to path of service module where manifest .yml file is present and executing 'cf push'
It deploys and starts my app in cloud. All good!
Next, lets say i make modification in config module class code. How can I just deploy the jar of config module in cloud? Would my app still work?
Previously I had deployed only service module's jar and not the config module's jar. App runs fine. But in future if i make changes in config module only and i don't touch service module, would I still need to create jar of service as i did initially or there is a way i just re create jar for config module which is modified and deploy that only.
You need a parent project to hold it all together:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.greg</groupId>
<artifactId>boot-multi-module-example</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>service</module>
<module>config</module>
</modules>
</project>
You need a config project as a child of the parent, something like:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>boot-multi-module-example</artifactId>
<groupId>com.greg</groupId>
<version>1.0</version>
</parent>
<artifactId>config</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
And finally the boot application that you will deploy, with a dependency to the config service above:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>boot-multi-module-example</artifactId>
<version>1.0</version>
</parent>
<artifactId>service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tag::actuator[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.greg</groupId>
<artifactId>service</artifactId>
<version>${project.version}</version>
</dependency>
<!-- end::actuator[] -->
<!-- tag::tests[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- end::tests[] -->
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Next, lets say i make modification in config module class code. How can I just deploy the jar of config module in cloud? Would my app still work?
It's not possible to do a partial deploy to Cloud Foundry. By this I mean that you can't take one JAR file from your app and swap that into the existing application code bundle. You need to do a cf push every time you're changing code, no matter how small the change.
When you run cf push you are sending a "complete" bundle of application code to Cloud Foundry to run. If you change any part of that complete bundle, you need to cf push again. This may sound inefficient but cf push is smart and skip files that are already on the server, so you'll notice that subsequent pushes are smaller and faster.
You do still need to stage & restart the application though. This is unavoidable as you cannot change an application in-place. This would make a snowflake, or app that has diverged from it's droplet, and that's considered bad practice.
Long story short...
Rebuild the module that changed.
Rebuild the JAR/WAR that you are pushing to Cloud Foundry.
Push to Cloud foundry
Your changes will be deployed.

SpringBoot app shows Whitelabel Error Page while serving JSP from JAR

I am exploring how JSPs can be served from JAR file. I'm using SpringBoot 1.5.10.RELEASE
I referred this & this examples and was able to create spring boot app which
includes JSPs at location src/main/resources/META-INF/resources/WEB-INF/jsp/
view resolver has prefix as /WEB-INF/jsp/ and suffix as .jsp
Strange thing is when I ran project as springboot app or Java App, I was able to hit the controller and got desired JSP file as response. After that I thought of packaging the app and then running it from JAR.
$ mvn package
$ java -jar target/springboot-web-jar-demo-0.0.1-SNAPSHOT.jar
Spring boot app comes up, when I hit the same URL pattern, control comes in the mapped method but after that...
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Feb 28 21:01:09 IST 2018
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/index.jsp
My problem/question...
Is there any major difference between the springboot app runs in IDE and when it runs as JARs, which can even cause 404?
Does spring boot 1.5.10.RELEASE support serving JSPs from JAR? (if yes then what I'm missing here, if no then where it is mentioned) - stackoverflow-link
I have tried searching proper solution or answer to my problem but didn't get any. Hence posting the question here please help.
Note:
Please don't provide solution for WAR packaging
I know JSP are not best choice nowadays, I'm just learning
I'm aware that I can look for alternatives like thymeleaf, freemarker and velocity
I don't want to downgrade the SpringBoot version
I've also tried specifying <scope> of jasper & JSTL to provided and compile but no luck
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atuldwivedi.learn.springboot</groupId>
<artifactId>springboot-web-jar-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-web-jar-demo</name>
<description>Learn how to serve JSP from JAR</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
For you spring boot recognize their jsps, you debt create a webapp directory in src.main like this:
src/main/webapp/WEB-INF/jsp
and not like:
src/main/resources/META-INF/resources/WEB-INF/jsp
for dispatcherServelet recognize their jsps, you create a webapp directory in src.main like this:
src/main/webapp/WEB-INF/jsp
and also autoconfigure your application in application.properties
spring.mvc.view.prefix:/WEB-INF/jsp
spring.mvc.view.suffix:.jsp`
JSPs are not supported when using an executable jar.

Resources