创建服务Maven模块
1、在已导入到工作空间的项目中,找到 smartedu-projects
2、在 smartedu-projects
目录上右键点击,选择 Other :
3、创建 Maven Module,点击 “Next”
4、选择 “Create a simple project ”,填写模块名称,点击 “Next”
5、检查包结构是否正确,根项目情况确定是否需要多模块,决定 packaging 类型,点击 “Finish”
5.1、packaging 类型为jar时,表示该模块不能继续分拆;打开pom.xml并修改父级模块信息
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.knowway.smartedu</groupId>
<artifactId>smartedu-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../smartedu-parent</relativePath>
</parent>
<artifactId>smartedu-demo</artifactId>
</project>
5.2、packaging 类型为pom时,表示该模块需要继续分拆;打开pom.xml并修改父级模块信息
删除多余的 src
目录,并修改pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.knowway.smartedu</groupId>
<artifactId>smartedu-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../smartedu-parent</relativePath>
</parent>
<artifactId>smartedu-demo</artifactId>
<packaging>pom</packaging>
</project>
5.3、如果是pom类型,则需要继续添加子模块,比如创建一个 smartedu-demo-admin
最终结构如下:
6、完成模块创建后,开始工程代码构建
6.1、修改pom.xml,添加Maven依赖,下面的代码主要注意 smartedu-core
依赖和 mainClass
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>smartedu-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- For Embed Tomcat -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency> -->
<!-- For Embed Jetty -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency> -->
<!-- For Embed Undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<mainClass>com.knowway.smartedu.demo.admin.DemoAdminApplication</mainClass>
</configuration>
</plugin>
<!-- 第一步:生成可执行的启动脚本 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
</plugin>
<!-- 第二步:通过antrun插件拷贝文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
<!-- 第三步:生成离线文档 -->
<!-- http://blog.didispace.com/swagger2markup-asciidoc/ -->
<!-- First, use the swagger2markup plugin to generate asciidoc -->
<!-- <plugin> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup-maven-plugin</artifactId>
</plugin> -->
<!-- Run the generated asciidoc through Asciidoctor to generate other
documentation types, such as PDFs or HTML5 -->
<!-- <plugin> <groupId>org.asciidoctor</groupId> <artifactId>asciidoctor-maven-plugin</artifactId>
</plugin> -->
</plugins>
</build>
6.2、创建 mainClass,例如:DemoAdminApplication
在 src/main/java 目录上右键创建 package
,如: com.knowway.smartedu.demo.admin
在 com.knowway.smartedu.demo.admin
包下创建对象:DemoAdminApplication
参考已有其他项目修改 DemoAdminApplication
,最终结果如下:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.client.RestTemplate;
import com.knowway.cloud.autoconfigure.EnableServiceConfiguration;
import com.knowway.cloud.autoconfigure.EnableWebMvcConfiguration;
import com.spring4all.swagger.EnableSwagger2Doc;
import io.micrometer.core.instrument.MeterRegistry;
@EnableCaching(proxyTargetClass = true)
@EnableSwagger2Doc
@EnableFeignClients
@EnableServiceConfiguration
@EnableWebMvcConfiguration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@SpringCloudApplication
public class DemoAdminApplication implements CommandLineRunner {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
public static void main(String[] args) {
SpringApplication.run(DemoAdminApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.err.println("Spring Cloud Application(Smartedu-Demo-Admin) Started !");
}
}
6.3、从已有项目拷贝配置文件到 src/main/resources
目录
6.4、从DemoAdminApplication启动服务,当看到下面红色框中的信息时,则说明服务创建完成。
更新时间:2023-12-20 16:58