java - spring boot多模块项目如打包?
问题描述
我构建了一个简单的多模块的项目: 结构:
外层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><groupId>com.scum</groupId><artifactId>demo-package</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules> <module>demo-controller</module> <module>demo-service</module></modules><build> <plugins><plugin> <!-- The plugin rewrites your manifest --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId><configuration><!-- 指定该Main Class为全局的唯一入口 --> <mainClass>com.example.demo.DemoControllerApplication</mainClass> <layout>ZIP</layout></configuration><executions> <execution><goals> <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中--></goals><!--可以生成不含依赖包的不可执行Jar包--><!-- configuration> <classifier>exec</classifier></configuration> --> </execution></executions> </plugin></plugins>
</build>
</project>
`web 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 http://maven.apache.org/xsd/maven-4.0.0.xsd'><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>demo-controller</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo-controller</name><description>Demo project for Spring Boot</description><parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.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></properties><dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency></dependencies><build> <plugins><plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId></plugin> </plugins></build>
</project>`
能运行 打包报IndexService 不存在
package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@SpringBootApplication@Controllerpublic class DemoControllerApplication { @Autowired private IndexService indexService; public static void main(String[] args) {SpringApplication.run(DemoControllerApplication.class, args); } @RequestMapping(value = '') @ResponseBody public String index(){return indexService.Index(); }}
问题解答
回答1:这不是Spring-boot的问题,这是Maven依赖的问题
假设我有一个maven工程是酱紫的
尖括号是工程类型,前面是工程层级
iot-cloud<pom>
iot-http<jar>iot-mqtt<jar>iot-oauth<jar>iot-sql-service<jar>iot-restful<war>
iot-cloud总工程下有5个子工程,其中iot-restful是入口工程,也就是Spring-boot的启动工程,它依赖其他四个工程来提供服务,那么你就在iot-resutful中引入其它四个作为dependency,然后maven install就好了
当然啦,测试用例不打包,配置文件引入优先级还是要考虑的
iot-restful的pom.xml<dependencies><dependency> <groupId>pri.somnus</groupId> <artifactId>iot-http</artifactId> <version>0.0.1-SNAPSHOT</version></dependency><dependency> <groupId>pri.somnus</groupId> <artifactId>iot-http</artifactId> <version>0.0.1-SNAPSHOT</version></dependency><dependency> <groupId>pri.somnus</groupId> <artifactId>iot-mqtt</artifactId> <version>0.0.1-SNAPSHOT</version></dependency><dependency> <groupId>pri.somnus</groupId> <artifactId>iot-oauth</artifactId> <version>0.0.1-SNAPSHOT</version></dependency><dependency> <groupId>pri.somnus</groupId> <artifactId>iot-sql-service</artifactId> <version>0.0.1-SNAPSHOT</version></dependency> </dependencies>
相关文章:
1. javascript - 静态页面引公共头尾文件,js怎么写吖?2. javascript - 读取页面源码,页面中所有的换行都被当成<br/>读取出来 了,,求解应该怎么让它被正确的解析3. java基础,求解答。4. javascript - 关于一段 for 循环代码执行顺序的问题5. javascript - vue 数据更新了。但是dom没有更新,,,,,如图6. html5 - 百度Ueditor代码高亮和代码段滚动条冲突是怎么回事?7. java - 为什么要将Runnable接口的子类对象传递给Thread的构造函数?8. debian - docker依赖的aufs-tools源码哪里可以找到啊?9. docker api 开发的端口怎么获取?10. javascript - HTML 原生js怎么控制table根据0,1值来显示或者隐藏