开发者

Gradle Replace text in jsp using filter

开发者 https://www.devze.com 2023-02-19 03:30 出处:网络
I have a jsp that contains a css link that looks like <link type=\"text/css\" href=\"/css/login-min.css\" rel=\"stylesheet\" />

I have a jsp that contains a css link that looks like

<link type="text/css" href="/css/login-min.css" rel="stylesheet" />

In order to keep browsers from caching the css file we replace login-min.css with the name of the css and a timestamp or version number

login-min.css?t=432432423423...

In ant I would do something like

<tstamp>
  <format property="current.time" pattern="MMddyyyyhhmmssaa" offset="-5" unit="hour" />
</tstamp>

<replace dir="${deploy.path}/${name}/WEB-INF/jsp" value="login-min.css?t=${current.time}">
  <incl开发者_StackOverflowude name="includes/login_css_include.jsp" />
  <replacetoken>login-min.css</replacetoken>
</replace>

For gradle I've updated the jsp page to look like

<link type="text/css" href="/css/@loginCSS@" rel="stylesheet" />

and in the build.gradle am doing

import org.apache.tools.ant.filters.ReplaceTokens
war {
  webInf {
    from ("${webAppDir}/WEB-INF/jsp") {
      include: "/includes/login_css_include.jsp"
      filter(ReplaceTokens, tokens: [loginCSS: 'login-min.css?v=1'])
    }
  }
}

but this isn't working.

This one works but it changes the source... I just want the files in the war to be modified.

import org.apache.tools.ant.filters.ReplaceTokens
war {
  webInf {
    from ("${webAppDir}/WEB-INF/jsp/includes/login_css_include.jsp") {
      it.eachFile {
        ant.replace(file: it.file, token: "@loginCSS@", value: "login-min.css?v=1")
      }
    }
  }
}

I'm brand new to gradle am I going about this totally incorrectly? Anyone needed to do something like this before? Using gradle 1.0-milestone-1.

Thanks


Ok, this seems to work:

jsp page:

<link type="text/css" href="/css/@loginCSS@" rel="stylesheet" />

build.gradle:

war {
   filter(ReplaceTokens, tokens: ['loginCSS': 'login-min.css'])
}


Yet another variant to try out (it works in my case to overcome this bug http://issues.gradle.org/browse/GRADLE-1566#)

war {   
   eachFile {
      if (it.name == 'yourFile.jsp') {
        it.filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [...])
      }
   }
}


Not sure if this fits with the groovy/gradle style this is how I was able to accomplish this.

jsp file now looks like this:

<link type="text/css" href="/css/${loginCSS}" rel="stylesheet" />

and the gradle.build like this:

war {
  eachFile {
    if (it.name == 'login_css_include.jsp') {
      it.expand(loginCSS: "login-min.css?v=1")
    }
  }
}

Utilizing a timestamp looks like this:

war {
  eachFile {
    if (it.name == 'login_css_include.jsp') {
      def now = new Date()
      it.expand(loginCSS: "login-min.css?t=${now.getTime()}")
    }
  }
}

Edit - as Axel points out below in the comments, this solution has trouble when it runs across binary files or files with JSP EL expressions.


We've done similar (but not quite exactly the same) things just creating java.io.Files and manipulating them, then including them into the .war using the from{} or webInf{} with a fileTree.

You could look into something like that, but since this seems to be handled pretty well by ant, have you looked into using ant directly from gradle like this?


You can also set encoding by(because by default it will change the file encoding):

war {
    filteringCharset = 'UTF-8'
    filesMatching( '**/*.jsp' ) {
        filter ReplaceTokens, tokens: [
                'versionDate': "${new Date().format('yyyyMMdd')}".toString(),
                'copyright'  : "${new Date().format('yyyy')}".toString()
        ]
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消