Add more configuration options #577

This commit is contained in:
Timo Westkämper 2013-11-30 19:29:37 +02:00
parent 380aad99fe
commit bc47130961
4 changed files with 152 additions and 99 deletions

View File

@ -198,6 +198,16 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
*/
private String[] customTypes;
/**
* @parameter
*/
private TypeMapping[] typeMappings;
/**
* @parameter
*/
private NumericMapping[] numericMappings;
/**
* @parameter default-value=false
*/
@ -241,119 +251,103 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
} else {
project.addCompileSourceRoot(targetFolder);
}
NamingStrategy namingStrategy;
if (namingStrategyClass != null) {
try {
try {
Configuration configuration = new Configuration(SQLTemplates.DEFAULT);
NamingStrategy namingStrategy;
if (namingStrategyClass != null) {
namingStrategy = (NamingStrategy) Class.forName(namingStrategyClass).newInstance();
} catch (InstantiationException e) {
throw new MojoExecutionException(e.getMessage(),e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException(e.getMessage(),e);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException(e.getMessage(),e);
} else {
namingStrategy = new DefaultNamingStrategy();
}
} else {
namingStrategy = new DefaultNamingStrategy();
}
// defaults for Scala
if (createScalaSources) {
if (serializerClass == null) {
serializerClass = "com.mysema.query.scala.sql.ScalaMetaDataSerializer";
// defaults for Scala
if (createScalaSources) {
if (serializerClass == null) {
serializerClass = "com.mysema.query.scala.sql.ScalaMetaDataSerializer";
}
if (exportBeans && beanSerializerClass == null) {
beanSerializerClass = "com.mysema.query.scala.ScalaBeanSerializer";
}
}
if (exportBeans && beanSerializerClass == null) {
beanSerializerClass = "com.mysema.query.scala.ScalaBeanSerializer";
}
}
MetaDataExporter exporter = new MetaDataExporter();
if (namePrefix != null) {
exporter.setNamePrefix(namePrefix);
}
if (nameSuffix != null) {
exporter.setNameSuffix(nameSuffix);
}
if (beanPrefix != null) {
exporter.setBeanPrefix(beanPrefix);
}
if (beanSuffix != null) {
exporter.setBeanSuffix(beanSuffix);
}
exporter.setCreateScalaSources(createScalaSources);
exporter.setPackageName(packageName);
exporter.setBeanPackageName(beanPackageName);
exporter.setInnerClassesForKeys(innerClassesForKeys);
exporter.setTargetFolder(new File(targetFolder));
exporter.setNamingStrategy(namingStrategy);
exporter.setSchemaPattern(schemaPattern);
exporter.setTableNamePattern(tableNamePattern);
exporter.setColumnAnnotations(columnAnnotations);
exporter.setValidationAnnotations(validationAnnotations);
exporter.setSchemaToPackage(schemaToPackage);
exporter.setLowerCase(lowerCase);
exporter.setExportTables(exportTables);
exporter.setExportViews(exportViews);
exporter.setExportPrimaryKeys(exportPrimaryKeys);
exporter.setExportForeignKeys(exportForeignKeys);
if (serializerClass != null) {
try {
exporter.setSerializerClass((Class)Class.forName(serializerClass));
} catch (ClassNotFoundException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
MetaDataExporter exporter = new MetaDataExporter();
if (namePrefix != null) {
exporter.setNamePrefix(namePrefix);
}
}
if (exportBeans) {
if (beanSerializerClass != null) {
if (nameSuffix != null) {
exporter.setNameSuffix(nameSuffix);
}
if (beanPrefix != null) {
exporter.setBeanPrefix(beanPrefix);
}
if (beanSuffix != null) {
exporter.setBeanSuffix(beanSuffix);
}
exporter.setCreateScalaSources(createScalaSources);
exporter.setPackageName(packageName);
exporter.setBeanPackageName(beanPackageName);
exporter.setInnerClassesForKeys(innerClassesForKeys);
exporter.setTargetFolder(new File(targetFolder));
exporter.setNamingStrategy(namingStrategy);
exporter.setSchemaPattern(schemaPattern);
exporter.setTableNamePattern(tableNamePattern);
exporter.setColumnAnnotations(columnAnnotations);
exporter.setValidationAnnotations(validationAnnotations);
exporter.setSchemaToPackage(schemaToPackage);
exporter.setLowerCase(lowerCase);
exporter.setExportTables(exportTables);
exporter.setExportViews(exportViews);
exporter.setExportPrimaryKeys(exportPrimaryKeys);
exporter.setExportForeignKeys(exportForeignKeys);
if (serializerClass != null) {
try {
exporter.setBeanSerializerClass((Class)Class.forName(beanSerializerClass));
exporter.setSerializerClass((Class)Class.forName(serializerClass));
} catch (ClassNotFoundException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
}
} else {
BeanSerializer serializer = new BeanSerializer();
if (beanInterfaces != null) {
for (String iface : beanInterfaces) {
try {
}
if (exportBeans) {
if (beanSerializerClass != null) {
exporter.setBeanSerializerClass((Class)Class.forName(beanSerializerClass));
} else {
BeanSerializer serializer = new BeanSerializer();
if (beanInterfaces != null) {
for (String iface : beanInterfaces) {
serializer.addInterface(Class.forName(iface));
} catch (ClassNotFoundException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
serializer.setAddFullConstructor(beanAddFullConstructor);
serializer.setAddToString(beanAddToString);
serializer.setPrintSupertype(beanPrintSupertype);
exporter.setBeanSerializer(serializer);
}
serializer.setAddFullConstructor(beanAddFullConstructor);
serializer.setAddToString(beanAddToString);
serializer.setPrintSupertype(beanPrintSupertype);
exporter.setBeanSerializer(serializer);
}
String sourceEncoding = (String)project.getProperties().get("project.build.sourceEncoding");
if (sourceEncoding != null) {
exporter.setSourceEncoding(sourceEncoding);
}
}
String sourceEncoding = (String)project.getProperties().get("project.build.sourceEncoding");
if (sourceEncoding != null) {
exporter.setSourceEncoding(sourceEncoding);
}
try {
if (customTypes != null) {
Configuration configuration = new Configuration(SQLTemplates.DEFAULT);
for (String cl : customTypes) {
configuration.register((Type<?>)Class.forName(cl).newInstance());
configuration.register((Type<?>) Class.forName(cl).newInstance());
}
}
if (typeMappings != null) {
for (TypeMapping mapping : typeMappings) {
configuration.register(mapping.table, mapping.column, (Type<?>) Class.forName(mapping.type).newInstance());
}
}
if (numericMappings != null) {
for (NumericMapping mapping : numericMappings) {
configuration.registerNumeric(mapping.size, mapping.digits, Class.forName(mapping.javaType));
}
exporter.setConfiguration(configuration);
}
} catch (IllegalAccessException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
} catch (InstantiationException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
} catch (ClassNotFoundException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
}
try {
exporter.setConfiguration(configuration);
Class.forName(jdbcDriver);
Connection conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
try{
@ -363,13 +357,16 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
conn.close();
}
}
} catch (SQLException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
} catch (ClassNotFoundException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
} catch (SQLException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (InstantiationException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
protected boolean isForTest() {

View File

@ -0,0 +1,28 @@
/*
* Copyright 2013, Mysema Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.query.maven;
/**
* @author tiwe
*
*/
public class NumericMapping {
public int size;
public int digits;
public String javaType;
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2013, Mysema Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.query.maven;
/**
* @author tiwe
*
*/
public class TypeMapping {
public String table;
public String column;
public String type;
}

View File

@ -1,6 +1,6 @@
/*
* Copyright 2011, Mysema Ltd
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -32,9 +32,9 @@ public class MetadataExportMojoTest {
private final String url = "jdbc:h2:mem:testdb" + System.currentTimeMillis();
private final MavenProject project = new MavenProject();
private final MetadataExportMojo mojo = new MetadataExportMojo();
@Test
public void Execute() throws Exception {
mojo.setProject(project);
@ -68,7 +68,7 @@ public class MetadataExportMojoTest {
assertEquals(Collections.singletonList("target/export2"), project.getCompileSourceRoots());
assertTrue(new File("target/export2").exists());
}
@Test
public void Execute_With_JodaTypes() throws Exception{
mojo.setProject(project);