Skip to content

Commit f78bb0a

Browse files
authored
Merge pull request #950 from jeffgbutler/info-logging
General Cleanup
2 parents a80dcd4 + 77c3fc3 commit f78bb0a

File tree

25 files changed

+133
-311
lines changed

25 files changed

+133
-311
lines changed

core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/ProgressCallback.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ default void generationStarted(int totalTasks) {}
6565
/**
6666
* Called to note the start of the file saving phase, and to note the
6767
* maximum number of startTask messages that will be sent for the file
68-
* saving phase phase.
68+
* saving phase.
6969
*
7070
* @param totalTasks
7171
* the maximum number of times startTask will be called for the
@@ -87,8 +87,8 @@ default void startTask(String taskName) {}
8787
default void done() {}
8888

8989
/**
90-
* The method is called periodically during a long running method.
91-
* If the the implementation throws <code>InterruptedException</code> then
90+
* The method is called periodically during a long-running method.
91+
* If the implementation throws <code>InterruptedException</code> then
9292
* the method will be canceled. Any files that have already been saved will
9393
* remain on the file system.
9494
*

core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/ShellRunner.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,17 +22,16 @@
2222
import java.sql.SQLException;
2323
import java.util.ArrayList;
2424
import java.util.HashMap;
25-
import java.util.HashSet;
2625
import java.util.List;
2726
import java.util.Map;
2827
import java.util.Set;
29-
import java.util.StringTokenizer;
3028

3129
import org.mybatis.generator.config.Configuration;
3230
import org.mybatis.generator.config.xml.ConfigurationParser;
3331
import org.mybatis.generator.exception.InvalidConfigurationException;
3432
import org.mybatis.generator.exception.XMLParserException;
3533
import org.mybatis.generator.internal.DefaultShellCallback;
34+
import org.mybatis.generator.internal.util.StringUtility;
3635
import org.mybatis.generator.logging.LogFactory;
3736

3837
/**
@@ -79,42 +78,22 @@ public static void main(String[] args) {
7978
return;
8079
}
8180

82-
Set<String> fullyqualifiedTables = new HashSet<>();
83-
if (arguments.containsKey(TABLES)) {
84-
StringTokenizer st = new StringTokenizer(arguments.get(TABLES), ","); //$NON-NLS-1$
85-
while (st.hasMoreTokens()) {
86-
String s = st.nextToken().trim();
87-
if (s.length() > 0) {
88-
fullyqualifiedTables.add(s);
89-
}
90-
}
91-
}
81+
Set<String> fullyQualifiedTables = StringUtility.tokenize(arguments.get(TABLES));
9282

93-
Set<String> contexts = new HashSet<>();
94-
if (arguments.containsKey(CONTEXT_IDS)) {
95-
StringTokenizer st = new StringTokenizer(
96-
arguments.get(CONTEXT_IDS), ","); //$NON-NLS-1$
97-
while (st.hasMoreTokens()) {
98-
String s = st.nextToken().trim();
99-
if (s.length() > 0) {
100-
contexts.add(s);
101-
}
102-
}
103-
}
83+
Set<String> contexts = StringUtility.tokenize(arguments.get(CONTEXT_IDS));
10484

10585
try {
10686
ConfigurationParser cp = new ConfigurationParser(warnings);
10787
Configuration config = cp.parseConfiguration(configurationFile);
10888

109-
DefaultShellCallback shellCallback = new DefaultShellCallback(
110-
arguments.containsKey(OVERWRITE));
89+
DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
11190

11291
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
11392

11493
ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
11594
: null;
11695

117-
myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
96+
myBatisGenerator.generate(progressCallback, contexts, fullyQualifiedTables);
11897

11998
} catch (XMLParserException e) {
12099
writeLine(getString("Progress.3")); //$NON-NLS-1$

core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/DefaultShellCallback.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,17 +33,15 @@ public DefaultShellCallback(boolean overwrite) {
3333
}
3434

3535
@Override
36-
public File getDirectory(String targetProject, String targetPackage)
37-
throws ShellException {
38-
// targetProject is interpreted as a directory that must exist
36+
public File getDirectory(String targetProject, String targetPackage) throws ShellException {
37+
// targetProject is interpreted as a directory that must already exist
3938
//
40-
// targetPackage is interpreted as a sub directory, but in package
41-
// format (with dots instead of slashes). The sub directory will be
42-
// created
43-
// if it does not already exist
39+
// targetPackage is interpreted as a subdirectory, but in package
40+
// format (with dots instead of slashes). The subdirectory will be
41+
// created if it does not already exist
4442

45-
File project = new File(targetProject);
46-
if (!project.isDirectory()) {
43+
File targetProjectDirectory = new File(targetProject);
44+
if (!targetProjectDirectory.isDirectory()) {
4745
throw new ShellException(getString("Warning.9", //$NON-NLS-1$
4846
targetProject));
4947
}
@@ -55,7 +53,7 @@ public File getDirectory(String targetProject, String targetPackage)
5553
sb.append(File.separatorChar);
5654
}
5755

58-
File directory = new File(project, sb.toString());
56+
File directory = new File(targetProjectDirectory, sb.toString());
5957
if (!directory.isDirectory()) {
6058
boolean rc = directory.mkdirs();
6159
if (!rc) {

core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/util/StringUtility.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.generator.internal.util;
1717

18+
import java.util.HashSet;
19+
import java.util.Set;
1820
import java.util.StringTokenizer;
1921

2022
public class StringUtility {
@@ -100,4 +102,26 @@ public static boolean stringContainsSQLWildcard(String s) {
100102

101103
return s.indexOf('%') != -1 || s.indexOf('_') != -1;
102104
}
105+
106+
/**
107+
* Given an input string, tokenize on the commas and trim all token.
108+
* Returns an empty set if the input string is null.
109+
*
110+
* @param in strong to tokenize.
111+
* @return Set of tokens
112+
*/
113+
public static Set<String> tokenize(String in) {
114+
Set<String> answer = new HashSet<>();
115+
if (StringUtility.stringHasValue(in)) {
116+
StringTokenizer st = new StringTokenizer(in, ","); //$NON-NLS-1$
117+
while (st.hasMoreTokens()) {
118+
String s = st.nextToken().trim();
119+
if (s.length() > 0) {
120+
answer.add(s);
121+
}
122+
}
123+
}
124+
return answer;
125+
}
126+
103127
}

