Skip to content

Commit 49e0f32

Browse files
author
Gyula Szalai
committed
Adds new maven plugin configuration property:
1 parent 3720aa0 commit 49e0f32

File tree

16 files changed

+129
-22
lines changed

16 files changed

+129
-22
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
/easydao-maven-plugin/target/
33
/easydao-samples/easydao-generating-dao/target/
44
/easydao-samples/easydao-using-dao/target/
5-
/easydao-samples/creating-sampledb/target/
5+
/easydao-samples/creating-sampledb/target/
6+
.idea
7+
*.iml

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# EasyDao Changelog:
22

3+
## 3.3.0
4+
5+
Adds new maven plugin configuration property `addDbNameToPackageNames`
6+
37
## 3.2.1
48

59
* Fixes Dao.update method generation with Oracle virtual columns

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Configuration example:
209209
</tableNameIncludes>
210210
<encoding>utf-8</encoding>
211211
<silent>true</silent>
212-
212+
<addDbNameToPackageNames>true</addDbNameToPackageNames>
213213
</configuration>
214214
<executions>
215215
<execution>
@@ -353,6 +353,17 @@ Defines the encoding of the generated Java sources. Default: ${project.build.sou
353353
## silent
354354
Indicates whether normal output should be suppressed during code generation. If set, only warnings will be printed.). Default: true
355355

356+
## addDbNameToPackageNames
357+
If true, database name will be added at the end of the java package names. If you have only one database schema, set it to false.
358+
Example:
359+
Let's assume you have a base package of `hu.vanio.easydao.sample` and your database name is `sampledb`
360+
361+
* If you set addDbNameToPackageNames to false, your model and dao packages will be `hu.vanio.easydao.sample.model` and `hu.vanio.easydao.sample.dao`
362+
363+
* If you set addDbNameToPackageNames to true (the default), your model and dao packages will be `hu.vanio.easydao.sample.model.sampledb` and `hu.vanio.easydao.sample.dao.sampledb`
364+
365+
The latter is useful when you have more than one databases, so your model and dao class names won't clash.
366+
356367
# Fields with enumerated vaules
357368
As of 1.0.9, you can use Java enums for fields with enumerated values. You have two choices: regular and irregular enumerations.
358369
If you use fields with enumerated values, values stored in the database need to be mapped to the corresponding enum value and vica versa.

