Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Replace text in jsp using filter

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}">
  <include 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

like image 905
Josh Avatar asked Mar 22 '11 14:03

Josh


3 Answers

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'])
}
like image 148
Axel Fontaine Avatar answered Oct 26 '22 14:10

Axel Fontaine


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: [...])
      }
   }
}
like image 45
Lukasz Lichota Avatar answered Oct 26 '22 13:10

Lukasz Lichota


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.

like image 39
Josh Avatar answered Oct 26 '22 13:10

Josh