简体   繁体   中英

What are best practices for Spring Boot application organization?

I have been developing Spring Boot applications using Eclipse as the IDE (not that I think that's particularly relevant) and all test executions up to this point have been done by launching it from the IDE. This works fine for development, but now the application needs to be "deployable" (ie packaged up in some way, placed on a test system, unpacked, configured and run).

Are there recommended or "best practices" ways a deployed application should be organized?

Should the application resources be organized into a certain directory structure that is "known" so that when it's launched, it can find everything it needs? Are there resource files that must be named a particular way?

Current Situation

Toward this goal I created a shell script (Linux is the target OS) to launch the program and modified my maven build to collect together the necessary resources (eg jar dependencies, application.yml, log4j2.xml, etc.). All of this is zipped up and placed onto the target test Linux box.

When the archive is unpacked, nearly everything sits in the "root". When I've attempted to execute the program, the program does execute (and quickly exits with errors), however there are issues that I think could be due to the run-time not having correct access to the resources it needs. This is just a guess.

Here's an example:

Unpacked zip:

[centos@route-assessor route-assessor]$ ls -l
total 41016
-rw-r--r--. 1 centos centos     1017 Oct 21 11:34 application.yml
-rw-r--r--. 1 centos centos  5059429 Oct 21 15:05 elastic-apm-agent-1.10.0.jar
drwxr-xr-x. 2 centos centos     8192 Oct 22 10:38 lib
-rw-r--r--. 1 centos centos     1975 Oct 22 15:12 log4j2.xml
drwxrwxr-x. 2 centos centos       71 Oct 22 15:12 logs
drwxrwxr-x. 2 centos centos       62 Oct 22 15:12 metrics
-rwxr-xr-x. 1 centos centos      740 Oct 22 15:09 run-route-assessor.sh
-rw-r--r--. 1 centos centos 36909394 Oct 22 10:38 services-0.0.1-SNAPSHOT.jar

UPDATE :

OK, perhaps "deployment" is not the term I should have used. I'm not planning on "deploying" or "installing" the application onto a machine, I just will place the zip archive on it, unzip it, do some configuration and run it.

With that said, my question pertains to whether (or not) there are recommended practices regarding how the artifacts in the unzipped result should be organized.

For example, in the eclipse project, the project is structured to be Maven "friendly" and it appears that Spring must expect this structure (or perhaps something similar).

So:

<project>
  src/
    main/
      java/
        <java code contained in a package hierarchy>
      resources/
        <various needed resource files>
        log4j2.xml
        application.yml
        etc.

When an application is "packaged up", should a directory structure like this be maintained? In other words, will the JVM/Spring Boot "runtime" expect to see resources organized like this?

The spring-boot docs describe installing to Linux here . The resulting single artifact from the maven build has the dependency libraries and resources where spring-boot looks for them.

Should the application resources be organized into a certain directory structure that is "known" so that when it's launched, it can find everything it needs? Are there resource files that must be named a particular way?

You don't unpack anything to run the application. . . what's in the jar stays in the jar. That's what the maven plugin (mentioned in that link) does.

Once, you have the build artifact on the server, you need to add configuration (eg db user/pass, etc.) and integrate it to centos's service management, which that doc also describes.

That doc also describes how to ship your application to a cloud platforms, if that's your ultimate goal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM