Share static files in multiple War projects. Share static files in war projects.
[Original statement: the article is original. You are welcome to repost it for non-profit purposes, but the source must be indicated in the reposted statement]
Internet products generally have multiple projects (Jar and WAR) to form a product line. Because these WAR projects use the same front-end architecture (such as jQuery and easyui), the Code of these components and the public resource files to be used in other projects exist in each project.
Traditionally, copy these files in each War project and package them into each War. If these components are very stable and rarely upgraded, and there are almost no bugs that need to be modified, this is not a problem. However, in the opposite case, the front-end staff will go crazy, because they need to modify and debug each project, and remember which projects have been modified and which have not been modified.
Is there any better solution to this problem?
1. static files are independent from the War package.
No matter which solution is used in the future, this is a required step. The difference lies in the method used to form an independent module for independent static files.
The typical static directory content in our project is as follows. Other projects are similar.
As you can see, the Code including jquery \ easyui will be repeated in each project.
2. Develop static files as independent jar packages
After the resource files are independent, use an independent jar package for development. Other war projects depend on this jar package.
2.1 static file jar Project
Create a static directory in the src/main/resources Directory of the project and copy the public static files to the directory.
Modify the pom. xml file and configure the address and user information of the private maven library. In this way, mvn deploy can automatically publish the jar to the maven library for other war projects.
2.2 War package project reference
First, modify pom. xml and add dependency.
<Dependency> <GroupId> cn. codestory. research </groupId> <ArtifactId> common-static </artifactId> <Version> 0.0.1-SNAPSHOT </version> </Dependency> |
Modify the mvc: resources Section in the spring configuration file
<Mvc: resources mapping = "/static/**" location = "classpath:/static/"/> |
Use Code directly when referencing static files on a page
<Script type = "text/javascript" src = '/static/javascript/framework/jquery. min. js'> </script> <Script type = "text/javascript" src = '/static/javascript/framework/jquery. easyui. min. js'> </script> |
In this way, the static files in the jar package can be accessed normally.
If the war package needs to add static files specific to this project, or modify some static files in the jar to meet the needs of this project, it can be saved in the/src/main/resources/static directory of the war Project. Other code does not need to be modified.
If the static files in the original war are saved in the src/main/webapp/static directory and do not want to move these files, modify the mvc: resources content in the spring configuration file
<Mvc: resources mapping = "/static/**" location = "classpath:/static/,/static/"/> |
Conclusion 2.3
With this modification method, you can share static files without modifying the project deployment method. It is only convenient to build a private Maven repository that is shared among projects.
When the front-end changes the code, you only need to modify it in the common-static.jar project. Of course, modification must be careful because more projects are involved. This allows you to gradually upgrade war projects by upgrading the jar version.
3. static files are deployed as independent WAR files.
In the production environment, the static files are deployed independently on a Server using Tomcat or Http Server. There are two ways to forward static files.
3.1 nginx forwards data based on the URI address
In this way, there are few changes to the project and no new domain names need to be added. You can set forwarding rules:
1. If the uri is/static/**, all requests are forwarded to the static server.
2. The host is app1.codestory.cn and is forwarded to app server 1.
3. The host is app2.codestory.cn and is forwarded to app server 2.
3.2 apply for an independent domain name on the static file server
The deployment scheme is similar to the previous one. The difference lies in the setting of forwarding rules. At the same time, the script Reference Path in the war package must include the complete URL of the host.
<Script type = "text/javascript" Src = 'HTTP: // static.codestory.cn/static/javascript/framework/jquery.min.js'> </script> |
3.3 deploy a static file project using CDN
A further solution is to deploy static files to data centers across the country using CDN. Users can automatically access the nearest server to provide higher access speeds.
However, this solution is completely a problem of deployment. If the project is necessary and the company has the money, you can try it out.