当前位置:   article > 正文

maven==docker搭建私有maven仓库,编写starter并发布到私有maven仓库,在项目中使用私有仓库中的starter_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. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>test-spring-boot-starter</artifactId>
  8. <version>0.3</version>
  9. <parent>
  10. <artifactId>spring-boot-starter-parent</artifactId>
  11. <groupId>org.springframework.boot</groupId>
  12. <version>2.5.2</version>
  13. </parent>
  14. <properties>
  15. <maven.compiler.source>8</maven.compiler.source>
  16. <maven.compiler.target>8</maven.compiler.target>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-configuration-processor</artifactId>
  22. <optional>true</optional>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-autoconfigure</artifactId>
  27. </dependency>
  28. </dependencies>
  29. <!--项目分发信息,在执行mvn deploy后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件jar等部署到远程仓库。 -->
  30. <distributionManagement>
  31. <repository><!--部署项目产生的构件到远程仓库需要的信息 -->
  32. <id>maven-releases</id><!-- 此处id和settings.xml的id保持一致 -->
  33. <name>Nexus Release Repository</name>
  34. <url>http://162.14.114.186:8081/repository/maven-releases/</url>
  35. </repository>
  36. <snapshotRepository><!--构件的快照部署到哪里?如果没有配置该元素,默认部署到repository元素配置的仓库,参见distributionManagement/repository元素 -->
  37. <id>maven-snapshots</id><!-- 此处id和settings.xml的id保持一致 -->
  38. <name>Nexus Snapshot Repository</name>
  39. <url>http://162.14.114.186:8081/repository/maven-snapshots/</url>
  40. </snapshotRepository>
  41. </distributionManagement>
  42. <build>
  43. <plugins>
  44. <!-- 上传源码 -->
  45. <plugin>
  46. <groupId>org.apache.maven.plugins</groupId>
  47. <artifactId>maven-source-plugin</artifactId>
  48. <version>3.0.1</version>
  49. <configuration>
  50. <attach>true</attach>
  51. </configuration>
  52. <executions>
  53. <execution>
  54. <phase>compile</phase>
  55. <goals>
  56. <goal>jar</goal>
  57. </goals>
  58. </execution>
  59. </executions>
  60. </plugin>
  61. </plugins>
  62. </build>
  63. </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<String> 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<String> 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. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.5.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>test-use-starter2</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>test-use-starter2</name>
  15. <description>test-use-starter2</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.alibaba</groupId>
  36. <artifactId>fastjson</artifactId>
  37. <version>2.0.3</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.example</groupId>
  41. <artifactId>test-spring-boot-starter</artifactId>
  42. <version>0.3</version>
  43. </dependency>
  44. </dependencies>
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. <configuration>
  51. <excludes>
  52. <exclude>
  53. <groupId>org.projectlombok</groupId>
  54. <artifactId>lombok</artifactId>
  55. </exclude>
  56. </excludes>
  57. </configuration>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </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源码分析 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/866629
推荐阅读
相关标签
  

闽ICP备14008679号