Java Spring-boot: Problem to finde resources - java

I have a Spring-boot backend Angular front end application. The application runs well if it is run on port 4200 (Running frontend from the front end). But I have done the integration an I run in to troubles. The application after the following commands:
mvn clean install
npm install
npm build
Properly generate the dist folder on the front end and copy them in the source folder (project/src/main/resources/static).
When I try to run the application from the back end (definded port 8999) the application finde the index.html file in static folder but cannot find the other scripts. The application start looking for those resources on localhost:8999/resourcefile
instead of looking for it in
localhost:8999/src/main/resources/static
Do anyone have any idea?

For anyone with the same problem. There was declared
<base href="/">
in my index.html and that's why it was looking for resources from the wrong directory.

Related

Spring boot basic guide: How to create complete folder?

I'm creating basic spring boot application as in this guide
If I download their completed-guide-code, I get the complete folder to run and test app.
But if I do practice as start from scratch, install prequisite, go to start.spring.io and config, generate starting project, create web app, create app class, and go to step Run the Application, I cannot find the folder complete as in guide, so I can't run and test app. Maybe I should run some build command to create it? So how to create it?
Please just run mvn spring-boot:run (use mvnw if maven is not installed or not on path. Spring boot project contains mvnw executable) in the project root without the directory specifier.
./ could be used for unix like systems but not windows.
complete folder is just the root project name in the guide. In your case, the root project name is spring-boot, go to spring-boot directory and run ./mvnw spring-boot:run.

Deployed webApp on tomcat macOS return 404

I'm trying to deploy my fullstack webApp to tomcatServer. Backend part is springBoot app and frontend part is react app. I built that using mvn clean package. I configured my pom file, so maven always runs npm i, npm run build and then it copies all files to webapps backend directory and create war file.
When I have installed the tomcat on the windows, everything works correctly. I have deployed the webApp.war to the tomcat and I can access to my webapp using localhost:8080/webApp.
I tried the same on the mashine with macOS, but localhost:8080/webApp does not work. There is shown whitescreen and all needed files return 404 code to the console.
Do you have some ideas, what is needed to be configured?
Thanks!
Have you tried manually stopping the server(tomcat), again setting it to automatic and re-running the application?

Spring boot Angular Application project runs compiled html... how do I 'recompile'

Setting: I inherited a project that I need to update the UI for
I have a spring boot application that has an angular front end.
If I make UI changes in the front end, code at src/main/web/, they do not appear in the application that launches when I run mvn spring-boot:run
The application refers to files in the src/main/resources/static/ instead. This folder seems to contain 'compiled` front end scripts.
Question
I am trying to recompile src/main/web/ files into the src/main/resources/static/, how do I do that?
Notes
I have tried
bower install
npm install
mvn install
mvn clean
They have grunt here. Running the grunt serve command will show my updated front end files, this, in a sense "works", but mvn spring boot runs the full application and it is necessary that this command works.
An ls of the main directory shows:
Gruntfile.js bower_components package-lock.json pom.xml src
README.md package.json swagger.json
bower.json node_modules packageOld.json target
The code is being compiled by grunt, not bower or npm or mvn.
grunt clean build

Spring boot + angular 2 Heroku deployment

