Home Define variables in gradle to fill data in persistence.xml
Reply: 2

Define variables in gradle to fill data in persistence.xml

Paddy3108
1#
Paddy3108 Published in 2017-12-06 12:04:23Z

Can I define variables in gradle, which are fill in the placeholders in the persistence.xml?

Example

gradle.properties:

dev.classes= <class>my.package.class</class>.....<class>my.package.class</class>

persistence.xml

<persistence>
   <persistence-unit name "a">
     <!--define a variable that will be overwritten by build.gradle-->
       ...
  </persistence-unit>
</persistence>

build.gradle

task replaceTokens {
  from(//persistence.xml path) {
     filter(ReplaceTokens, tokens: [//variable in xml, //value in gradle.properties])
   }
}

I need it, because I have more persistence-units in my persistence.xml, which all needs the same classes. Currently the classes are in all persistence-unit "hardcoded". It would be nicer, if I can define it only once.

lance-java
2#
lance-java Reply to 2017-12-06 16:03:16Z

If it were me, I'd put the xml template under src/template/resources

src/template/resources/persistence.xml

<persistence>
    <persistence-unit name="a">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/A</jta-data-source>
@persistence.classes@
    </persistence-unit>

    <persistence-unit name="b">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/B</jta-data-source>
@persistence.classes@
    </persistence-unit>
</persistence>

gradle.properties

persistence.classes=\
        <class>com.example.Foo</class>\
        <class>com.example.Bar</class>\
        ...

build.gradle

apply plugin: 'java'
processResources {
    from 'src/template/resources', {
        filter(ReplaceTokens, tokens: ['persistence.classes': property('persistence.classes')])
    }
}
Vampire
3#
Vampire Reply to 2017-12-06 13:53:48Z

Sure you can, replace //value in gradle.properties by project.'dev.classes', or if your property does not contain a dot, simply dev_classes will also do.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307986 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO