The SQLComments Maven plugin speeds up development and preserves type safety in Java code.

There are three goals:

  • export - moves SQL statements from comments in Java sources to separate SQL files
  • generate - generates result, configuration and mapper classes from SQL files
  • domain - generates domain, configuration and mapper classes for basic CRUD operations from database tables or views

 

Export goal

It moves the SQL statements from the comments in the Java source code files into separate .sql files. Each statement is placed in a separate file. The statement starts with the @SQLComment annotation and ends at the first empty line. Therefore, it is possible to keep multiple statements within one Java comment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<build>
    <plugins>
        <plugin>
            <groupId>sk.vracon.sqlcomments</groupId>
            <artifactId>sqlcomments-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>exportSqlFromMainSources</id>
                    <goals>
                        <goal>export</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Configuration parameters:

Property Description
sourceDirectory The directory which contains the sources to be parsed.
Default: ${project.build.sourceDirectory}
outputDirectory The output directory for generated files and java classes.
Default: ${project.build.directory}/generated-sources/sqlcomments
includes A list of files to include. Ant-style wildcards and double wildcards are supported.
Default: **/*.java
excludes A list of files to exclude. Ant-style wildcards and double wildcards are supported.
compileWithTestClasses This indicates whether the standard or test classpath should be used to compile the generated classes.
Default: false - using standard classpath.

 

Generate goal

According to the database model it generates result, configuration and mapper classes from the SQL statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<build>
    <plugins>
        <plugin>
            <groupId>sk.vracon.sqlcomments</groupId>
            <artifactId>sqlcomments-maven-plugin</artifactId>
            <version>${project.version}</version>
            <executions>
                <execution>
                    <id>generateClasses</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <jdbcDriverClass>org.hsqldb.jdbcDriver</jdbcDriverClass>
                        <databaseUrl>jdbc:hsqldb:res:testdb</databaseUrl>
                        <dbUserName>sa</dbUserName>
                        <dbPassword></dbPassword>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <!-- JDBC driver and Javascript engine -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Configuration parameters:

Property Description
sourceDirectory The directory which contains the *.sql files to be parsed.
Default: ${project.build.directory}/generated-sources/sqlcomments
outputDirectory The output directory for generated files and java classes.
Default: ${project.build.directory}/generated-sources/sqlcomments
includes A list of files to include. Ant-style wildcards and double wildcards are supported.
Default: **/*.sql
excludes A list of files to exclude. Ant-style wildcards and double wildcards are supported.
compileWithTestClasses This indicates whether the standard or test classpath should be used to compile the generated classes.
Default: false - using standard classpath.
databaseDialect Database dialect class used to translate database data types into Java types.
Default: sk.vracon.sqlcomments.core.dialect.HSQLDialect
jdbcDriverClass JDBC driver class name.
Class must be included in plugin dependendcies.
databaseUrl Database URL.
dbUserName Database credentials - username.
dbPassword Database credentials - password.

 

 Domain goal

It generates domain, configuration and mapper classes required for basic CRUD operations from database tables or views. The domain class is in fact the result class of the findByPK statement. The classes generated by this goal are used by the AbstractSQLCommentsRepository (see Spring integration for more).

The class name is generated from the table name by removing the table prefix and converting the name to camelCase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<build>
    <plugins>
        <plugin>
            <groupId>sk.vracon.sqlcomments</groupId>
            <artifactId>sqlcomments-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>generateDomain</id>
                    <goals>
                        <goal>domain</goal>
                    </goals>
                    <configuration>
                        <packageName>sk.vracon.sqlcomments.spring.domain</packageName>
                        <tables>
                            <USERS/>
                            <COMPANIES>pkGenerator=nexval('COMPANIES_SEQ')</COMPANIES>
                            <DOCUMENTS>pkGenerator=DOCUMENTS_SEQ.nextval()</DOCUMENTS>
                        </tables>
                        <jdbcDriverClass>org.hsqldb.jdbcDriver</jdbcDriverClass>
                        <databaseUrl>jdbc:hsqldb:res:testdb</databaseUrl>
                        <dbUserName>sa</dbUserName>
                        <dbPassword></dbPassword>
                        <compileWithTestClasses>true</compileWithTestClasses>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <!-- JDBC driver and Javascript engine -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Configuration parameters:

