X-Git-Url: https://juplo.de/gitweb/?p=hibernate4-maven-plugin;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fplugins%2Fhibernate%2FAbstractSchemaMojo.java;h=b61d83c9ade9889a8d934871e32ee57ea669937c;hp=6c8a28189a0156500c568903328ab2c4ef19920f;hb=6dff3bfb0f9ea7a1d0cc56398aaad29e31a17b91;hpb=4940080670944a15916c68fb294e18a6bfef12d5
diff --git a/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java b/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java
index 6c8a2818..b61d83c9 100644
--- a/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java
+++ b/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java
@@ -44,9 +44,12 @@ import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.boot.spi.MetadataImplementor;
import static org.hibernate.cfg.AvailableSettings.DIALECT;
import static org.hibernate.cfg.AvailableSettings.DRIVER;
+import static org.hibernate.cfg.AvailableSettings.FORMAT_SQL;
+import static org.hibernate.cfg.AvailableSettings.HBM2DLL_CREATE_NAMESPACES;
import static org.hibernate.cfg.AvailableSettings.IMPLICIT_NAMING_STRATEGY;
import static org.hibernate.cfg.AvailableSettings.PASS;
import static org.hibernate.cfg.AvailableSettings.PHYSICAL_NAMING_STRATEGY;
+import static org.hibernate.cfg.AvailableSettings.SHOW_SQL;
import static org.hibernate.cfg.AvailableSettings.USER;
import static org.hibernate.cfg.AvailableSettings.URL;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
@@ -70,7 +73,13 @@ import org.scannotation.AnnotationDB;
*/
public abstract class AbstractSchemaMojo extends AbstractMojo
{
- public final static String EXPORT_SKIPPED_PROPERTY = "hibernate.export.skipped";
+ public final static String EXPORT = "hibernate.schema.export";
+ public final static String DELIMITER = "hibernate.schema.delimiter";
+ public final static String OUTPUTDIRECTORY = "project.build.outputDirectory";
+ public final static String SCAN_DEPENDENCIES = "hibernate.schema.scan.dependencies";
+ public final static String SCAN_TESTCLASSES = "hibernate.schema.scan.test_classes";
+ public final static String TEST_OUTPUTDIRECTORY = "project.build.testOutputDirectory";
+ public final static String SKIPPED = "hibernate.schema.skipped";
private final static Pattern SPLIT = Pattern.compile("[^,\\s]+");
@@ -97,27 +106,144 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
*/
String buildDirectory;
+
+ /** Parameters to configure the genaration of the SQL *********************/
+
/**
- * Classes-Directory to scan.
+ * Export the database-schma to the database.
+ * If set to false
, only the SQL-script is created and the
+ * database is not touched.
*
- * This parameter defaults to the maven build-output-directory for classes.
- * Additionally, all dependencies are scanned for annotated classes.
+ * Important:
+ * This configuration value can only be configured through the
+ * pom.xml
, or by the definition of a system-property, because
+ * it is not known by Hibernate nor JPA and, hence, not picked up from
+ * their configuration!
*
- * @parameter property="project.build.outputDirectory"
+ * @parameter property="hibernate.schema.export" default-value="true"
+ * @since 2.0
+ */
+ Boolean export;
+
+ /**
+ * Skip execution
+ *
+ * If set to true
, the execution is skipped.
+ *
+ * A skipped execution is signaled via the maven-property
+ * ${hibernate.export.skipped}
.
+ *
+ * The execution is skipped automatically, if no modified or newly added + * annotated classes are found and the dialect was not changed. + *
+ * Important:
+ * This configuration value can only be configured through the
+ * pom.xml
, or by the definition of a system-property, because
+ * it is not known by Hibernate nor JPA and, hence, not picked up from
+ * their configuration!
+ *
+ * @parameter property="hibernate.schema.skip" default-value="${maven.test.skip}"
* @since 1.0
*/
- private String outputDirectory;
+ private boolean skip;
/**
- * Whether to scan test-classes too, or not.
+ * Force execution
*
- * If this parameter is set to true
the test-classes of the
- * artifact will be scanned for hibernate-annotated classes additionally.
+ * Force execution, even if no modified or newly added annotated classes
+ * where found and the dialect was not changed.
+ *
+ * skip
takes precedence over force
.
+ *
+ * Important:
+ * This configuration value can only be configured through the
+ * pom.xml
, or by the definition of a system-property, because
+ * it is not known by Hibernate nor JPA and, hence, not picked up from
+ * their configuration!
*
- * @parameter property="hibernate.export.scan_testclasses" default-value="false"
- * @since 1.0.1
+ * @parameter property="hibernate.schema.force" default-value="false"
+ * @since 1.0
+ */
+ private boolean force;
+
+ /**
+ * Hibernate dialect.
+ *
+ * @parameter property="hibernate.dialect"
+ * @since 1.0
+ */
+ private String dialect;
+
+ /**
+ * Delimiter in output-file.
+ *
+ * Important:
+ * This configuration value can only be configured through the
+ * pom.xml
, or by the definition of a system-property, because
+ * it is not known by Hibernate nor JPA and, hence, not picked up from
+ * their configuration!
+ *
+ * @parameter property="hibernate.schema.delimiter" default-value=";"
+ * @since 1.0
+ */
+ String delimiter;
+
+ /**
+ * Show the generated SQL in the command-line output.
+ *
+ * @parameter property="hibernate.show_sql"
+ * @since 1.0
*/
- private boolean scanTestClasses;
+ Boolean show;
+
+ /**
+ * Format output-file.
+ *
+ * @parameter property="hibernate.format_sql"
+ * @since 1.0
+ */
+ Boolean format;
+
+ /**
+ * Specifies whether to automatically create also the database schema/catalog.
+ *
+ * @parameter property="hibernate.hbm2dll.create_namespaces" default-value="false"
+ * @since 2.0
+ */
+ Boolean createNamespaces;
+
+ /**
+ * Implicit naming strategy
+ *
+ * @parameter property="hibernate.implicit_naming_strategy"
+ * @since 2.0
+ */
+ private String implicitNamingStrategy;
+
+ /**
+ * Physical naming strategy
+ *
+ * @parameter property="hibernate.physical_naming_strategy"
+ * @since 2.0
+ */
+ private String physicalNamingStrategy;
+
+ /**
+ * Classes-Directory to scan.
+ *
+ * This parameter defaults to the maven build-output-directory for classes. + * Additionally, all dependencies are scanned for annotated classes. + *
+ * Important:
+ * This configuration value can only be configured through the
+ * pom.xml
, or by the definition of a system-property, because
+ * it is not known by Hibernate nor JPA and, hence, not picked up from
+ * their configuration!
+ *
+ * @parameter property="project.build.outputDirectory"
+ * @since 1.0
+ */
+ private String outputDirectory;
/**
* Dependency-Scopes, that should be scanned for annotated classes.
@@ -125,7 +251,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
* By default, only dependencies in the scope compile
are
* scanned for annotated classes. Multiple scopes can be seperated by
* white space or commas.
- *
md5s + *
* If you do not want any dependencies to be scanned for annotated
* classes, set this parameter to none
.
*
@@ -133,11 +259,28 @@ public abstract class AbstractSchemaMojo extends AbstractMojo * dependencies. If some of your annotated classes are hidden in a * transitive dependency, you can simply add that dependency explicitly. * - * @parameter property="hibernate.export.scan_dependencies" default-value="compile" + * @parameter property="hibernate.schema.scan.dependencies" default-value="compile" * @since 1.0.3 */ private String scanDependencies; + /** + * Whether to scan test-classes too, or not. + *
+ * If this parameter is set to true
the test-classes of the
+ * artifact will be scanned for hibernate-annotated classes additionally.
+ *
+ * Important:
+ * This configuration value can only be configured through the
+ * pom.xml
, or by the definition of a system-property, because
+ * it is not known by Hibernate nor JPA and, hence, not picked up from
+ * their configuration!
+ *
+ * @parameter property="hibernate.schema.scan.test_classes" default-value="false"
+ * @since 1.0.1
+ */
+ private Boolean scanTestClasses;
+
/**
* Test-Classes-Directory to scan.
*
@@ -146,40 +289,20 @@ public abstract class AbstractSchemaMojo extends AbstractMojo *
* This parameter is only used, when scanTestClasses
is set
* to true
!
+ *
+ * Important:
+ * This configuration value can only be configured through the
+ * pom.xml
, or by the definition of a system-property, because
+ * it is not known by Hibernate nor JPA and, hence, not picked up from
+ * their configuration!
*
* @parameter property="project.build.testOutputDirectory"
* @since 1.0.2
*/
private String testOutputDirectory;
- /**
- * Skip execution
- *
- * If set to true
, the execution is skipped.
- *
- * A skipped execution is signaled via the maven-property
- * ${hibernate.export.skipped}
.
- *
- * The execution is skipped automatically, if no modified or newly added - * annotated classes are found and the dialect was not changed. - * - * @parameter property="hibernate.skip" default-value="${maven.test.skip}" - * @since 1.0 - */ - private boolean skip; - /** - * Force execution - *
- * Force execution, even if no modified or newly added annotated classes - * where found and the dialect was not changed. - *
- * skip
takes precedence over force
.
- *
- * @parameter property="hibernate.export.force" default-value="false"
- * @since 1.0
- */
- private boolean force;
+ /** Conection parameters *************************************************/
/**
* SQL-Driver name.
@@ -213,29 +336,8 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
*/
private String password;
- /**
- * Hibernate dialect.
- *
- * @parameter property="hibernate.dialect"
- * @since 1.0
- */
- private String dialect;
- /**
- * Implicit naming strategy
- *
- * @parameter property=IMPLICIT_NAMING_STRATEGY
- * @since 2.0
- */
- private String implicitNamingStrategy;
-
- /**
- * Physical naming strategy
- *
- * @parameter property=PHYSICAL_NAMING_STRATEGY
- * @since 2.0
- */
- private String physicalNamingStrategy;
+ /** Parameters to locate configuration sources ****************************/
/**
* Path to a file or name of a ressource with hibernate properties.
@@ -302,8 +404,8 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
private String mappings;
- @Override
- public final void execute()
+
+ public final void execute(String filename)
throws
MojoFailureException,
MojoExecutionException
@@ -311,14 +413,14 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
if (skip)
{
getLog().info("Execution of hibernate-maven-plugin was skipped!");
- project.getProperties().setProperty(EXPORT_SKIPPED_PROPERTY, "true");
+ project.getProperties().setProperty(SKIPPED, "true");
return;
}
ModificationTracker tracker;
try
{
- tracker = new ModificationTracker(buildDirectory, getLog());
+ tracker = new ModificationTracker(buildDirectory, filename, getLog());
}
catch (NoSuchAlgorithmException e)
{
@@ -353,10 +455,10 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
properties.putAll(loadPersistenceUnit(classLoaderService, properties));
/** Overwriting/Completing configuration */
- configure(properties);
+ configure(properties, tracker);
/** Check configuration for modifications */
- if(tracker.check(properties))
+ if(tracker.track(properties))
getLog().debug("Configuration has changed.");
else
getLog().debug("Configuration unchanged.");
@@ -384,7 +486,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
else
{
getLog().info("Skipping schema generation!");
- project.getProperties().setProperty(EXPORT_SKIPPED_PROPERTY, "true");
+ project.getProperties().setProperty(SKIPPED, "true");
return;
}
}
@@ -563,170 +665,115 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
}
}
- private void configure(Properties properties)
+ private void configure(Properties properties, ModificationTracker tracker)
throws MojoFailureException
{
- /** Overwrite values from properties-file or set, if given */
+ /**
+ * Special treatment for the configuration-value "export": if it is
+ * switched to "true", the genearation fo the schema should be forced!
+ */
+ if (tracker.check(EXPORT, export.toString()) && export)
+ tracker.touch();
+
+ /**
+ * Configure the generation of the SQL.
+ * Overwrite values from properties-file if the configuration parameter is
+ * known to Hibernate.
+ */
+ dialect = configure(properties, dialect, DIALECT);
+ tracker.track(DELIMITER, delimiter); // << not reflected in hibernate configuration!
+ format = configure(properties, format, FORMAT_SQL);
+ createNamespaces = configure(properties, createNamespaces, HBM2DLL_CREATE_NAMESPACES);
+ implicitNamingStrategy = configure(properties, implicitNamingStrategy, IMPLICIT_NAMING_STRATEGY);
+ physicalNamingStrategy = configure(properties, physicalNamingStrategy, PHYSICAL_NAMING_STRATEGY);
+ tracker.track(OUTPUTDIRECTORY, outputDirectory); // << not reflected in hibernate configuration!
+ tracker.track(SCAN_DEPENDENCIES, scanDependencies); // << not reflected in hibernate configuration!
+ tracker.track(SCAN_TESTCLASSES, scanTestClasses.toString()); // << not reflected in hibernate configuration!
+ tracker.track(TEST_OUTPUTDIRECTORY, testOutputDirectory); // << not reflected in hibernate configuration!
+
+ /**
+ * Special treatment for the configuration-value "show": a change of its
+ * configured value should not lead to a regeneration of the database
+ * schama!
+ */
+ if (show == null)
+ show = Boolean.valueOf(properties.getProperty(SHOW_SQL));
+ else
+ properties.setProperty(SHOW_SQL, show.toString());
- if (driver != null)
- {
- if (properties.containsKey(DRIVER))
- getLog().debug("Overwriting property " +
- DRIVER + "=" + properties.getProperty(DRIVER) +
- " with the value " + driver
- );
- else
- getLog().debug("Using the value " + driver);
- properties.setProperty(DRIVER, driver);
- }
- if (properties.getProperty(DRIVER) == null)
- {
- String driver = properties.getProperty(JDBC_DRIVER);
- if (driver != null)
- {
- getLog().info(DRIVER +
- " is not set. Borrow setting from " +
- JDBC_DRIVER +
- ": " +
- driver);
- properties.setProperty(DRIVER, driver);
- }
- }
+ /**
+ * Configure the connection parameters.
+ * Overwrite values from properties-file.
+ */
+ driver = configure(properties, driver, DRIVER, JDBC_DRIVER);
+ url = configure(properties, url, URL, JDBC_URL);
+ username = configure(properties, username, USER, JDBC_USER);
+ password = configure(properties, password, PASS, JDBC_PASSWORD);
- if (url != null)
- {
- if (properties.containsKey(URL))
- getLog().debug(
- "Overwriting property " +
- URL + "=" + properties.getProperty(URL) +
- " with the value " + url
- );
- else
- getLog().debug("Using the value " + url);
- properties.setProperty(URL, url);
- }
- if (properties.getProperty(URL) == null)
+ if (properties.isEmpty())
{
- String url = properties.getProperty(JDBC_URL);
- if (url != null)
- {
- getLog().info(URL +
- " is not set. Borrow setting from " +
- JDBC_URL +
- ": " +
- url);
- properties.setProperty(URL, url);
- }
+ getLog().error("No properties set!");
+ throw new MojoFailureException("Hibernate configuration is missing!");
}
- if (username != null)
- {
- if (properties.containsKey(USER))
- getLog().debug("Overwriting property " +
- USER + "=" + properties.getProperty(USER) +
- " with the value " + username
- );
- else
- getLog().debug("Using the value " + username);
- properties.setProperty(USER, username);
- }
- if (properties.getProperty(USER) == null)
- {
- username = properties.getProperty(JDBC_USER);
- if (username != null)
- {
- getLog().info(USER +
- " is not set. Borrow setting from " +
- JDBC_USER +
- ": " +
- username);
- properties.setProperty(USER, username);
- }
- }
+ getLog().info("Gathered hibernate-configuration (turn on debugging for details):");
+ for (Entry