Próbuję zrobić deploy aplikacji Spring Boot MVC za pomocą jara. Tworzę jat komendą mvn. Kiedy jestm w katalogu głównym aplikacji, to jar działa i wyświetla się strona. Ale jak przeniosę tego jara do zupełnie innego katalogu, poza projekt, to po odpaleniu pokazuje się błąd 404 w przeglądarce.
Chciałbym zrozumieć, czemu się tak dzieje. Czy to wina aplikacji, czy niewłaściwie tworzę jara.
Sklonowałem tę aplikację z githuba: https://github.com/spring-guides/gs-rest-service-cors/tree/master/complete
( To jest w ogóle kod do jednego tutoriala Springa Boot )
Poniżej widać utworzenie jara, odpalenie jara, sprawdzenie, czy działa curlem. Widać, że działa. Jesteśmy w katalogu głównym aplikacji.
u@apu ~/gs-rest-service-cors/complete
$ ./mvnw clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.example:rest-service-cors >--------------------
[INFO] Building rest-service-cors 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ rest-service-cors ---
[INFO] Deleting /home/u/gs-rest-service-cors/complete/target
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ rest-service-cors ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /home/u/gs-rest-service-cors/complete/src/main/resources
[INFO] skip non existing resourceDirectory /home/u/gs-rest-service-cors/complete/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ rest-service-cors ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/u/gs-rest-service-cors/complete/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ rest-service-cors ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ rest-service-cors ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ rest-service-cors ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ rest-service-cors ---
[INFO] Building jar: /home/u/gs-rest-service-cors/complete/target/rest-service-cors-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.2:repackage (repackage) @ rest-service-cors ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.012 s
[INFO] Finished at: 2021-03-11T02:41:56+01:00
[INFO] ------------------------------------------------------------------------
u@apu ~/gs-rest-service-cors/complete (master=)
$ java -jar target/rest-service-cors-0.0.1-SNAPSHOT.jar &
[1] 2441
u@apu ~/gs-rest-service-cors/complete (master=)
$
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-11 02:44:40.095 INFO 2441 --- [ main] c.e.r.RestServiceCorsApplication : Starting RestServiceCorsApplication v0.0.1-SNAPSHOT using Java 1.8.0_241 on apu with PID 2441 (/home/u/gs-rest-service-cors/complete/target/rest-service-cors-0.0.1-SNAPSHOT.jar started by u in /home/u/gs-rest-service-cors/complete)
2021-03-11 02:44:40.100 INFO 2441 --- [ main] c.e.r.RestServiceCorsApplication : No active profile set, falling back to default profiles: default
2021-03-11 02:44:41.645 INFO 2441 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-11 02:44:41.667 INFO 2441 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-11 02:44:41.667 INFO 2441 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-11 02:44:41.752 INFO 2441 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-11 02:44:41.752 INFO 2441 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1546 ms
2021-03-11 02:44:42.073 INFO 2441 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 02:44:42.224 INFO 2441 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: ServletContext resource [/index.html]
2021-03-11 02:44:42.429 INFO 2441 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 02:44:42.452 INFO 2441 --- [ main] c.e.r.RestServiceCorsApplication : Started RestServiceCorsApplication in 2.954 seconds (JVM running for 3.697)
$ curl localhost:8080
2021-03-11 02:45:36.328 INFO 2441 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-11 02:45:36.328 INFO 2441 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-03-11 02:45:36.329 INFO 2441 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
<!DOCTYPE html>
<html>
<head>
<title>Hello CORS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="hello.js"></script>
</head>
<body>
<div>
<p class="greeting-id">The ID is </p>
<p class="greeting-content">The content is </p>
</div>
</body>
</html>
u@apu ~/gs-rest-service-cors/complete (master=)
$
Teraz zabijamy proces serwera, kopiujemy jara poza projekt i odpalamy jara. Curl pokazuje status 404:
$ kill `lsof -ti:8080`
2021-03-11 02:47:21.173 INFO 2441 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
u@apu ~/gs-rest-service-cors/complete (master=)
$ cp target/rest-service-cors-0.0.1-SNAPSHOT.jar ~/target/
[1]+ Exit 143 /home/u/opt/jdk/bin/java -jar target/rest-service-cors-0.0.1-SNAPSHOT.jar
u@apu ~/gs-rest-service-cors/complete (master=)
$ cd ~/target/
u@apu ~/target
$ curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
u@apu ~/target
$ java -jar rest-service-cors-0.0.1-SNAPSHOT.jar &
[1] 2585
u@apu ~/target
$
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-11 02:51:35.644 INFO 2585 --- [ main] c.e.r.RestServiceCorsApplication : Starting RestServiceCorsApplication v0.0.1-SNAPSHOT using Java 1.8.0_241 on apu with PID 2585 (/home/u/target/rest-service-cors-0.0.1-SNAPSHOT.jar started by u in /home/u/target)
2021-03-11 02:51:35.649 INFO 2585 --- [ main] c.e.r.RestServiceCorsApplication : No active profile set, falling back to default profiles: default
2021-03-11 02:51:37.352 INFO 2585 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-11 02:51:37.373 INFO 2585 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-11 02:51:37.373 INFO 2585 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-11 02:51:37.452 INFO 2585 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-11 02:51:37.452 INFO 2585 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1704 ms
2021-03-11 02:51:37.762 INFO 2585 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 02:51:38.047 INFO 2585 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 02:51:38.066 INFO 2585 --- [ main] c.e.r.RestServiceCorsApplication : Started RestServiceCorsApplication in 2.999 seconds (JVM running for 3.741)
$ curl localhost:8080
2021-03-11 02:52:53.748 INFO 2585 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-11 02:52:53.748 INFO 2585 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-03-11 02:52:53.751 INFO 2585 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
{"timestamp":"2021-03-11T01:52:53.825+00:00","status":404,"error":"Not Found","message":"","path":"/"}u@apu ~/target
$
Ta różnica zachowania jara zależy od tego, w którym katalogu odpalamy java -jar ...
. Działa dobrze tylko, jak odpalamy w katalogu głównym aplikacji.
Rozwikłanie tego jest mi potrzebne, by robić deploy aplikacji webowych.
Cały projekt widać na githubie, ale wkleję tu pom.xml, może komuś ułatwi oglądanie
Z góry dziękuję
<?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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rest-service-cors</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-service-cors</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>