com.jdc.mvn.plugins.dbtools.GenClassesMojo Maven / Gradle / Ivy
package com.jdc.mvn.plugins.dbtools;
import org.apache.maven.plugin.MojoExecutionException;
import org.dbtools.gen.DBObjectsBuilder;
import org.dbtools.gen.DateType;
import org.dbtools.gen.GenConfig;
import org.dbtools.gen.android.AndroidObjectsBuilder;
import org.dbtools.gen.jpa.JPAObjectsBuilder;
import java.io.File;
/**
* Goal which creates database classes based on an DBTools schema.xml file
*
* @author Jeff Campbell
* @version $Id$
* @goal genclasses
* @phase generate-sources
*/
public class GenClassesMojo extends AbstractDBToolsMojo {
/**
* Based directory where the source files will be generated.
*
* @parameter expression="${basedir}/src/main/java"
*/
private String outputSrcDir;
/**
* Based directory where the test source files will be generated.
*
* @parameter expression="${basedir}/src/test/java"
*/
private String outputTestSrcDir;
/**
* Type of application: JPA, ANDROID, ANDROID-KOTLIN
*
* @parameter default-value="JPA"
*/
private String type = "JPA";
/**
* Add JEE/Spring Transactional annotations to CRUD methods in BaseManager
*
* @parameter default-value="false"
*/
private boolean javaEESupport = false;
/**
* Type of date to use in the database: JAVA-DATE, JODA, JSR-310 (not yet supported)
*
* @parameter default-value="JAVA-DATE"
*/
private String dateType = "JAVA-DATE";
/**
* Use CDI Dependency Injection
*
* @parameter default-value="false"
*/
private boolean injectionSupport = false;
/**
* Use RxJava
*
* @parameter default-value="false"
*/
private boolean rxJavaSupport = false;
/**
* Use jsr 305 (@Nullable, @Notnull, etc)
*
* @parameter default-value="true"
*/
private boolean jsr305Support = true;
/**
* Use SQLQueryBuilder for generated queries
*
* @parameter default-value="false"
*/
private boolean sqlQueryBuilderSupport = false;
/**
* If using multiple databases, it may be better to organize domain objects by database name
*
* @parameter default-value="false"
*/
private boolean includeDatabaseNameInPackage = false;
/**
* Name of the base package that should be used for generated files. This
* package name is a base to the packages that will be generated
* (example: com.company.data will produce the following com.company.data.object1,
* com.company.data.object2, etc) This package is also used to determine the
* directories to create in both the
* src/main/java AND src/test/java directories
*
* @parameter
* @required
*/
private String basePackageName;
/**
* Generate default unit tests to test generate classes. This may be useful
* to help increase code coverage tests.
*
* @parameter default-value="false"
*/
private boolean genUnitTests = false;
/**
* Skip code generation
*
* @parameter default-value="false"
*/
private boolean skip = false;
/**
* Classpath of the renderer to use to generate objects. If not specified,
* then the default renderer is used. Include the dependency jar file for
* the classpath item:
*
*
*
*
* mygroup
* myartifact
* 1.0
*
*
*
* @parameter
*/
private String rendererClasspath = null;
@Override
public void execute() throws MojoExecutionException {
if (!skip) {
getLog().info("Generating classes...");
verifyParameters();
genClasses();
} else {
getLog().info("SKIPPING Database classes.");
}
}
private void verifyParameters() throws MojoExecutionException {
File schemaFile = new File(getSchemaFullFilename());
if (!schemaFile.exists()) {
throw new MojoExecutionException("Could not find file: " + getSchemaFullFilename());
}
}
private String getPathFromPackage(String baseDir, String packageName) {
String path = baseDir;
if (!path.endsWith("\\") && !path.endsWith("/")) {
path += "/";
}
for (char c : packageName.toCharArray()) {
switch (c) {
case '.':
path += '/';
break;
default:
path += c;
}
}
return path;
}
private void genClasses() throws MojoExecutionException {
DBObjectsBuilder builder;
GenConfig genConfig = new GenConfig();
genConfig.setInjectionSupport(injectionSupport);
genConfig.setJavaeeSupport(javaEESupport);
genConfig.setIncludeDatabaseNameInPackage(includeDatabaseNameInPackage);
genConfig.setRxJavaSupport(rxJavaSupport);
genConfig.setJsr305Support(jsr305Support);
genConfig.setSqlQueryBuilderSupport(sqlQueryBuilderSupport);
switch (dateType) {
default:
case "JAVA-DATE":
genConfig.setDateType(DateType.JAVA_DATE);
break;
case "JODA":
genConfig.setDateType(DateType.JODA);
break;
case "JSR-310":
genConfig.setDateType(DateType.JSR_310);
break;
}
switch (type) {
default:
case "JPA":
builder = new JPAObjectsBuilder(genConfig);
break;
case "ANDROID":
builder = new AndroidObjectsBuilder(genConfig);
}
// schema file
builder.setXmlFilename(getSchemaFullFilename());
// tables to build objects for
builder.setTables(null); // means all
// output directory
//File source = project.
builder.setOutputBaseDir(getPathFromPackage(outputSrcDir, basePackageName));
// object information
builder.setPackageBase(basePackageName);
// GENERATE
builder.build();
// show results
int filesGenerated = builder.getNumberFilesGenerated();
getLog().info("Generated [" + filesGenerated + "] files.");
}
}