How to unify two Csss files

Asked

Viewed 250 times

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.

  • 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

  • @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?

  • The archive all.css is in your file war? (Open it with a zip tool after running mvn package).

  • Now that I saw that the plugin also matches, I will delete my reply, you add to your @Anthony

  • Delete no. The answer is good!

  • Beauty then :)

  • Another thing, why are you trying to minify jsp files? The Javascript scanner only minifies Javascript files (usually with extension . js).

  • I don’t know why I put it there, corrected it here in the test and in the question.

  • @Douglas, leave the answer there, boy :).

  • @Anthonyaccioly, I did an update project with Marven, and even an export of the project to generate a war, did not appear the all.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

  • 1

    Ricardo, try to execute mvn clean minify:minify to force the execution of the plugin. Then look for the all.css in the briefcase target 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 tag phase 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.

  • 1

    Besides the tips of utluiz, take a look at demo, even if you do not declare one cssFinalFile was for him to generate a style.css in the directory css inside your WAR.

Show 8 more comments

1 answer

2

Commented from wro4j, You must have tried using an old version, already removed from the repository.

But you will find updated distributions here, choose which one to serve you http://mvnrepository.com/artifact/ro.isdc.wro4j

There is also the repository on github next to documentation, but it is necessary to create an xml named wro.xml and configure the web.xml

But the advantage is that besides minifying, it is possible to merge (unify) that seeks.

Other options

  • At the beginning of the wro4j documentation it teaches how to configure in web.xml, you would know how to convert this to java configuration?

  • I do not know, but I wonder if it is not possible to implement this filter in a class, problem is to know if it would be visible for wro4j. It’s sad, libraries without convention on configuration.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.