如题:导入一个新的spring boot项目作为spring cloud的一个子模块微服务,怎么做
或者说每次导入一个新的spring boot项目,IDEA不识别,启动类无法启动,怎么解决
下面分别展示[tx-manager只是作为例子进行操作,你可以把这个服务名字想象成例如springcloud-ms-xxx等等都可以]
一.只需要将一个spring boot服务识别为子模块,不会建立真正的父子依赖关系
将tx-Manager修改为本微服务组可以识别的子模块module
导入更新
成功识别为子module
二.需要建立真正父子依赖关系的 子模块识别
下面一起来走一遍这个流程:
1.将一个spring boot服务导入spring cloud中作为一个子模块
如图:这里有一个现成的spring cloud微服务集群,【如何创建一个spring cloud微服务:https://www.cnblogs.com/sxdcgaq8080/p/9035724.html】
然后
打开本服务根目录在资源管理器下:
可以看到根目录下的现有的两个ms 和parent的pom文件
然后将想要加入的外部的一个spring boot项目粘贴到此处【将tx-manager整个目录拷贝过来】
如下:
修该名字为统一的ms名字
这个时候,再打开idea ,看到
2.解决上面导入但是没有被识别的问题
所以先解决这个问题:
现将java目录设置为tx-manager这个微服务的跟路径
同理,设置resource目录为根资源路径
ok。现在就剩最后一个pom.xml文件的转变了
3.整理pom.xml文件内容,建立和最外层parent的pom之间的父子依赖关系
先看一下最外层的parent的pom.xml完整的依赖:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.swapping</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--
注意 最后打包 最外层的pom 需要更改为 pom而不是jar
<packaging>jar</packaging>
-->
<packaging>pom</packaging> <name>springcloud</name>
<description>Demo project for Spring cloud</description> <modules>
<module>springcloud-ms-eureka</module>
<module>springcloud-ms-member</module>
</modules> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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-cloud.version>2.0.0.RELEASE</spring-cloud.version>
<lcn.last.version>4.1.0</lcn.last.version>
</properties> <dependencies> <!--========================spring boot 基础依赖 start ==============================-->
<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>
<!--========================spring boot 基础依赖 start ==============================--> <!--最外层 主项目 添加spring cloud依赖 https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>${spring-cloud.version}</version>
</dependency> <!-- ========================LCN 分布式事务 start============================-->
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-springcloud</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- ========================LCN 分布式事务 END============================--> <!-- ======================== DB相关所有jar start =============================--> <!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency> <!-- 使用mysql数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <!-- 假设 每一个子模块都会 使用 jpa 和 mybatis 操作数据库 才将相关jar放在父级pom中 --> <!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <!-- mybatis 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency> <!-- ======================== DB相关所有jar end =============================--> <!-- ======================== redis start ======================== -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- ======================== redis end ======================== --> <!-- ======================== 公用 工具包 start ==============================-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency> <dependency>
<groupId>com.xiaoleilu</groupId>
<artifactId>hutool-all</artifactId>
<version>3.3.2</version>
</dependency> <dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency> <!-- base64加密解密 -->
<dependency>
<groupId>org.apache.ws.commons.util</groupId>
<artifactId>ws-commons-util</artifactId>
<version>1.0.2</version>
</dependency> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency> <!-- ======================== 公用 工具包 end ==============================--> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
再看一下现在的ms-member这个现成的子module的pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.swapping</groupId>
<artifactId>springcloud-ms-member</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springcloud-ms-member</name>
<description>ms-member is consumer ,transfer ms-order and ms-integral</description> <parent>
<groupId>com.swapping</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> </project>
最后看一下现在的tx-manage的pom.xml依赖:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.codingapi</groupId>
<artifactId>tx-manager</artifactId>
<version>4.2.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>tx-manager</name>
<description>tx-manager</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compile.source>1.7</maven.compile.source>
<maven.compile.target>1.7</maven.compile.target>
<java.version>1.7</java.version> <guava.version>19.0</guava.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
<maven.deploy.skip>true</maven.deploy.skip>
<maven.install.skip>true</maven.install.skip> </properties> <dependencies> <dependency>
<groupId>com.github.1991wangliang</groupId>
<artifactId>lorne_core</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency> <!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>-->
<!--</dependency>--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<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> <build>
<plugins> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compile.source}</source>
<target>${maven.compile.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>com.codingapi.tm.TxManagerApplication</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>config/**</exclude>
</excludes>
</configuration>
</plugin> <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/build/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugins>
</build> </project>
然后将tx-manager的pom按照member的更改:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.codingapi</groupId>
<artifactId>springcloud-ms-tx-manager</artifactId>
<version>4.2.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>springcloud-ms-tx-manager</name>
<description>tx-manager, LCN's distributed transaction coordinator</description> <parent>
<groupId>com.swapping</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>com.github.1991wangliang</groupId>
<artifactId>lorne_core</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency> </dependencies> </project>
并且在parent的pom.xml文件新增tx-manager子模块
最后导入:点击import changes
最后可以看到 这个子模块的pom文件终于被识别,并且父子依赖关系已经建立起来:
4.最后将tx-manager真正设立成子module,让子module的蓝色图标显示出来
到了这里,还不是真正的子模块
那如何让这个子模块的蓝色图标显示出来呢?
setting -->build,Execution,Deployment-->Build tools --> Maven -->Ignored Files -->勾选忽略的文件
取消勾选以后,就可以看到 子模块真正成功
至此,导入外部项目 即可成功 导入成子模块!!!并且被识别为子模块!!