天天看點

項目之建立靜态資源和設定子子產品項目、開發簡易上傳功能(11)

48. 在父項目中管理子子產品項目使用的依賴

在一個項目中,如果某些依賴隻是部分子子產品項目需要使用的,應該将這些依賴配置在<dependencyManagement>節點中,凡配置在這個節點中的依賴,任何子子產品項目中都不會直接擁有,如果某個子子產品項目需要使用這些依賴,依然需要使用<dependency>節點來添加!與在子子產品項目中直接添加<denpendency>(父級的<dependencyManagement>沒有配置某個依賴而子子產品項目中直接添加)的差別在于:如果事先使用父級項目的<dependencyManagement>進行了配置,則子子產品項目在添加時,不需要指定版本号,直接使用父級項目配置的版号,以便于在父級項目中統一管理依賴的版本!

注意:在父級項目中,添加許多依賴都是不需要指定版本号的,但是,如果将這些依賴配置到<dependencyManagement>中用于指導子子產品項目所使用的依賴的版本時,必須顯式的指定版本号,否則,子子產品項目将不明确需要使用的是哪個版本!

則在父級項目中,關于依賴的管理:

<properties>
    <!-- Java Version -->
    <java.version>1.8</java.version>
    <!-- Dependency Version -->
    <mysql.version>8.0.20</mysql.version>
    <mybatis.version>2.1.3</mybatis.version>
    <mybatis.plus.version>3.3.2</mybatis.plus.version>
    <druid.version>1.1.23</druid.version>
    <pagehelper.version>1.2.13</pagehelper.version>
    <thymeleaf.springsecurity5.version>3.0.4.RELEASE</thymeleaf.springsecurity5.version>
    <spring.boot.starter.version>2.3.1.RELEASE</spring.boot.starter.version>
    <lombok.version>1.18.12</lombok.version>
</properties>

<!-- 直接添加在dependencies節點的中的依賴是每個子子產品項目都直接擁有的 -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.starter.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<!-- 添加在dependencyManagement中的依賴隻是為了管理子子產品項目使用依賴時的版本 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>${thymeleaf.springsecurity5.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring.boot.starter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>${spring.boot.starter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${spring.boot.starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

      

由于大量的依賴都已經添加在<dependencyManagement>中了,則straw-generator和straw-portal項目都不會直接擁有這些依賴,則需要在這2個子子產品項目中自行添加所需的依賴!

在straw-generator項目中(關于代碼生成器的相關依賴由于過于特殊,一定隻有目前項目需要使用,是以,對版本的管理方式可以不嚴格要求):

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-extension</artifactId>
        <version>3.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
        <version>2.3.1.RELEASE</version>
    </dependency>
</dependencies>

      

straw-portal

項目中:

<dependencies>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

      

繼續閱讀