SpringBoot+Maven多模块项目(17)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SpringBoot+Maven多模块项目(17),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4280字,纯文字阅读大概需要7分钟。
内容图文
![SpringBoot+Maven多模块项目(17)](/upload/InfoBanner/zyjiaocheng/1024/2764477c013c4240aae702841958299b.jpg)
工程结构:
????父模块 multimodule-parent:
????????子模块 multimodule-core
????????子模块 multimodule-web(web,唯一有启动类的模块)
????关系:
????????multimodule-web依赖 multimodule-core (需要把multimodule-core 包打到multimodule-web中)
一.创建Maven多模块项目
1.创建multimodule-parent模块
1.1创建父模块,用于管理各个模块
接下来,把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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.multimodule</groupId> <artifactId>multimodule-parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>multimodule</name> <description>多模块项目父模块</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.3.RELEASE</version> </parent> <dependencies> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> </dependencies> <modules> <module>multimodule-core</module> <module>multimodule-web</module> </modules> </project>
1.2创建子模块multimodule-core
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example.multimodule</groupId> <artifactId>multimodule-parent</artifactId> <version>1.0.0</version> </parent> <artifactId>multimodule-core</artifactId> </project>
1.3 创建子模块multimodule-web
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example.multimodule</groupId> <artifactId>multimodule-parent</artifactId> <version>1.0.0</version> </parent> <artifactId>multimodule-web</artifactId> <packaging>jar</packaging> <name>multimodule-web</name> <description>multimodule-web依赖 multimodule-core </description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.example.multimodule</groupId> <artifactId>multimodule-core</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 指定该Main Class为全局的唯一入口 --> <mainClass>com.example.multimodule.web.WebApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中--> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
二.代码测试
2.1multimodule-core模块下新建CoreTest类
package com.example.multimodule.core; public class CoreTest { public String show() { return "这是一个子模块core"; } }
2.2multimodule-web模块下新建WebApplication类
package com.example.multimodule.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class WebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(WebApplication.class, args); } }
2.3multimodule-web模块下新建WebTestController类
package com.example.multimodule.web; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.multimodule.core.CoreTest; @RestController public class WebTestController { @GetMapping("/show") public String test() { CoreTest c=new CoreTest(); return c.show(); } }
三.打包运行测试
3.1Maven install
? ?右键multimodule-core项目,选择Maven install
复制打包成功的multimodule-web-1.0.0.jar,把它拷贝到一个目录
3.2 java -jar 运行jar包
访问http://localhost:8080/show 地址(默认不带项目名),返回结果如下,证明最后的multimodule-web-1.0.0.jar包把要依赖的multimodule-core包,也打进去,并且整个springboot 项目打包成功.
内容总结
以上是互联网集市为您收集整理的SpringBoot+Maven多模块项目(17)全部内容,希望文章能够帮你解决SpringBoot+Maven多模块项目(17)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。