sql-enum-generator-maven-plugin Maven Mojo

Maven plugin to generate java enums from sql database table(s)

License

License

Categories

Categories

Maven Build Tools
GroupId

GroupId

de.enlightened
ArtifactId

ArtifactId

sql-enum-generator-maven-plugin
Last Version

Last Version

0.4.0
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

sql-enum-generator-maven-plugin Maven Mojo
Maven plugin to generate java enums from sql database table(s)
Project URL

Project URL

https://github.com/McNetic/sqlenumgen-maven-plugin
Source Code Management

Source Code Management

https://github.com/McNetic/sqlenumgen-maven-plugin/tree/master

Download sql-enum-generator-maven-plugin

How to add to project

<plugin>
    <groupId>de.enlightened</groupId>
    <artifactId>sql-enum-generator-maven-plugin</artifactId>
    <version>0.4.0</version>
</plugin>

Dependencies

compile (7)

Group / Artifact Type Version
org.apache.maven : maven-plugin-api jar 3.2.5
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.5
org.apache.maven : maven-core jar 3.2.5
org.apache.velocity : velocity jar 1.7
org.apache.velocity : velocity-tools jar 2.0
org.apache.commons : commons-collections4 jar 4.1
commons-lang : commons-lang jar 2.6

test (8)

Group / Artifact Type Version
org.apache.maven : maven-compat jar 3.2.5
org.apache.maven.plugin-testing : maven-plugin-testing-harness jar 3.3.0
junit : junit jar 4.11
org.mockito : mockito-core jar 1.10.19
org.powermock : powermock-module-junit4 jar 1.6.5
org.powermock : powermock-api-mockito jar 1.6.5
com.google.jimfs : jimfs jar 1.1
com.h2database : h2 jar 1.4.192

Project Modules

There are no modules declared in this project.

sqlenumgen-maven-plugin

Maven plugin to generate java enums from sql database table(s)

Abstract

Often, database-backed applications require so called 'master data' tables, containing a few data records that are runtime constant. In Java, such master data is represented by enums.

To avoid having to manually synchronize the generation of the database tables/records and corresponding enum types, this maven plugin allows to generate the enums automatically from existing database tables via jdbc.

Usage

Add the plugin to your pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>de.enlightened</groupId>
      <artifactId>sql-enum-generator-maven-plugin</artifactId>
      <version>0.4.0</version>

      <!-- The plugin should hook into the generate goal -->
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
        </execution>
      </executions>
      <dependencies>
        ...
      </dependencies>
      <configuration>
        <jdbc>...</jdbc>
        <generator>...</generator>
      </configuration>
    </plugin>
  </plugin>
</build>

The configuration is also made in the pom.xml. First, you will usually need a dependency on the jdbc driver for your database (in the plugin configuration, as depicted above). For example, if you use sqlite, add the following to the above dependencies:

<dependency>
  <groupId>org.xerial</groupId>
  <artifactId>sqlite-jdbc</artifactId>
  <version>3.8.11.2</version>
</dependency>

Next, you can add the jdbc connection parameters as required to the above configuration:

  <jdbc>
    <url>...</url>
    <user>...</user>
    <password>...</password>
  </jdbc>

For example:

<jdbc>
  <url>jdbc:sqlite:test.db</url>
</jdbc>

Now for the interesting part: The generator configuration. By default, the enums will be generated in a package de.enlightened.sqlenum in the directory target/generated-sources/sql-enum. You can override both:

    <target>
      <package>de.enlightened.sqlenum</package>
      <directory>target/generated-sources/sql-enum</directory>
    </target>

At last, you specify the enums to be generated:

  <database>
    <enum>
      <name>Enumname</name>
      <table>tablename</table>
      <valueColumn>columnname</valueColumn>
    </enum>
    ...
  </database>

This will generate an enum with the specified name from the specified table records. By default, the first column whose sql type has a string representation in java will be chosen for naming the enum values. This can be overriden by explicitly setting the 'valueColumn'. All other columns will be used to populate properties of the enum.

Multiple enums can be configured.

Options

Multiple enums from one table

It is possible to have multiple enums generated from one table. In this case, one column has to be specified as nameColumn (<nameColumn>columnname</nameColumn>). All rows with the same value in this column will be used for generating values in an enum, whose name is the value of the name column.

Public attributes

By default, the attributes of generated enums are private, and getters are provided for each attribute. It is possible to have all attributes be generated as public (they are final anyways), and omit the getters at all by setting <attributeVisibility>public</attributeVisibility> in the generator config.

Versions

Version
0.4.0