maven中默认过滤css_在您的Maven构建中增强CSS

maven中默认过滤css

认识我的人也知道我对GUI感兴趣:这意味着有时我会变得不习惯并深入研究样式表(即使我一点也不具备图形技能)。 当发生这种情况时,我总是对如何最好地分解样式提出疑问。 在这方面,缺少不同的CSS版本,因为它们不是由工程师管理的。 最近的趋势是从源代码生成CSS,它带来了一些有趣的属性,例如嵌套,变量,混合,继承和其他。 这种趋势的两个例子是LESSSASS

下面可以找到一个不太快的示例。

假设我是一名工程师,那么我对那些技术的唯一要求就是我可以在构建过程中以自动化和可复制的方式生成最终CSS。 经过快速搜索,我说服了使用wro4j 。 在简单的用例中,这就是原因。

Maven插件

Wro4j包含一个Maven插件 。 为了在构建中调用它,只需将其添加到您的POM中:

<plugin>
    <groupId> ro.isdc.wro4j </groupId>
    <artifactId> wro4j-maven-plugin </artifactId>
    <version> 1.4.6 </version>
    <executions>
        <execution>
            <goals>
                <goal> run </goal>
            </goals>
            <phase> generate-resources </phase>
        </execution>
    </executions>
</plugin>

您对Maven过敏? 没问题,还免费提供了命令行工具

建立时间

出于明显的性能原因,我倾向于生成时间而不是运行时。 但是,如果您更喜欢后者,则可以为此准备一个JavaEE过滤器

可配置性

由于wro4j的原始策略是运行时生成,因此默认配置文件应位于存档中。 但是,可以通过在POM中设置一些标签来轻松地进行调整:

<configuration>
    <wroManagerFactory> ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory </wroManagerFactory>
    <wroFile> ${basedir}/src/main/config/wro.xml </wroFile>
    <extraConfigFile> ${basedir}/src/main/config/wro.properties </extraConfigFile>
</configuration>

最后一击

赞扬wro4j的真正原因是LESS生成只是其功能的一小部分:对于wro4j而言,它只是其中的一个处理器 。 您只需要查看一长串处理器 (前置和后置)即可使用它们。 例如,wro4j还包装了一个JAWR处理器( 一段时间前写过的捆绑和缩小产品)。

但是,一旦到达那里,就会有一个全新的世界出现在眼前,因为您得到了以下处理器:

  • 通过使用Google,YUI,JAWR等将其最小化来减小JavaScript文件的大小。
  • 通过缩小CSS文件来减小它们的大小
  • 通过将JavaSscript文件合并到一个文件中来最大程度地减少请求数
  • 通过将CSS文件合并为一个文件,最大程度地减少请求数
  • 处理LESS CSS
  • 处理SASS CSS
  • 使用LINT分析您JavaScript

列表实际上是无止境的,您应该真正看看。 此外,如果需要,您可以通过编写自己的处理器来弥合您的最爱。

快速入门

为了快速设置wro4j,以下是一些准备使用的代码片段:

  • 如上所示,Maven POM
    <plugin>
        <groupId> ro.isdc.wro4j </groupId>
        <artifactId> wro4j-maven-plugin </artifactId>
        <version> 1.4.6 </version>
        <configuration>
            <wroManagerFactory> ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory </wroManagerFactory>
            <wroFile> ${basedir}/src/main/config/wro.xml </wroFile>
            <extraConfigFile> ${basedir}/src/main/config/wro.properties </extraConfigFile>
            <targetGroups> all </targetGroups>
            <cssDestinationFolder> ${project.build.directory}/${project.build.finalName}/style/ </cssDestinationFolder>
            <jsDestinationFolder> ${project.build.directory}/${project.build.finalName}/script/ </jsDestinationFolder>
            <contextFolder> ${basedir}/src/main/webapp/ </contextFolder>
            <ignoreMissingResources> false </ignoreMissingResources>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal> run </goal>
                </goals>
                <phase> generate-resources </phase>
            </execution>
        </executions>
    </plugin>

    注意ConfigurableWroManagerFactory的使用。 通过更新以下文件,轻松添加和删除处理器。

  • wro.properties处理器文件,该文件列出了应在源文件上执行的处理。 在这里,我们从LESS生成CSS,将导入解析为一个文件,并最小化CSS(使用JAWR)和JavaScript:
    preProcessors=lessCss,cssImport
    postProcessors=cssMinJawr,jsMin
  • wro.xml配置文件,告诉wro4j如何合并CSS和JS文件。 在我们的例子中, styles.css将被合并到all.cssglobal.jsall.js
    <?xml version="1.0" encoding="UTF-8"?>
    <groupsxmlns="http://www.isdc.ro/wro">
      <groupname="all">
        <css> /style/styles.css </css>
        <js> /script/global.js </js>
      </group>
    </groups>
  • 最后,在下面可以找到一个LESS示例。

结论

