6
I’m doing a project Spring MVC 4.2.
In this project I have more than one file CSS and more than one JS.
What I want is to transform all the files Csss in a single ALL.css and that this file is minify
.
What I’ve got going is a setting that puts the resources in a pre defined directory. The setting is this:
VersionResourceResolver versionResolver = new VersionResourceResolver()
.addFixedVersionStrategy(version, "/**/*.js", "/**/*.css")
.addContentVersionStrategy("/**");
registry.addResourceHandler("/public-resources/**")
.addResourceLocations("/resources/")
.setCachePeriod(1)
.resourceChain(true)
.addResolver(versionResolver)
.addTransformer(appCacheTransformer);
With this I can access the files on view thus:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<link rel="stylesheet" type="text/css" href="<spring:url value="/resources/css/magnific-popup.css"/>">
The result in HTML is this:
<link rel="stylesheet" type="text/css" href="/MeuProjeto/public-resources/css/magnific-popup.css">
Everything works.
Is there any way to unify Csss into a single all.css file and call it that? Spring is able to do this?
<link rel="stylesheet" type="text/css" href="<spring:url value="/resources/css/all.css"/>">
I found some plugins that were supposed to work for this, I thought the wro4j that you can’t even start using by adding dependency on pom.xml
he can’t read the repository.
UPDATING
I am updating to include trials and doubts in the question, I think it will help future readers.
The first try with Minify-Maven-plugin.
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.4</version>
<executions>
<execution>
<id>default-minify</id>
<configuration>
<charset>UTF-8</charset>
<cssSourceDir>resources/css</cssSourceDir>
<cssSourceFiles>
<cssSourceFile>Site.css</cssSourceFile>
<cssSourceFile>style.css</cssSourceFile>
</cssSourceFiles>
<cssFinalFile>all.css</cssFinalFile>
<jsSourceDir>resources/js</jsSourceDir>
<jsSourceFiles>
<jsSourceFile>index.js</jsSourceFile>
<jsSourceFile>sendmail.js</jsSourceFile>
</jsSourceFiles>
<jsFinalFile>all.js</jsFinalFile>
<jsEngine>CLOSURE</jsEngine>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
I used it in the view like this:
<link rel="stylesheet" type="text/css" href="<spring:url value="/resources/css/all.css"/>">
Generated the following output:
<link rel="stylesheet" type="text/css" href="/Isabele/resources/css/all.css">
But the file all css. is not there, giving 404. At the test level I didn’t change the other css files in the view, so the two files used in the plugin configuration Site css. and css style. are being used exactly as the all css. and the two are on site and responding.
Hi Ricardo, see if the Minify-Maven-plugin solves your problem. I used for Javascript and CSS successfully.
– Anthony Accioly
My comment was without thinking, I did not see that the setting was in the
pom.xml
. I thought it would be on some web.xml. That’s why I talked about the java configuration, it even deletes the comment. Thank you– Ricardo
@Anthonyaccioly, I updated the question with the plugin attempt, I think I understand what it does, actually it is quite simple, I just did not understand why my all.css was not generated. Could you read the update, please?
– Ricardo
The archive
all.css
is in your filewar
? (Open it with a zip tool after runningmvn package
).– Anthony Accioly
Now that I saw that the plugin also matches, I will delete my reply, you add to your @Anthony
– dougg0k
Delete no. The answer is good!
– Ricardo
Beauty then :)
– dougg0k
Another thing, why are you trying to minify jsp files? The Javascript scanner only minifies Javascript files (usually with extension . js).
– Anthony Accioly
I don’t know why I put it there, corrected it here in the test and in the question.
– Ricardo
@Douglas, leave the answer there, boy :).
– Anthony Accioly
@Anthonyaccioly, I did an update project with Marven, and even an export of the project to generate a
war
, did not appear theall.css
no, the other files are there. I’m thinking that the setting might be wrong where I use<cssSourceDir>
. The files are inside Webcontent/Resources/css– Ricardo
Ricardo, try to execute
mvn clean minify:minify
to force the execution of the plugin. Then look for theall.css
in the briefcasetarget
and in the subdirectories as well. If the file is there, the plugin is working and the problem may be or that it is not running in the normal Maven cycle (in this case, try adding a tagphase
as in the example of documentation) or it may be that the file is not being considered at the time of packaging WAR, then you would have to review your configuration of this plugin.– utluiz
Besides the tips of utluiz, take a look at demo, even if you do not declare one
cssFinalFile
was for him to generate astyle.css
in the directorycss
inside your WAR.– Anthony Accioly