switching jpa 2.1 schema generation per maven profile

438 views Asked by At

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?

1

There are 1 answers

2
ppysz On

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"/>