• maven==docker搭建私有maven仓库,编写starter并发布到私有maven仓库,在项目中使用私有仓库中的starter



    docker创建maven私有仓库

    Linux使用docker搭建Maven私有仓库_Icoolkj的博客-CSDN博客_docker搭建maven仓库

    docker run --name docker-nexus3 -p 8081:8081 -v /usr/local/nexus3/nexus-data:/nexus-data -d sonatype/nexus3

    ===============================================================

    编写starter项目并推送到私有仓库:

      如何自定义一个springboot starter(超详细)_johnhum123的博客-CSDN博客_springboot自定义starter

      maven打包上传到私有仓库的步骤_茁壮成长的凌大大的博客-CSDN博客_maven发布到私有仓库

     

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>org.examplegroupId>
    6. <artifactId>test-spring-boot-starterartifactId>
    7. <version>0.3version>
    8. <parent>
    9. <artifactId>spring-boot-starter-parentartifactId>
    10. <groupId>org.springframework.bootgroupId>
    11. <version>2.5.2version>
    12. parent>
    13. <properties>
    14. <maven.compiler.source>8maven.compiler.source>
    15. <maven.compiler.target>8maven.compiler.target>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.springframework.bootgroupId>
    20. <artifactId>spring-boot-configuration-processorartifactId>
    21. <optional>trueoptional>
    22. dependency>
    23. <dependency>
    24. <groupId>org.springframework.bootgroupId>
    25. <artifactId>spring-boot-autoconfigureartifactId>
    26. dependency>
    27. dependencies>
    28. <distributionManagement>
    29. <repository>
    30. <id>maven-releasesid>
    31. <name>Nexus Release Repositoryname>
    32. <url>http://162.14.114.186:8081/repository/maven-releases/url>
    33. repository>
    34. <snapshotRepository>
    35. <id>maven-snapshotsid>
    36. <name>Nexus Snapshot Repositoryname>
    37. <url>http://162.14.114.186:8081/repository/maven-snapshots/url>
    38. snapshotRepository>
    39. distributionManagement>
    40. <build>
    41. <plugins>
    42. <plugin>
    43. <groupId>org.apache.maven.pluginsgroupId>
    44. <artifactId>maven-source-pluginartifactId>
    45. <version>3.0.1version>
    46. <configuration>
    47. <attach>trueattach>
    48. configuration>
    49. <executions>
    50. <execution>
    51. <phase>compilephase>
    52. <goals>
    53. <goal>jargoal>
    54. goals>
    55. execution>
    56. executions>
    57. plugin>
    58. plugins>
    59. build>
    60. project>
    1. package org.example.test.spring.boot.starter;
    2. import java.util.List;
    3. /**
    4. * @author Johnhum on 2021/7/18
    5. */
    6. public interface ISplitService {
    7. List split(String value);
    8. }

     

    1. package org.example.test.spring.boot.starter;
    2. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    3. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. @Configuration
    7. @ConditionalOnClass(value = {ISplitService.class, SplitServiceImpl.class})
    8. public class SplitAutoConfigure {
    9. @Bean
    10. @ConditionalOnMissingBean
    11. ISplitService startService() {
    12. return new SplitServiceImpl();
    13. }
    14. }
    1. package org.example.test.spring.boot.starter;
    2. import org.springframework.util.StringUtils;
    3. import java.util.List;
    4. import java.util.stream.Collectors;
    5. import java.util.stream.Stream;
    6. /**
    7. * @author Johnhum on 2021/7/18
    8. */
    9. public class SplitServiceImpl implements ISplitService {
    10. @SuppressWarnings("all")
    11. @Override
    12. public List split(String value) {
    13. return Stream.of(StringUtils.split(value, ",")).collect(Collectors.toList());
    14. }
    15. }

    \test-spring-boot-starter\test-spring-boot-starter\src\main\resources\META-INF\spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.test.spring.boot.starter.SplitAutoConfigure
    

     mvn deploy

     成功上传了

     

     ======================================

    清空本地maven仓库的所有依赖包,然后新建一个maven项目,引入上面新建的starter

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <parent>
    5. <groupId>org.springframework.bootgroupId>
    6. <artifactId>spring-boot-starter-parentartifactId>
    7. <version>2.5.2version>
    8. <relativePath/>
    9. parent>
    10. <groupId>com.examplegroupId>
    11. <artifactId>test-use-starter2artifactId>
    12. <version>0.0.1-SNAPSHOTversion>
    13. <name>test-use-starter2name>
    14. <description>test-use-starter2description>
    15. <properties>
    16. <java.version>1.8java.version>
    17. properties>
    18. <dependencies>
    19. <dependency>
    20. <groupId>org.springframework.bootgroupId>
    21. <artifactId>spring-boot-starter-webartifactId>
    22. dependency>
    23. <dependency>
    24. <groupId>org.projectlombokgroupId>
    25. <artifactId>lombokartifactId>
    26. <optional>trueoptional>
    27. dependency>
    28. <dependency>
    29. <groupId>org.springframework.bootgroupId>
    30. <artifactId>spring-boot-starter-testartifactId>
    31. <scope>testscope>
    32. dependency>
    33. <dependency>
    34. <groupId>com.alibabagroupId>
    35. <artifactId>fastjsonartifactId>
    36. <version>2.0.3version>
    37. dependency>
    38. <dependency>
    39. <groupId>org.examplegroupId>
    40. <artifactId>test-spring-boot-starterartifactId>
    41. <version>0.3version>
    42. dependency>
    43. dependencies>
    44. <build>
    45. <plugins>
    46. <plugin>
    47. <groupId>org.springframework.bootgroupId>
    48. <artifactId>spring-boot-maven-pluginartifactId>
    49. <configuration>
    50. <excludes>
    51. <exclude>
    52. <groupId>org.projectlombokgroupId>
    53. <artifactId>lombokartifactId>
    54. exclude>
    55. excludes>
    56. configuration>
    57. plugin>
    58. plugins>
    59. build>
    60. project>

     

    1. package com.example.testusestarter2;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class TestUseStarter2Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(TestUseStarter2Application.class, args);
    8. }
    9. }
    1. package com.example.testusestarter2;
    2. import com.alibaba.fastjson.JSON;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.example.test.spring.boot.starter.ISplitService;
    5. import org.junit.jupiter.api.Test;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. @SpringBootTest
    9. @Slf4j
    10. class TestUseStarter2ApplicationTests {
    11. @Test
    12. void contextLoads() {
    13. }
    14. @Autowired
    15. private ISplitService splitServiceImpl;
    16. @Test
    17. public void splitTest() {
    18. log.info("split context: {}", JSON.toJSONString(splitServiceImpl.split("8888,6666666666")));
    19. }
    20. }

     

     

     

    ===============================================================

    原理,springboot注解@Configuration

    @Configuration就是个bean工厂,工厂类中的方法就是生成bean的方法

    见spring refresh和spring bean源码分析 

  • 相关阅读:
    MySQL进阶语句
    【React】01-React的入门
    Women of Polkadot:波卡生态的女性社群与创新力量
    第一次写计算机论文无从下手怎么办?(一) - 易智编译easeediting
    3分钟从零解读Transformer的Encoder
    C++中的类模板(黑马程序员)
    稻盛和夫-如是说(读书笔记)
    AWK语言第二版 2.3转换
    acwing算法提高之数据结构--树状数组
    2011年09月06日 Go生态洞察:Go语言的反射法则
  • 原文地址:https://blog.csdn.net/hebian1994/article/details/126084836