Property Description
outputDirectory The output directory for generated files and java classes.
Default: ${project.build.directory}/generated-sources/sqlcomments
compileWithTestClasses This indicates whether the standard or test classpath should be used to compile the generated classes.
Default: false - using standard classpath.
tables A list of tables to include.
Map consists of pairs <table name> - <table properties>. Properties can be used to create a fine-grained configuration of table columns and their mappings. Properties are in form java.util.Properties.
Currently supported properties:
  
className Java class name to use instead of generated name.
interfaces A list of interface classes that should be used to mark generated domain classes.
pkGeneratorA sequence call or other statement that can be used in the generated INSERT statements instead of the value of the primary key column. If different settings are required for various database types, more pkGenerator properties can be defined for the same column. If this is the case, add the database name as a prefix to the property name. E.g.:
mycolumn.pkGenerator = null                                      # use 'null' value as default
mycolumn.pkGenerator.postgresql = nextval('companies_seq')       # use sequence for PostgreSQL
type Type implementation to read/write value from java.sql API.
excluded Boolean property to exclude column from generating in domain classes
 mappingFiles List of xml files containing table configurations. Content of configuration is the same as defined in the tables parameter wrapped in <sqlcomments> element. E.g.
<sqlcomments>
    <USERS />
    <COMPANIES>
        id.type=sk.vracon.sqlcomments.types.LongType
        country.type=sk.vracon.sqlcomments.types.EnumType(sk.vracon.sqlcomments.maven.ExampleEnum)
    </COMPANIES>
    <DOCUMENTS />
</sqlcomments>
 tablePrefix A table prefix to be removed when creating domain class name.
 packageName Target package name for generated classes.
databaseDialect Database dialect class used to translate database data types into Java types.
Default: sk.vracon.sqlcomments.core.dialect.HSQLDialect
jdbcDriverClass JDBC driver class name.
Class must be listed in plugin dependendcies.
databaseUrl Database URL.
dbUserName Database credentials - username.
dbPassword Database credentials - password.

 

 Configuration for tests

Maven distinguishes between main and test classpaths. It is necessary to configure the plugins separately for tests.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<build>
    <plugins>
        <plugin>
            <groupId>sk.vracon.sqlcomments</groupId>
            <artifactId>sqlcomments-maven-plugin</artifactId>
            <version>${project.version}</version>
            <executions>
                <execution>
                    <id>exportSqlFromTestSources</id>
                    <phase>generate-test-resources</phase>
                    <goals>
                        <goal>export</goal>
                    </goals>
                    <configuration>
                        <sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
                        <compileWithTestClasses>true</compileWithTestClasses>
                    </configuration>
                </execution>
                <execution>
                    <id>generateClassesFromTestSources</id>
                    <phase>generate-test-resources</phase>
                    <goals>
                        <goal>generate</goal>
                        <goal>domain</goal>
                    </goals>
                    <configuration>
                        <packageName>sk.vracon.sqlcomments.spring.domain</packageName>
                        <tables>
                            <USERS/>
                            <COMPANIES/>
                            <DOCUMENTS/>
                        </tables>
                        <jdbcDriverClass>org.hsqldb.jdbcDriver</jdbcDriverClass>
                        <databaseUrl>jdbc:hsqldb:res:testdb</databaseUrl>
                        <dbUserName>sa</dbUserName>
                        <dbPassword></dbPassword>
                        <compileWithTestClasses>true</compileWithTestClasses>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <!-- JDBC driver and Javascript engine -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>