core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2022 the original author or authors.
4+
Copyright 2006-2023 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,65 +16,43 @@
1616
package org.mybatis.generator.maven;
1717

1818
import java.io.File;
19-
import java.util.StringTokenizer;
2019

2120
import org.mybatis.generator.exception.ShellException;
2221
import org.mybatis.generator.internal.DefaultShellCallback;
23-
import org.mybatis.generator.internal.util.messages.Messages;
22+
23+
import static org.mybatis.generator.internal.util.messages.Messages.getString;
2424

2525
/**
2626
* Shell callback that calculates the Maven output directory.
2727
*
2828
* @author Jeff Butler
2929
*/
3030
public class MavenShellCallback extends DefaultShellCallback {
31-
private MyBatisGeneratorMojo mybatisGeneratorMojo;
31+
private final MyBatisGeneratorMojo mybatisGeneratorMojo;
3232

3333
public MavenShellCallback(MyBatisGeneratorMojo mybatisGeneratorMojo, boolean overwrite) {
3434
super(overwrite);
3535
this.mybatisGeneratorMojo = mybatisGeneratorMojo;
3636
}
3737

3838
@Override
39-
public File getDirectory(String targetProject, String targetPackage)
40-
throws ShellException {
41-
if (!"MAVEN".equals(targetProject)) {
42-
return super.getDirectory(targetProject, targetPackage);
43-
}
44-
45-
// targetProject is the output directory from the MyBatis generator
46-
// Mojo. It will be created if necessary
47-
//
48-
// targetPackage is interpreted as a sub directory, but in package
49-
// format (with dots instead of slashes). The sub directory will be created
50-
// if it does not already exist
51-
52-
File project = mybatisGeneratorMojo.getOutputDirectory();
53-
if (!project.exists()) {
54-
project.mkdirs();
55-
}
56-
57-
if (!project.isDirectory()) {
58-
throw new ShellException(Messages.getString("Warning.9", //$NON-NLS-1$
59-
project.getAbsolutePath()));
60-
}
61-
62-
StringBuilder sb = new StringBuilder();
63-
StringTokenizer st = new StringTokenizer(targetPackage, "."); //$NON-NLS-1$
64-
while (st.hasMoreTokens()) {
65-
sb.append(st.nextToken());
66-
sb.append(File.separatorChar);
67-
}
68-
69-
File directory = new File(project, sb.toString());
70-
if (!directory.isDirectory()) {
71-
boolean rc = directory.mkdirs();
72-
if (!rc) {
73-
throw new ShellException(Messages.getString("Warning.10", //$NON-NLS-1$
74-
directory.getAbsolutePath()));
39+
public File getDirectory(String targetProject, String targetPackage) throws ShellException {
40+
if ("MAVEN".equals(targetProject)) {
41+
// targetProject is the output directory from the MyBatis generator
42+
// Mojo. It will be created if necessary
43+
44+
File project = mybatisGeneratorMojo.getOutputDirectory();
45+
if (!project.exists()) {
46+
boolean rc = project.mkdirs();
47+
if (!rc) {
48+
throw new ShellException(getString("Warning.10", //$NON-NLS-1$
49+
project.getAbsolutePath()));
50+
}
7551
}
76-
}
7752

78-
return directory;
53+
return super.getDirectory(project.getAbsolutePath(), targetPackage);
54+
} else {
55+
return super.getDirectory(targetProject, targetPackage);
56+
}
7957
}
8058
}

core/mybatis-generator-maven-plugin/src/main/java/org/mybatis/generator/maven/MyBatisGeneratorMojo.java

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import java.util.HashSet;
2323
import java.util.List;
2424
import java.util.Set;
25-
import java.util.StringTokenizer;
2625

2726
import org.apache.maven.artifact.DependencyResolutionRequiredException;
2827
import org.apache.maven.model.Resource;
@@ -178,69 +177,38 @@ public void execute() throws MojoExecutionException {
178177
ObjectFactory.addExternalClassLoader(cl);
179178

180179
if (configurationFile == null) {
181-
throw new MojoExecutionException(
182-
Messages.getString("RuntimeError.0")); //$NON-NLS-1$
180+
throw new MojoExecutionException(Messages.getString("RuntimeError.0")); //$NON-NLS-1$
183181
}
184182

185-
List<String> warnings = new ArrayList<>();
186-
187183
if (!configurationFile.exists()) {
188-
throw new MojoExecutionException(Messages.getString(
189-
"RuntimeError.1", configurationFile.toString())); //$NON-NLS-1$
184+
throw new MojoExecutionException(Messages.getString("RuntimeError.1", configurationFile.toString())); //$NON-NLS-1$
190185
}
191186

192187
runScriptIfNecessary();
193188

194-
Set<String> fullyqualifiedTables = new HashSet<>();
195-
if (StringUtility.stringHasValue(tableNames)) {
196-
StringTokenizer st = new StringTokenizer(tableNames, ","); //$NON-NLS-1$
197-
while (st.hasMoreTokens()) {
198-
String s = st.nextToken().trim();
199-
if (s.length() > 0) {
200-
fullyqualifiedTables.add(s);
201-
}
202-
}
203-
}
189+
Set<String> fullyQualifiedTables = StringUtility.tokenize(tableNames);
204190

205-
Set<String> contextsToRun = new HashSet<>();
206-
if (StringUtility.stringHasValue(contexts)) {
207-
StringTokenizer st = new StringTokenizer(contexts, ","); //$NON-NLS-1$
208-
while (st.hasMoreTokens()) {
209-
String s = st.nextToken().trim();
210-
if (s.length() > 0) {
211-
contextsToRun.add(s);
212-
}
213-
}
214-
}
191+
Set<String> contextsToRun = StringUtility.tokenize(contexts);
192+
193+
List<String> warnings = new ArrayList<>();
215194

216195
try {
217-
ConfigurationParser cp = new ConfigurationParser(
218-
project.getProperties(), warnings);
196+
ConfigurationParser cp = new ConfigurationParser(project.getProperties(), warnings);
219197
Configuration config = cp.parseConfiguration(configurationFile);
220198

221199
ShellCallback callback = new MavenShellCallback(this, overwrite);
222200

223-
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
224-
callback, warnings);
201+
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
225202

226-
myBatisGenerator.generate(new MavenProgressCallback(getLog(),
227-
verbose), contextsToRun, fullyqualifiedTables);
203+
myBatisGenerator.generate(new MavenProgressCallback(getLog(), verbose), contextsToRun, fullyQualifiedTables);
228204

229-
} catch (XMLParserException e) {
205+
} catch (XMLParserException | InvalidConfigurationException e) {
230206
for (String error : e.getErrors()) {
231207
getLog().error(error);
232208
}
233209

234210
throw new MojoExecutionException(e.getMessage());
235-
} catch (SQLException e) {
236-
throw new MojoExecutionException(e.getMessage());
237-
} catch (IOException e) {
238-
throw new MojoExecutionException(e.getMessage());
239-
} catch (InvalidConfigurationException e) {
240-
for (String error : e.getErrors()) {
241-
getLog().error(error);
242-
}
243-
211+
} catch (SQLException | IOException e) {
244212
throw new MojoExecutionException(e.getMessage());
245213
} catch (InterruptedException e) {
246214
// ignore (will never happen with the DefaultShellCallback)
@@ -250,8 +218,7 @@ public void execute() throws MojoExecutionException {
250218
getLog().warn(error);
251219
}
252220

253-
if (project != null && outputDirectory != null
254-
&& outputDirectory.exists()) {
221+
if (project != null && outputDirectory != null && outputDirectory.exists()) {
255222
project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
256223

257224
Resource resource = new Resource();

core/mybatis-generator-maven-plugin/src/main/java/org/mybatis/generator/maven/SqlScriptRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)