springboot一个项目多模块打包踩过的坑

转载:https://blog.csdn.net/qq_28929589/article/details/79267467

其实主要几点:
切记父pom,xml和不需要打包的模块不要写下面一段,在主模块写就行。
因为我的主模块需要到对应包中的依赖,打包了就找不到

  <build>
         <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                 </plugin>   
        </plugins>    
    </build>

详情:
项目结构
这里写图片描述

core不依赖谁

user依赖core

hr依赖user和core

hr是主模块,application的入口放在这里的

如果以后我在做一个项目比如hr1,我可以再次去依赖core和user。

core和user。就可以公用了。。。。

其次我的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>
	 <packaging>pom</packaging>
	 <!-- 项目说明:这里作为聚合工程的父工程 -->
	 <groupId>com.ewe</groupId>
	 <artifactId>ewe-hr</artifactId>
	 <version>1.0.0.RELEASE</version>
	 
	  <!-- 继承说明:这里继承SpringBoot提供的父工程 -->
	 <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.5.9.RELEASE</version>
	 </parent>
  	<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>
	 <!-- 模块说明:这里声明多个子模块 -->
     <modules>
        <module>core</module>
        <module>hr</module>
        <module>user</module>
     </modules>
     <dependencyManagement>
      <!-- 模块说明:这里声明多个子模块 -->
	 	 <dependencies>
	 		<!--子版本依赖-->
	        <dependency>
	            <groupId>com.ewe</groupId>
	            <artifactId>core</artifactId>
	            <version>0.0.1-SNAPSHOT</version>
	        </dependency>  
	        <dependency>
	            <groupId>com.ewe</groupId>
	            <artifactId>user</artifactId>
	            <version>0.0.1-SNAPSHOT</version>
	        </dependency>
	        <dependency>
	            <groupId>com.ewe</groupId>
	            <artifactId>hr</artifactId>
	            <version>0.0.1-SNAPSHOT</version>
	        </dependency>
		</dependencies>
	 </dependencyManagement>
</project>

切记父文件不需要打包。
不需要写

	<build>
         <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                 </plugin>   
        </plugins>    
    </build>

再次我的core文件

<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>
	 <packaging>pom</packaging>
	 <!-- 项目说明:这里作为聚合工程的父工程 -->
	 <groupId>com.ewe</groupId>
	 <artifactId>ewe-hr</artifactId>
	 <version>1.0.0.RELEASE</version>
	 
	  <!-- 继承说明:这里继承SpringBoot提供的父工程 -->
	 <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.5.9.RELEASE</version>
	 </parent>
  	<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>
	 <!-- 模块说明:这里声明多个子模块 -->
     <modules>
        <module>core</module>
        <module>hr</module>
        <module>user</module>
     </modules>
     <dependencyManagement>
      <!-- 模块说明:这里声明多个子模块 -->
	 	 <dependencies>
	 		<!--子版本依赖-->
	        <dependency>
	            <groupId>com.ewe</groupId>
	            <artifactId>core</artifactId>
	            <version>0.0.1-SNAPSHOT</version>
	        </dependency>  
	        <dependency>
	            <groupId>com.ewe</groupId>
	            <artifactId>user</artifactId>
	            <version>0.0.1-SNAPSHOT</version>
	        </dependency>
	        <dependency>
	            <groupId>com.ewe</groupId>
	            <artifactId>hr</artifactId>
	            <version>0.0.1-SNAPSHOT</version>
	        </dependency>
		</dependencies>
	 </dependencyManagement>
</project>

我的core文件不需要打包,因为我的主项目hr需要core包中的依赖,打包了就找不到了
然后我的user文件

<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.ewe</groupId>
    <artifactId>ewe-hr</artifactId>
    <version>1.0.0.RELEASE</version>
     <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>user</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>user</name>
  <description>user information</description>
  <dependencies>
      <dependency>
          <groupId>com.ewe</groupId>
          <artifactId>core</artifactId>
      </dependency>  
      
  </dependencies>
 <!-- Package as an executable jar -->
    <build>
		  <resources>
		   <resource>
		       <directory>src/main/java</directory>
		       <includes>
		           <include>**/*.xml</include>
		       </includes>
		   </resource>
		   <resource>
		       <directory>src/main/resources</directory>
		       <includes>
		           <include>**.*</include>
		           <include>**/*.*</include><!-- i18n能读取到 -->
		            <include>**/*/*.*</include>
		       </includes>
		   </resource>
		 </resources>
    </build>
</project>

为什么user有build呢,因为依赖的打包的时候我只是指明了需要打包的目录resources
最后我的住主项目hr

<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>
  <artifactId>hr</artifactId>
  <name>hr</name>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
   <!-- 继承本项目的父工程 -->
    <parent>
        <groupId>com.ewe</groupId>
        <artifactId>ewe-hr</artifactId>
        <version>1.0.0.RELEASE</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
  	<dependencies>
      <dependency>
          <groupId>com.ewe</groupId>
          <artifactId>core</artifactId>
      </dependency>  
      <dependency>
          <groupId>com.ewe</groupId>
          <artifactId>user</artifactId>
      </dependency>  
  	</dependencies>
     <!-- Package as an executable jar -->
    <build>
		  <resources>
		   <resource>
		       <directory>src/main/java</directory>
		       <includes>
		           <include>**/*.xml</include>
		       </includes>
		   </resource>
		   <resource>
		       <directory>src/main/resources</directory>
		       <includes>
		           <include>**.*</include>
		           <include>**/*.*</include><!-- i18n能读取到 -->
		            <include>**/*/*.*</include>
		       </includes>
		   </resource>
		 </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
           
            <configuration>
	             <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
	            <!-- 指定该Main Class为全局的唯一入口 -->
	            <mainClass>com.ewe.Application</mainClass>
	            <layout>ZIP</layout>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
              </goals>
            </execution>
          </executions>
          </plugin>
        </plugins>
        <finalName>hr</finalName>
    </build>
</project>

需要进行打包并指明main函数的入口
以上是解析如果有帮助你,点个赞,我也是从痛苦中过来的

  • 12
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值