Spring boot maven多模块打包踩坑

最近折腾了两次spring boot在maven下的多模块打包,踩了很多坑,现在记录如下。

项目目录:

  • 项目 P
  • 模块 A
  • 模块 B
  • 公有基础模块 C
  • Mybatis基础模块 M

父pom.xml文件:

  
  <groupId>com.parentgroupId>
  <artifactId>demoartifactId>
  <version>0.0.1-SNAPSHOTversion>

  
  <modules>
    <modules>Cmodules>
    <modules>Mmodules>
  modules>

  
  <parent>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-parentartifactId>
      <version>2.0.0.M5version>
  parent>

  
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.modulegroupId>
        <artifactId>cartifactId>
        <version>${project.version}version>
      dependency>
      <dependency>
        <groupId>com.modulegroupId>
        <artifactId>martifactId>
        <version>${project.version}version>
      dependency>
    dependencies>
  dependencyManagement>

  
  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    <java.version>1.8java.version>
  properties>

  
  <dependencies>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starterartifactId>
    dependency>
  dependencies>

  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.1version>
          <configuration>
            <source>${java.version}source>
            <target>${java.version}target>
            configuration>
      plugin>

      <plugin>
          <groupId>org.apache.maven.pluginsgroupId>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.19.1version>
            <configuration>
              <skipTests>trueskipTests>    
            configuration>
      plugin>
    plugins>
  build>

模块A的pom.xml

  <groupId>com.modulegroupId>
  <artifactId>aartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  
  <parent>
    <groupId>com.parentgroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <relativePath>../pom.xmlrelativePath>
  parent>

  
  <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
          <configuration>
            
            <mainClass>com.module.a.ApplicationmainClass>
            <layout>ZIPlayout>
          configuration>
          <executions>
            <execution>
              <goals>
                <goal>repackagegoal>
              goals>
            execution>
          executions>
      plugin>
		plugins>
	build>

模块B的pom.xml

同A即可

模块C的pom.xml

如果是共有模块的话,不需要打包,否则会报错,因为其他模块在打包的时候会自动添加依赖进去,如果这里打包了,其他的模块就找不到该依赖了。

  <groupId>com.modulegroupId>
	<artifactId>cartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<packaging>jarpackaging>

  
  <parent>
    <groupId>com.parentgroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <relativePath>../pom.xmlrelativePath>
  parent>

模块M的pom.xml

如果项目中使用的Mybatis的话,肯定是作为一个单独的模块来处理的,这个Mybatis是需要打包的

  <groupId>com.modulegroupId>
  <artifactId>martifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  
  <parent>
    <groupId>com.parentgroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <relativePath>../pom.xmlrelativePath>
  parent>

  
  <build>
		<plugins>
			<plugin>
				<groupId>org.mybatis.generatorgroupId>
				<artifactId>mybatis-generator-maven-pluginartifactId>
				<version>1.3.5version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifactsid>
						<phase>nonephase>
						<goals>
							<goal>generategoal>
						goals>
					execution>
				executions>
				<configuration>
					<overwrite>trueoverwrite>
				configuration>
			plugin>
		plugins>
	build>

打包

按照上面的配置好以后,执行下面的命令就好了

  mvn clean package

但是如果使用了多个模块,上面的命令是会吧全部的模块都执行打包的,如果只是打包某个模块的话,可以用

  mvn -pl A -am install

你可能感兴趣的:(Spring boot maven多模块打包踩坑)