1

In my jpa app i want to switch ddl schema generation per profile. For schema generation enabled i need additional lines:

  <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
  <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
  <property name="javax.persistence.schema-generation.scripts.create-target" value="sampleCreate.ddl"/>
  <property name="javax.persistence.schema-generation.scripts.drop-target" value="sampleDrop.ddl"/>

if schema generation is disabled this lines should be not there or disabled. Adding ${some.prop} into xml to filter it by maven raises errors in persistence.xml (ide). Since i want to switch all this lines at once it would be fine if there is onlz one property to be set. So how to achieve this?

dermoritz
  • 12,519
  • 25
  • 97
  • 185

1 Answers1

1

Per profile you should set property like this

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...

    <dependencies>
...
    </dependencies>

    <profiles>
        <profile>
            <id>enableSchemaGeneration</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <schema.generation>
                 <![CDATA[
                   <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
                   <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
                   <property name="javax.persistence.schema-generation.scripts.create-target" value="sampleCreate.ddl"/>
                   <property name="javax.persistence.schema-generation.scripts.drop-target" value="sampleDrop.ddl"/>
                 ]]>
                </schema.generation>
            </properties>
        </profile>
        <profile>
            <id>disableSchemaGeneration</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <schema.generation></schema.generation>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

Now you can build it like mvn clean install -PenableSchemaGeneration and it will change

${schema.generation}

in persistence.xml on

<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="sampleCreate.ddl"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="sampleDrop.ddl"/>
ppysz
  • 380
  • 5
  • 21
  • this is what i did, but all validators moan about "${schema.generation}" in persistence.xml – dermoritz Jun 25 '15 at 11:57
  • How about then having two different xml files, and depending on profile you could copy right one into persistance.xml using `` ? – ppysz Jun 26 '15 at 08:22