I have a big issue. I'm trying to deploy Spring Boot + Angular 2 web app on heroku but don't know how to do it. I tried several things including:
Making a .war file and deploying it to heroku (source here)
Deploying project as standard java application (source here)
but none of these worked. The first attempt didn't work because I constatly got 404 not found, and the second one didn't work due to, I think, some jar file wasn't found in the location which was described in the Procfile.
Can anyone give me a link, an example, or write a step by step instruction how to achieve this. Thank you.
The most simple way to do it:
run ng build in angular 2 project root (if you are using angular-cli) and copy the content of dist folder to src/main/resources/static/.
create Procfile (for maven):
web: java $JAVA_OPTS -Dserver.port=$PORT -jar target/*.jar
commit and push changes.
Also, you need spring-boot-starter-web present in dependencies. Which has embedded tomcat and automatically configured to serve static content from the static folder.
If you deploy your app as a standard Java application, you can combine it with the Node.js buildpack to run ng build during the Heroku build.
$ heroku buildpacks:add heroku/nodejs
$ heroku buildpacks:add heroku/java
$ git push heroku master
The Node.js buildpack will detect your package.json, install Node.js and run npm. Then the Java build can proceed as normal.
There is a guide for doing something very similar but with Grunt: Using Grunt with Java and Maven to Automate JavaScript Tasks
Use JHipster: https://jhipster.github.io
Once installed, run:
$ yo jhipster
Then run
$ yo jhipster:heroku

How to Dockerize a tomcat app

I am trying to dockerize some Tomcat application but I never touch Java application before so the lack of understand it makes it really hard to understand what should I do.
So far I have this but it doesn't work and I don't if it's the correct path as well
FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk
COPY . $APP_ROOT/
WORKDIR $APP_ROOT
RUN jar -cvf app_name.war *
# this fail for some reason, when I do `ls` the file is there but if fail to copy it
COPY app_name.war $CATALINA_BASE/webapps/app_name.war
I am just going on loop on this because I don't understand and Google Search do not help me that much (I don't know how to ask).
Should I use the jar command in the build? If not, I guess I have to build it locally and just make sure that the .war is there right?!
How the building of the Java with Tomcat app works? and How to integrate with Docker?
Sorry for being too generic but I don't understand anything about Java
Looking at your code this is what I could gleam:
You have some java files stored in current directory (.)
When you call COPY you copy all these contents to /app_name
You create a .war on the file
There are some things to note, first is that the app_name.war is not on the host disk, it is currently inside of the docker file system. What this means is that you cannot COPY the .war.
What you are really after is this: RUN cp app_name.war $CATALINA_BASE/webapps/app_name.war
This would look like the following:
Dockerfile
FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk
COPY . $APP_ROOT/
WORKDIR $APP_ROOT
RUN jar -cvf app_name.war *
RUN cp app_name.war $CATALINA_BASE/webapps/app_name.war
Adding the docker COPY reference here as it explains the command in detail. It might also be helpful for you to make a script called provision.sh, then do something like:
COPY provision.sh /tmp/provision.sh
RUN sh /tmp/provision.sh
That way you can put all your building, configuring and other in a single script that you can test locally (again if it helps)
EDIT: Adding mention about building locally and copying into dockerfile
You can build the .war on your machine, use COPY to put is on the machine.
Dockerfile
FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk
COPY app_name.war $CATALINA_BASE/webapps/app_name.war
WORKDIR $APP_ROOT
The above copies the file app_name.war then add it to the filesystem of the container at the path $CATALINA_BASE/webapps/app_name.war. So for that you do this:
Build the .war on your machine with java
Put .war in directory with Dockerfile
COPY app_name.war into the container's filesystem
You can try to do this "by hand" before trying to automate it, it should help to understand the process. You don't need to extend a tomcat official image to be able to deploy a war on a dockerized tomcat, you can use the image directly if you don't need to customize permissions and users (in production, you need).
If you need Tomcat 6.x because your webapp implements servlet API < 3, do this :
sudo docker run --name tomcat --detach --port 8080:8080 tomcat:6
Now, your Tomcat is running in background (--detach), waiting for a deployment. You've exported port 8080 from the container and mapped it to port 8080 from you host, so the app will be available at http://localhost:8080/ on your host.
From now if you copy your .war in /usr/local/tomcat/webapps into the container, the app will be deployed :
sudo docker cp app_name.war tomcat:/usr/local/tomcat/webapps/
I don't use docker, I use a similar AWS product called codedeploy for provisioning instances, so I tell you what I do for Tomcat setup in my provisioning scripts. Should be easy to port to docker as just bash comands.
1) Build the WAR
Most java applications these days are built using Maven but Gradle is catching up. Maven and the WAR plugin are used to turn java code into a WAR file which you can deploy on Tomcat. But it looks like you already have the WAR built by someone else? Either way, you dont run the war directly, you put it in Tomcat, unless you've bundled Tomcat into the app, in which case it would be a JAR but lets not talk about that....The simple solution is build the war from java code using a build tool like Maven or Gradle. By build, I mean turn it from source code to binary.
2) Install Tomcat
yum install tomcat6,7,8 etc etc (Whichever version you need)
Then turn it on
service start tomcat8
3) Deploy the war
To run the war place it in the webapps folder of the Tomcat installation. I generally like to shut tomcat off when I do this but you can do it while its running. After a few seconds the WAR, which is really just a zip file, will be exploded/unzipped to create a directory.
4) Accessing the application/site
If you rename your war to ROOT.war then you can access the applicaition at http://localhost:8080 if your configuration is to have it listen on 8080. If you war is named pets.war then your webapp URL would be http://localhost:8080/pets. You configure which port for Tomcat to listen on in the server.xml file in its conf folder.
Most Important
Tomcat documentation is very good once you know what to look for. The primary configuration files are web.xml, context.xml, and server.xml. The central tomcat guides explain each component https://tomcat.apache.org/tomcat-7.0-doc/setup.html you just need to find the doc that corresponds to your version of Tomcat.

Categories