Eclipse Maven创建Spring Boot Project Module

时间:2023-01-22 12:54:51

通过Eclipse的Maven创建Spring Boot Project Module机构工程,并实现eureka

一、创建Maven Project

File -> New -> Maven -> Maven Project

Eclipse Maven创建Spring Boot Project Module

勾选“Create a simple project(skip archetype selection)”

Eclipse Maven创建Spring Boot Project Module

Packaging选择pom,然后点“Finish”即完成“ershuai-blog”创建


二、ershuai-blog的pom.xml

给“ershuai-blog”的pom.xml加配置

一定要记得加 dependencyManagement。不然子项目没法加<parent>,因为spring boot会占用<parent>

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-boot.version>1.5.7.RELEASE</spring-boot.version>
		<spring-cloud.version>Dalston.SR3</spring-cloud.version>
	</properties>

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

		<!-- spring boot 默认支持logback日志框架,logging是关联包 -->
		<!-- 控制台输出日志级别:TRACE < DEBUG < INFO < WARN < ERROR < FATAL。默认INFO -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-logging</artifactId>
		</dependency>

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

	<!-- 加入该配置后,spring boot类型的子项目,可不用配置org.springframework.boot的<parent>标签 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>${spring-boot.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

三、创建子项目

选中“ershuai-blog”项目,右键 Maven -> New Maven Module Project,创建“ershuai-eureka-server”项目

勾选“Create a simple project(skip archetype selection)”

Packaging选择jar,然后点“Finish”即完成“ershuai-eureka-server”创建

Eclipse Maven创建Spring Boot Project Module


四、ershuai-eureka-server的pom.xml

這个子项目是作为eureka使用的,所以引入eureka包

子项目要打成jar包运行,一定要加 spring-boot-maven-plugin,不然打出来的jar包MANIFEST.MF信息是不正确的,没法正常运行。可能会提示“没有主清单属性”错误什么的

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>
	
	<!-- spring boot 打jar包,正常运行需要该配置。否则可能提示jar中没有主清单属性,等错误信息 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>${spring-boot.version}</version>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


五、application.yml配置

eureka的application.yml配置

spring:
  application:
    name: ershuai-eureka-server

server:
  port: 1001
  
eureka:
  client:
    register-with-eureka: false # 是否注册自身到eureka服务器
    fetch-registry: false # 是否从eureka上获取注册信息
  instance:
    hostname: localhost
    instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
    prefer-ip-address: true # 注册时使用ip而不是主机名
  #server:
    #enable-self-preservation: false # 开启保护机制


六、启动

6.1、main方法运行

/**
 * 
 * @author ershuai
 * @date 2018年4月23日 下午6:33:29
 * 
 * jar 运行方式
 * java -jar ershuai-eureka-server-0.0.1-SNAPSHOT.jar
 * 
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

6.2、jar包运行

选中“ershuai-eureka-server”项目,右键,Run As -> Maven Install。成功后,在target文件夹下,将会产生jar包

windows下,cmd到该目录下,执行命令:java -jar xxxx.jar。即可运行


七、访问eureka

http://localhost:1001/

Eclipse Maven创建Spring Boot Project Module