父Pom和微服务

发布于 2021-01-30 17:18:26

  • 我们有几个微服务项目,每个项目都是独立的(在单独的spring boot服务器上运行,公开其余服务,使用单独的DB模式…)
  • 我们使用Maven管理依赖关系。

有一个父pom将每个微服务声明为模块是一个好主意吗?因此,有助于管理公共依赖项(例如,在每个项目中都使用lib servlet-api
witch,将其全部删除并仅在父pom中声明)

关注者
0
被浏览
79
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。

    多模块父pom的“问题”是,没有复杂的配置文件,它将模块锁定在相同的发布周期中(假设您使用的是Release
    Plugin
    )。

    我使用Maven的方式是让一个父pom声明:

    每个模块都会将父pom作为其父代,但父代对模块一无所知。

    这样做的好处来自上面的最后两个项目符号,即“管理”部分。“管理”部分中包含的所有内容都需要在要使用特定依赖项或插件的模块中重新声明。

    例如,父母可能看起来像这样:

    <project>
    
      <groupId>com.example</groupId>
      <artifactId>parent</artifactId>
      <version>1.0.00-SNAPSHOT</version>
    
      ...
    
      <dependencies>
    
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.7</version>
        </dependency>
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
      </dependencies>
    
      <dependencyManagement>
    
        <dependency>
          <groupId>commons-lang</groupId>
          <artifactId>commons-lang</artifactId>
          <version>2.6</version>
        </dependency>
    
        <dependency>
          <groupId>commons-collections</groupId>
          <artifactId>commons-collections</artifactId>
          <version>2.1</version>
        </dependency>
    
      </dependencyManagement>
    
      <plugins>
    
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
          </configuration>
        </plugin>
    
      <plugins>
    
      <pluginManagement>
    
        <plugins>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
              <appendAssemblyId>false</appendAssemblyId>
              <descriptors>
                <descriptor>src/main/assembly/assembly.xml</descriptor>
              </descriptors>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    
        </plugins>
    
      </pluginManagement>
    
    </project>
    

    模块可能看起来像这样:

    <project>
    
      <parent>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.00-SNAPSHOT</version>
      </parent>
    
      <groupId>com.example</groupId>
      <artifactId>module</artifactId>
      <version>1.0.00-SNAPSHOT</version>
    
      <dependencies>
    
        <dependency>
          <groupId>commons-lang</groupId>
          <artifactId>commons-lang</artifactId>          
        </dependency>
    
      </dependencies>
    
      <plugins>
    
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
        </plugin>
    
      </plugins>
    
    </project>
    

    该模块将:

    • 有依赖性org.slf4j:slf4j-api:1.7.7:compilejunit:junit:4.11:testcommons-lang:commons-lang:2.6:compile
    • 有插件 org.apache.maven.plugins:maven-assembly-plugin:2.4


推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看