easydao-maven-plugin/src/main/java/hu/vanio/easydao/Engine.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ private void generateMetadataFile() throws IOException, TemplateException {
154154
private void generateModelClasses() throws IOException, TemplateException {
155155
List<Table> tableList = engineConfiguration.getDatabase().getTableList();
156156
// create directory for java package
157-
Path dir = Paths.get(engineConfiguration.getGeneratedSourcePath()
157+
String dirPath = engineConfiguration.getGeneratedSourcePath()
158158
+ fileSeparator
159159
+ engineConfiguration.getPackageOfJavaModel().replace(".", fileSeparator)
160-
+ fileSeparator
161-
+ engineConfiguration.getDatabase().getName());
160+
+ ( engineConfiguration.isAddDbNameToPackageNames() ? fileSeparator + engineConfiguration.getDatabase().getName() : "" );
161+
Path dir = Paths.get(dirPath);
162162
Files.createDirectories(dir);
163163
if (!engineConfiguration.isSilent()) {
164164
System.out.println("\nWrite model classes into " + dir.toAbsolutePath());
@@ -185,11 +185,11 @@ private void generateModelClasses() throws IOException, TemplateException {
185185
private void generateDaoInterfaces() throws IOException, TemplateException {
186186
List<Table> tableList = engineConfiguration.getDatabase().getTableList();
187187
// create directory for java package
188-
Path dir = Paths.get(engineConfiguration.getGeneratedSourcePath()
188+
String dirPath = engineConfiguration.getGeneratedSourcePath()
189189
+ fileSeparator
190190
+ engineConfiguration.getPackageOfJavaDao().replace(".", fileSeparator)
191-
+ fileSeparator
192-
+ engineConfiguration.getDatabase().getName());
191+
+ ( engineConfiguration.isAddDbNameToPackageNames() ? fileSeparator + engineConfiguration.getDatabase().getName() : "" );
192+
Path dir = Paths.get(dirPath);
193193
Files.createDirectories(dir);
194194
if (!engineConfiguration.isSilent()) {
195195
System.out.println("\nWrite dao interfaces into " + dir.toAbsolutePath());
@@ -217,11 +217,11 @@ private void generateDaoInterfaces() throws IOException, TemplateException {
217217
private void generateDaoClasses() throws IOException, TemplateException {
218218
List<Table> tableList = engineConfiguration.getDatabase().getTableList();
219219
// create directory for java package
220-
Path dir = Paths.get(engineConfiguration.getGeneratedSourcePath()
220+
String dirPath = engineConfiguration.getGeneratedSourcePath()
221221
+ fileSeparator
222222
+ engineConfiguration.getPackageOfJavaDao().replace(".", fileSeparator)
223-
+ fileSeparator
224-
+ engineConfiguration.getDatabase().getName());
223+
+ ( engineConfiguration.isAddDbNameToPackageNames() ? fileSeparator + engineConfiguration.getDatabase().getName() : "" );
224+
Path dir = Paths.get(dirPath);
225225
Files.createDirectories(dir);
226226
if (!engineConfiguration.isSilent()) {
227227
System.out.println("\nWrite dao classes into " + dir.toAbsolutePath());

easydao-maven-plugin/src/main/java/hu/vanio/easydao/EngineConfiguration.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static public enum SEQUENCE_NAME_CONVENTION {
116116
protected String encoding;
117117
/** Indicates whether normal output should be suppressed during code generation. If set, only warnings will be printed. */
118118
protected boolean silent;
119+
/** If true, database name will be added at the end of the java package names */
120+
private final boolean addDbNameToPackageNames;
119121

120122
/** Map of Java class names and database table names. Names not included in this list will be auto-generated.
121123
* e.g.: APPUSERS = User -> (User and UserDao) instead of (Appusers and AppusersDao)
@@ -196,6 +198,7 @@ static public enum SEQUENCE_NAME_CONVENTION {
196198
* @param tableNameIncludes
197199
* @param encoding Encoding of the generated source files
198200
* @param silent Indicates whether normal output should be suppressed during code generation. If set, only warnings will be printed.
201+
* @param addDbNameToPackageNames If true, database name will be added at the end of the java package names
199202
* @throws java.io.IOException
200203
*/
201204
public EngineConfiguration(
@@ -209,7 +212,8 @@ public EngineConfiguration(
209212
String licenseFilename,
210213
List<String> tableNameIncludes,
211214
String encoding,
212-
boolean silent) throws IOException {
215+
boolean silent,
216+
boolean addDbNameToPackageNames) throws IOException {
213217

214218
this.database = new Database(databaseName);
215219
this.databaseType = databaseType;
@@ -234,6 +238,7 @@ public EngineConfiguration(
234238
this.tableNameIncludes = tableNameIncludes;
235239
this.encoding = encoding;
236240
this.silent = silent;
241+
this.addDbNameToPackageNames = addDbNameToPackageNames;
237242

238243
if (encoding == null) {
239244
throw new IllegalArgumentException("Encoding cannot be null");
@@ -301,7 +306,8 @@ static public EngineConfiguration createFromProperties(Properties props) throws
301306
props.getProperty("licenseFilename"),
302307
getPropertyAsList(props, "tableNameIncludes"),
303308
props.getProperty("encoding"),
304-
Boolean.valueOf(props.getProperty("silent", "true"))
309+
Boolean.valueOf(props.getProperty("silent", "true")),
310+
Boolean.valueOf(props.getProperty("addDbNameToPackageNames", "true"))
305311
);
306312

307313
String languageCode = props.getProperty("language");
@@ -457,7 +463,15 @@ public boolean isFieldSuffix() {
457463
public boolean isSilent() {
458464
return silent;
459465
}
460-
466+
467+
/**
468+
* If true, database name will be added at the end of the java package names
469+
* @return the addDbNameToPackageNames
470+
*/
471+
public boolean isAddDbNameToPackageNames() {
472+
return addDbNameToPackageNames;
473+
}
474+
461475
/**
462476
* File system directory for generated source files
463477
* @return the generatedSourcePath

easydao-maven-plugin/src/main/java/hu/vanio/maven/plugins/easydao/Generator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ public class Generator extends AbstractMojo {
244244
@Parameter(required = false, defaultValue = "true")
245245
protected String silent;
246246

247+
/**
248+
* Optional, default: true. If true, database name will be added at the end of the java package names
249+
*/
250+
@Parameter(required = false, defaultValue = "true")
251+
protected String addDbNameToPackageNames;
252+
253+
247254
public void execute() throws MojoExecutionException {
248255
getLog().info("easydao-maven-plugin has started. Generating classes from database.");
249256

@@ -302,7 +309,8 @@ public void execute() throws MojoExecutionException {
302309
licenseFilename,
303310
tableNameIncludes,
304311
encoding,
305-
Boolean.parseBoolean(silent)
312+
Boolean.parseBoolean(silent),
313+
Boolean.parseBoolean(addDbNameToPackageNames)
306314
);
307315
if (language != null) {
308316
ec.setLocale(new Locale(language));

easydao-maven-plugin/src/main/resources/hu/vanio/easydao/templates/dao.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GENERATED FILE, DO NOT MODIFY! YOUR MODIFICATION WILL BE LOST!
22
${e.licenseText}
3-
package ${e.packageOfJavaDao}.${e.database.name};
3+
package ${e.packageOfJavaDao}<#if e.addDbNameToPackageNames>.${e.database.name}</#if>;
44

55
import java.math.BigDecimal;
66
<#if t.hasBlobField>import java.sql.Blob;${'\n'}</#if><#if t.hasClobField>import java.sql.Clob;${'\n'}</#if><#if t.hasArrayField>import java.sql.Array;${'\n'}</#if>
@@ -19,7 +19,7 @@ import org.springframework.jdbc.core.RowCallbackHandler;
1919
import org.springframework.jdbc.core.RowMapper;
2020
import org.springframework.stereotype.Repository;
2121

22-
import ${e.packageOfJavaModel}.${e.database.name}.${t.javaName};
22+
import ${e.packageOfJavaModel}<#if e.addDbNameToPackageNames>.${e.database.name}</#if>.${t.javaName};
2323

2424
<#list t.fieldList as field><#if field.enumerated || field.customType>import ${field.javaType};${'\n'}</#if></#list>
2525

easydao-maven-plugin/src/main/resources/hu/vanio/easydao/templates/dao_interface.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// GENERATED FILE, DO NOT MODIFY! YOUR MODIFICATION WILL BE LOST!
22
${e.licenseText}
3-
package ${e.packageOfJavaDao}.${e.database.name};
3+
package ${e.packageOfJavaDao}<#if e.addDbNameToPackageNames>.${e.database.name}</#if>;
44

55
import java.sql.Timestamp;
66
import java.util.List;
77

8-
import ${e.packageOfJavaModel}.${e.database.name}.${t.javaName};
8+
import ${e.packageOfJavaModel}<#if e.addDbNameToPackageNames>.${e.database.name}</#if>.${t.javaName};
99

1010
<#list t.fieldList as field><#if field.enumerated>import ${field.javaType};${'\n'}</#if></#list>
1111
/**

easydao-maven-plugin/src/main/resources/hu/vanio/easydao/templates/model.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GENERATED FILE, DO NOT MODIFY! YOUR MODIFICATION WILL BE LOST!
22
${e.licenseText}
3-
package ${e.packageOfJavaModel}.${e.database.name};
3+
package ${e.packageOfJavaModel}<#if e.addDbNameToPackageNames>.${e.database.name}</#if>;
44

55
import java.sql.Timestamp;
66
import java.math.BigDecimal;

easydao-maven-plugin/src/test/java/hu/vanio/easydao/EngineConfigurationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private void assertProperties(EngineConfiguration conf) {
8787
assertEquals(Locale.getDefault(), conf.getLocale());
8888
assertEquals(Arrays.asList("IR_.+", "KR_.+"), conf.getTableNameIncludes());
8989
assertEquals("utf-8", conf.getEncoding());
90+
assertEquals(true, conf.isAddDbNameToPackageNames());
9091
}
9192

9293
/**

0 commit comments

Comments
 (0)