SpringBoot 初始项目自动生成

大家都应该知道Spring initializr , 可以自动生成springboot项目,截图:

https://start.spring.io/


今天我们也来生成一个springboot项目。但是不是用Spring initializr ,而是用我们自己写的工具。

spring-initializr 工程截图如下:

https://pic3.zhimg.com/v2-3455f57eba073cc48dcc67417ff0baf6_b.jpg

App类定义了工程元数据:

public class App {

	String springbootVersion;

	String groupId;

	String artifactId;

	String name;

	String packName;

	String javaVersion;

	String out;

	public String getSpringbootVersion() {
		return springbootVersion;
	}

	public void setSpringbootVersion(String springbootVersion) {
		this.springbootVersion = springbootVersion;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getArtifactId() {
		return artifactId;
	}

	public void setArtifactId(String artifactId) {
		this.artifactId = artifactId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPackName() {
		return packName;
	}

	public void setPackName(String packName) {
		this.packName = packName;
	}

	public String getJavaVersion() {
		return javaVersion;
	}

	public void setJavaVersion(String javaVersion) {
		this.javaVersion = javaVersion;
	}

	public void setOut(String out) {
		this.out = out;
	}

	public String getOut() {
		return out;
	}

}

FileProcessor 为文件读写工具:

public class FileProcessor {

	// 创建文件并写入文件
	public static void mkFile(String filePath, String content) throws IOException {
		File file = new File(filePath);
		File dir = file.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		file.createNewFile();
		FileWriter writer = new FileWriter(file);
		writer.write(content);
		writer.close();
	}

	// 读文件
	public static String cat(String filePath) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		StringBuilder stringBuilder = new StringBuilder();
		String line = null;
		String ls = System.getProperty("line.separator");
		while ((line = reader.readLine()) != null) {
			stringBuilder.append(line);
			stringBuilder.append(ls);
		}
		stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		reader.close();
		return stringBuilder.toString();
	}

}

TemplateLoader为模板加载器:

public abstract class TemplateLoader {

	public abstract String load(App app) throws IOException;

	public static class PomTemplateLoader extends TemplateLoader {

		@Override
		public String load(App app) throws IOException {
			// 读取pom模板
			String pom = FileProcessor.cat(getClass().getClassLoader().getResource("template/pom.mb").getFile());
			// 替换表达式
			pom = pom.replace("${pom.version}", app.getSpringbootVersion());
			pom = pom.replace("${pom.groupId}", app.getGroupId());
			pom = pom.replace("${pom.artifactId}", app.getArtifactId());
			pom = pom.replace("${pom.name}", app.getName());
			pom = pom.replace("${pom.java}", app.getJavaVersion());
			return pom;
		}
	}

	public static class TestTemplateLoader extends TemplateLoader {

		@Override
		public String load(App app) throws IOException {
			// 读取pom模板
			String pom = FileProcessor.cat(getClass().getClassLoader().getResource("template/test.mb").getFile());
			// 替换表达式
			pom = pom.replace("${test.pack}", app.getPackName());
			return pom;
		}
	}

	public static class MainTemplateLoader extends TemplateLoader {
		@Override
		public String load(App app) throws IOException {
			// 读取pom模板
			String pom = FileProcessor.cat(getClass().getClassLoader().getResource("template/main.mb").getFile());
			// 替换表达式
			pom = pom.replace("${main.pack}", app.getPackName());
			// 首字母大写
			pom = pom.replace("${main.name}", app.getName().substring(0, 1).toUpperCase() + app.getName().substring(1));
			return pom;
		}
	}

	public static class PropertiesTemplateLoader extends TemplateLoader {
		@Override
		public String load(App app) throws IOException {
			// 读取pom模板
			String pom = FileProcessor.cat(getClass().getClassLoader().getResource("template/properties.mb").getFile());
			// 替换表达式
			pom = pom.replace("${properties.name}", app.getName());
			return pom;
		}
	}

}

Main为启动类:

public class Main {

	public static void main(String[] args) throws IOException {
		App app = createApp();
		// 项目根目录
		String root = app.getOut() + app.getName() + File.separator;
		// 1. 生成pom
		String pomContent = new TemplateLoader.PomTemplateLoader().load(app);
		FileProcessor.mkFile(root + "pom.xml", pomContent);
		// 2. 生成test
		String testContent = new TemplateLoader.TestTemplateLoader().load(app);
		String javaName = app.getName().substring(0, 1).toUpperCase() + app.getName().substring(1)
				+ "ApplicationTest.java";
		FileProcessor.mkFile(root + "src/test/java/" + app.getPackName().replaceAll("\\.", Matcher.quoteReplacement(File.separator))
				+ File.separator + javaName, testContent);
		// 3. 生成main
		String mainContent = new TemplateLoader.MainTemplateLoader().load(app);
		javaName = app.getName().substring(0, 1).toUpperCase() + app.getName().substring(1) + "Application.java";
		FileProcessor.mkFile(root + "src/main/java/" + app.getPackName().replaceAll("\\.", Matcher.quoteReplacement(File.separator))
				+ File.separator + javaName, mainContent);
		// 4. 生成properties
		String propertiesContent = new TemplateLoader.PropertiesTemplateLoader().load(app);
		FileProcessor.mkFile(root + "src/main/resources/application.properties", propertiesContent);
		System.out.println("恭喜!SpringBoot工程生成成功 :" + app.getOut() + app.getName());
	}

	static App createApp() throws FileNotFoundException {
		Yaml yaml = new Yaml();
		URL url = Main.class.getClassLoader().getResource("app.yml");
		return yaml.loadAs(new FileInputStream(url.getFile()), App.class);
	}
}

项目依赖snakeyaml maven如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.hadluo.springinit</groupId>
	<artifactId>spring-initializr</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<!--读取yml文件 -->
		<dependency>
			<groupId>org.yaml</groupId>
			<artifactId>snakeyaml</artifactId>
			<version>1.23</version>
		</dependency>

	</dependencies>
</project>

模板文件:

/template/main.mb 启动类模板

package ${main.pack};

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ${main.name}Application {
	public static void main(String[] args) {
		SpringApplication.run(${main.name}Application.class, args);
	}
}

/template/pom.mb pom文件模板

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>${pom.version}</version>
		<relativePath/> 
	</parent>
	<groupId>${pom.groupId}</groupId>
	<artifactId>${pom.artifactId}</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>${pom.name}</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>${pom.java}</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- starter web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

/template/properties.mb properties配置文件模板

server.port=8080
spring.application.name=${properties.name}

/template/test.mb 测试类模板

package ${test.pack};

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

	@Test
	void contextLoads() {
	}

}


app.yml配置文件:

springbootVersion: 2.4.10
groupId: com.hadluo
artifactId: hadluo-order
name: order
packName: com.hadluo.order
javaVersion: 8
#工程输出目录
out: C:/Users/youh/Desktop/

对标这个

启动Main类:

将order maven工程 导入到eclipse就是一个简单的Springboot工程了。

支付宝打赏 微信打赏

如果文章对您有帮助,您可以鼓励一下作者