有一些针对Web应用程序进行优化的途径。 有些提供了定义CSS的替代方法,有些缩小了CSS,有些JS,另一些将这些文件合并在一起,等等。忽略它们是很可惜的,因为它们可能是繁重的场景中的真正资产。 wro4j提供了一种简单的方法来将所有这些操作包装到可复制的版本中。

LESS

@default-font-family: Helvetica, Arial, sans-serif;
@default-radius: 8px;
@default-color: =5B83AD;
@dark-color: @default-color - =222;
@light-color: @default-color + =222;

.bottom-left-rounded-corners (@radius: @default-radius) {
    .rounded-corners(0, 0, @radius, 0)
}

.bottom-right-rounded-corners (@radius: @default-radius) {
    .rounded-corners(0, 0, 0, @radius)
}

.bottom-rounded-corners (@radius: @default-radius) {
    .rounded-corners(0, 0, @radius, @radius)
}

.top-left-rounded-corners (@radius: @default-radius) {
    .rounded-corners(@radius, 0, 0, 0)
}

.top-right-rounded-corners (@radius: @default-radius) {
    .rounded-corners(0, @radius, 0, 0)
}

.top-rounded-corners (@radius: @default-radius) {
    .rounded-corners(@radius, @radius, 0, 0)
}

.rounded-corners(@top-left: @default-radius, @top-right: @default-radius, @bottom-left: @default-radius,
                                                                          @bottom-right: @default-radius) {
    -moz-border-radius: @top-left @top-right @bottom-right @bottom-left;
    -webkit-border-radius: @top-left @top-right @bottom-right @bottom-left;
    border-radius: @top-left @top-right @bottom-right @bottom-left;
}

.default-border {
    border: 1px solid @dark-color;
}

.no-bottom-border {
    border-bottom: 0;
}

.no-left-border {
    border-left: 0;
}

.no-right-border {
    border-right: 0;
}

body {
    font-family: @default-font-family;
    color: @default-color;
}

h1 {
    color: @dark-color;
}

a {
    color: @dark-color;
    text-decoration: none;
    font-weight: bold;
    &:hover {
        color: black;
    }
}

th {
    color: white;
    background-color: @light-color;
}

td, th {
    .default-border;
    .no-left-border;
    .no-bottom-border;
    padding: 5px;
    &:last-child {
        .no-right-border;
    }
}

tr:first-child {
    th:first-child {
        .top-left-rounded-corners;
    }
    th:last-child {
        .top-right-rounded-corners;
    }
    th:only-child {
        .top-rounded-corners;
    }
}

tr:last-child {
    td:first-child {
        .bottom-left-rounded-corners;
    }
    td:last-child {
        .bottom-right-rounded-corners;
    }
    td:only-child {
        .bottom-rounded-corners;
    }
}

thead tr:first-child th, thead tr:first-child th {
    border-top: 0;
}

table {
    .rounded-corners;
    .default-border;
    border-spacing: 0;
    margin: 5px;
}

Generated CSS

.default-border{
    border:1pxsolid=39618b;
}

.no-bottom-border{
    border-bottom:0;
}

.no-left-border{
    border-left:0;
}

.no-right-border{
    border-right:0;
}

body{
    font-family:Helvetica,Arial,sans-serif;
    color:=5b83ad;
}

h1{
    color:=39618b;
}

a{
    color:=39618b;
    text-decoration:none;
    font-weight:bold;
}

a:hover{
    color:black;
}

th{
    color:white;
    background-color:=7da5cf;
}

td,th{
    border:1pxsolid=39618b;
    border-left:0;
    border-bottom:0;
    padding:5px;
}

td:last-child,th:last-child{
    border-right:0;
}

tr:first-childth:first-child{
    -moz-border-radius:8px000;
    -webkit-border-radius:8px000;
    border-radius:8px000;
}

tr:first-childth:last-child{
    -moz-border-radius:08px00;
    -webkit-border-radius:08px00;
    border-radius:08px00;
}

tr:first-childth:only-child{
    -moz-border-radius:8px8px00;
    -webkit-border-radius:8px8px00;
    border-radius:8px8px00;
}

tr:last-childtd:first-child{
    -moz-border-radius:0008px;
    -webkit-border-radius:0008px;
    border-radius:0008px;
}

tr:last-childtd:last-child{
    -moz-border-radius:008px0;
    -webkit-border-radius:008px0;
    border-radius:008px0;
}

tr:last-childtd:only-child{
    -moz-border-radius:008px8px;
    -webkit-border-radius:008px8px;
    border-radius:008px8px;
}

theadtr:first-childth,theadtr:first-childth{
    border-top:0;
}

table{
    -moz-border-radius:8px8px8px8px;
    -webkit-border-radius:8px8px8px8px;
    border-radius:8px8px8px8px;
    border:1pxsolid=39618b;
    border-spacing:0;
    margin:5px;
}

翻译自: https://blog.frankel.ch/empower-your-css-in-your-maven-build/

maven中默认过滤css

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值