PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 28, 2022

[FIXED] How to access gradle variable in thymeleaf template?

 September 28, 2022     continuous-deployment, gradle, thymeleaf     No comments   

Issue

I have simple web project with gradle and I wish to obtain gradle variable in thymeleaf template like: Updated part of Thymeleaf template <title>Simple project title v.: @myVar@-beta</title>

and in build.gradle I have this variable (updated after comments): import org.apache.tools.ant.filters.ReplaceTokens def myVar = "dev" ... minimized ... war { filter( ReplaceTokens, tokens: ['@myVar@': myVar]) } where dev - is a custom value from my build server which is passing by gradle war -P myVar=122.

How I can get it if this is possible?


Solution

You wont be able to do this. Well... not the way you are trying to. And then there is the thing that your code should not depend on the build tool.

What you should do instead is, get the build tool to inject the variable into your code.

I am sure your thymeleaf template is being copied from its location to some place else during build, at this point add a filter clause.

change
<title>Some title ${#myVar}</title>
to   
<title>Some title @myVar@</title>

and then

import org.apache.tools.ant.filters.ReplaceTokens // dont forget this import

war {
    filter(ReplaceTokens, tokens: [myVar: myVar])
}


Answered By - Anup Puranik
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing