Servlets, development, and a CDN for static files

2019-08-29 02:51发布

问题:

I'm working on a Java site (jQuery, Wicket, Maven, Spring, Hibernate) and we have just started using a CDN to serve static files on our production server. We use a placeholder for the CDN domain, and have added it to every static file that references a static file. The placeholder gets replaced by Maven through properties filters with Maven.

So, for every static file served by the CDN, we have something like this:

<img src="${placeholder}/images/dogs/rex.jpg"/>

For production, ${placeholder} is replaced with "http://cdn.com" and for development environments, it is replaced with blank.

...Is there a better way? It seems unusual to globally add a Maven-specific placeholder to the static files themselves. It will need to be added for every new image going forward, as well. In addition, changes to static files MUST go through a Maven build before those changes will show in a browser, and this slows down development in certain environments. This is probably avoidable, but still seems unnecessary in the first place.

回答1:

When we used to do this kind of thing, we would have the cdn url be a property exposed by an application scoped spring bean, then you can inject this property in your applicationContext.xml and that's where you would replace it with the maven filter. Then we reference an EL expression with that property in the jsps.



标签: maven-2 cdn