修改: 多模块路径获取错误修正

master
曹世达 6 months ago
parent 8621e4f244
commit bdfc250061

@ -10,12 +10,29 @@ public class PathUtils {
private static final String JAVA_FILE_SUFFIX = ".java"; private static final String JAVA_FILE_SUFFIX = ".java";
public static String getProjectPath() { public static String getProjectBasePath() {
return CONFIG.getString(CfgConsts.PROJECT_PATH_BASE); return CONFIG.getString(CfgConsts.PROJECT_PATH_BASE).replaceAll("\\\\", "/");
} }
public static String getJavaPath() { public static String getProjectName() {
String basePath = getProjectPath(); return StrUtils.last(getProjectBasePath(), "/");
}
public static String getProjectPath(String moduleName) {
String projectBasePath = getProjectBasePath();
if (!isMultiModule()) {
return projectBasePath;
} else {
return projectBasePath + "/" + getProjectName() + "-" + moduleName;
}
}
public static boolean isMultiModule() {
return CONFIG.getBoolean(CfgConsts.MODULE_MULTIPLE);
}
public static String getJavaPath(String moduleName) {
String basePath = getProjectPath(moduleName);
String configPath = CONFIG.getString(CfgConsts.PROJECT_PATH_JAVA); String configPath = CONFIG.getString(CfgConsts.PROJECT_PATH_JAVA);
String defaultPath = CfgConsts.DEFAULT_PROJECT_PATH_JAVA; String defaultPath = CfgConsts.DEFAULT_PROJECT_PATH_JAVA;
String path = StrUtils.defaultIfBlank(configPath, defaultPath); String path = StrUtils.defaultIfBlank(configPath, defaultPath);
@ -27,10 +44,10 @@ public class PathUtils {
} }
public static String getPackageBasePath(String moduleName) { public static String getPackageBasePath(String moduleName) {
String basePath = getJavaPath(); String javaPath = getJavaPath(moduleName);
String packageName = CONFIG.getString(CfgConsts.PACKAGE_NAME_BASE); String packageName = CONFIG.getString(CfgConsts.PACKAGE_NAME_BASE);
String path = packageToPath(packageName); String packagePath = packageToPath(packageName);
return Paths.get(basePath, moduleName, path).toString(); return Paths.get(javaPath, packagePath, moduleName).toString();
} }
public static String getPathBasedPackage( public static String getPathBasedPackage(
@ -42,15 +59,9 @@ public class PathUtils {
return Paths.get(basePath, path).toString(); return Paths.get(basePath, path).toString();
} }
public static boolean isMultiModule() {
return CONFIG.getBoolean(CfgConsts.MODULE_MULTIPLE);
}
public static String getModuleName(String configName, String defaultName) { public static String getModuleName(String configModuleName, String defaultModuleName) {
if (!isMultiModule()) { return isMultiModule() ? StrUtils.defaultIfBlank(configModuleName, defaultModuleName) : "";
return "";
}
return StrUtils.defaultIfBlank(configName, defaultName);
} }
public static String getRepositoryModuleName() { public static String getRepositoryModuleName() {
@ -71,54 +82,53 @@ public class PathUtils {
return getModuleName(configName, defaultName); return getModuleName(configName, defaultName);
} }
public static String getServicePackagePath() { public static String getServicePackagePath() {
String moduleName = getServiceModuleName(); String moduleName = getServiceModuleName();
String configPath = CONFIG.getString(CfgConsts.PACKAGE_NAME_SERVICE); String configName = CONFIG.getString(CfgConsts.PACKAGE_NAME_SERVICE);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_NAME_MAPPER; String defaultName = CfgConsts.DEFAULT_PACKAGE_NAME_MAPPER;
return getPathBasedPackage(moduleName, configPath, defaultPath); return getPathBasedPackage(moduleName, configName, defaultName);
} }
public static String getControllerPackagePath() { public static String getControllerPackagePath() {
String moduleName = getControllerModuleName(); String moduleName = getControllerModuleName();
String configPath = CONFIG.getString(CfgConsts.PACKAGE_NAME_CONTROLLER); String configName = CONFIG.getString(CfgConsts.PACKAGE_NAME_CONTROLLER);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_NAME_CONTROLLER; String defaultName = CfgConsts.DEFAULT_PACKAGE_NAME_CONTROLLER;
return getPathBasedPackage(moduleName, configPath, defaultPath); return getPathBasedPackage(moduleName, configName, defaultName);
} }
public static String getViewPackagePath() { public static String getViewPackagePath() {
String moduleName = getControllerModuleName(); String moduleName = getControllerModuleName();
String configPath = CONFIG.getString(CfgConsts.PACKAGE_NAME_VIEW); String configName = CONFIG.getString(CfgConsts.PACKAGE_NAME_VIEW);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_NAME_VIEW; String defaultName = CfgConsts.DEFAULT_PACKAGE_NAME_VIEW;
return getPathBasedPackage(moduleName, configPath, defaultPath); return getPathBasedPackage(moduleName, configName, defaultName);
} }
public static String getFormPackagePath() { public static String getFormPackagePath() {
String moduleName = getControllerModuleName(); String moduleName = getControllerModuleName();
String configPath = CONFIG.getString(CfgConsts.PACKAGE_NAME_FORM); String configName = CONFIG.getString(CfgConsts.PACKAGE_NAME_FORM);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_NAME_FORM; String defaultName = CfgConsts.DEFAULT_PACKAGE_NAME_FORM;
return getPathBasedPackage(moduleName, configPath, defaultPath); return getPathBasedPackage(moduleName, configName, defaultName);
} }
public static String getQueryPackagePath() { public static String getQueryPackagePath() {
String moduleName = getControllerModuleName(); String moduleName = getControllerModuleName();
String configPath = CONFIG.getString(CfgConsts.PACKAGE_NAME_QUERY); String configName = CONFIG.getString(CfgConsts.PACKAGE_NAME_QUERY);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_NAME_QUERY; String defaultName = CfgConsts.DEFAULT_PACKAGE_NAME_QUERY;
return getPathBasedPackage(moduleName, configPath, defaultPath); return getPathBasedPackage(moduleName, configName, defaultName);
} }
public static String getPoPackagePath() { public static String getPoPackagePath() {
String moduleName = getRepositoryModuleName(); String moduleName = getRepositoryModuleName();
String configPath = CONFIG.getString(CfgConsts.PACKAGE_NAME_PO); String configName = CONFIG.getString(CfgConsts.PACKAGE_NAME_PO);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_NAME_PO; String defaultName = CfgConsts.DEFAULT_PACKAGE_NAME_PO;
return getPathBasedPackage(moduleName, configPath, defaultPath); return getPathBasedPackage(moduleName, configName, defaultName);
} }
public static String getMapperPackagePath() { public static String getMapperPackagePath() {
String moduleName = getRepositoryModuleName(); String moduleName = getRepositoryModuleName();
String configPath = CONFIG.getString(CfgConsts.PACKAGE_NAME_MAPPER); String configName = CONFIG.getString(CfgConsts.PACKAGE_NAME_MAPPER);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_NAME_MAPPER; String defaultName = CfgConsts.DEFAULT_PACKAGE_NAME_MAPPER;
return getPathBasedPackage(moduleName, configPath, defaultPath); return getPathBasedPackage(moduleName, configName, defaultName);
} }
private static String getFilename(String className, String suffix) { private static String getFilename(String className, String suffix) {

@ -3,16 +3,22 @@ package space.caoshd.otone.util;
import org.apache.velocity.Template; import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity; import org.apache.velocity.app.Velocity;
import space.caoshd.otone.tool.PropTools;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Properties;
public class VelocityUtils { public class VelocityUtils {
public static void render( public static void render(
String beanName, Object bean, String templatePath, String outputPath String beanName, Object bean, String templatePath, String outputPath
) { ) {
PropTools propTools = new PropTools(PathConsts.VELOCITY_PROPERTIES_PATH);
Properties properties = propTools.getProperties();
Velocity.init(properties);
VelocityContext context = new VelocityContext(); VelocityContext context = new VelocityContext();
context.put(beanName, bean); context.put(beanName, bean);
@ -24,3 +30,4 @@ public class VelocityUtils {
} }
} }
} }

@ -6,13 +6,19 @@ class PathUtilsTest {
@Test @Test
void getProjectPath() { void getProjectPath() {
String projectPath = PathUtils.getProjectPath(); String projectPath = PathUtils.getProjectBasePath();
System.out.println(projectPath); System.out.println(projectPath);
} }
@Test
void getProjectName() {
String projectName = PathUtils.getProjectName();
System.out.println(projectName);
}
@Test @Test
void getJavaPath(){ void getJavaPath(){
String javaPath = PathUtils.getJavaPath(); String javaPath = PathUtils.getJavaPath("data");
System.out.println(javaPath); System.out.println(javaPath);
} }

Loading…
Cancel
Save