SQLComments comes with maven plugin to speed-up development and to preserve type safety in Java code.

There are three goals:

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

 

Export goal

Moves SQL statements from comments in Java sources to separate .sql files. Each statement is placed in separate file. Statement starts with @SQLComment annotation and ends at first empty line, hence it is possible to keep more statements in one Java comments.

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 you want to be parsed.
Default: ${project.build.sourceDirectory}
outputDirectory The output directory where to generate files and java classes.
Default: ${project.build.directory}/generated-sources/sqlcomments
includes A list of files to include. Can contain ant-style wildcards and double wildcards.
Default: **/*.java
excludes A list of files to exclude. Can contain ant-style wildcards and double wildcards.
compileWithTestClasses Indicates whether to use standard or test class path for compilation of generated classes.
Default: false - using standard class path.

 

Generate goal

According to database model it generates result, configuration and mapper classes from 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 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Configuration parameters:

Property Description
sourceDirectory The directory which contains the *.sql files you want to be parsed.
Default: ${project.build.directory}/generated-sources/sqlcomments
outputDirectory The output directory where to generate files and java classes.
Default: ${project.build.directory}/generated-sources/sqlcomments
includes A list of files to include. Can contain ant-style wildcards and double wildcards.
Default: **/*.sql
excludes A list of files to exclude. Can contain ant-style wildcards and double wildcards.
compileWithTestClasses Indicates whether to use standard or test class path for compilation of generated classes.
Default: false - using standard class path.
databaseDialect Database dialect class used to translate database data types into Java types.
Default: sk.vracon.sqlcomments.maven.DefaultDatabaseDialect
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

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

Class names are generated from table names by removing table prefix and making name camel case.

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 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Configuration parameters:

Property Description
outputDirectory The output directory where to generate files and java classes.
Default: ${project.build.directory}/generated-sources/sqlcomments
compileWithTestClasses Indicates whether to use standard or test class path for compilation of generated classes.
Default: false - using standard class path.
tables A list of tables to include.
Map consists of pairs <table name> - <table properties>. Properties are in form Properties.
Currently supported properties:
   - pkGenerator - sequence call or other statement to use in generated insert statements instead of PK column
 tablePrefix A table prefix to be removed when creating domain class name.
 packageName Target package name where generate classes.
databaseDialect Database dialect class used to translate database data types into Java types.
Default: sk.vracon.sqlcomments.maven.DefaultDatabaseDialect
jdbcDriverClass JDBC driver class name.
Class must be included in plugin dependendcies.
databaseUrl Database URL.
dbUserName Database credentials - username.
dbPassword Database credentials - password.

 

 Configuration for tests

Maven distinguish between main and test classpath. It is necessary to configure plugins separatelly 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 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>