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发布到私有仓库

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test-spring-boot-starter</artifactId>
    <version>0.3</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.2</version>
    </parent>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>

    <!--项目分发信息,在执行mvn deploy后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件jar等部署到远程仓库。 -->
    <distributionManagement>
        <repository><!--部署项目产生的构件到远程仓库需要的信息 -->
            <id>maven-releases</id><!-- 此处id和settings.xml的id保持一致 -->
            <name>Nexus Release Repository</name>
            <url>http://162.14.114.186:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository><!--构件的快照部署到哪里?如果没有配置该元素,默认部署到repository元素配置的仓库,参见distributionManagement/repository元素 -->
            <id>maven-snapshots</id><!-- 此处id和settings.xml的id保持一致 -->
            <name>Nexus Snapshot Repository</name>
            <url>http://162.14.114.186:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <build>
        <plugins>
            <!-- 上传源码 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>
package org.example.test.spring.boot.starter;

import java.util.List;

/**
 * @author Johnhum on 2021/7/18
 */
public interface ISplitService {
    List<String> split(String value);
}

package org.example.test.spring.boot.starter;


import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConditionalOnClass(value = {ISplitService.class, SplitServiceImpl.class})
public class SplitAutoConfigure {
    @Bean
    @ConditionalOnMissingBean
    ISplitService startService() {
        return new SplitServiceImpl();
    }
}
package org.example.test.spring.boot.starter;


import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Johnhum on 2021/7/18
 */
public class SplitServiceImpl implements ISplitService {
    @SuppressWarnings("all")
    @Override
    public List<String> split(String value) {
        return Stream.of(StringUtils.split(value, ",")).collect(Collectors.toList());
    }
}

\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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test-use-starter2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test-use-starter2</name>
    <description>test-use-starter2</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>test-spring-boot-starter</artifactId>
            <version>0.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

package com.example.testusestarter2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestUseStarter2Application {

    public static void main(String[] args) {
        SpringApplication.run(TestUseStarter2Application.class, args);
    }

}
package com.example.testusestarter2;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.test.spring.boot.starter.ISplitService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
class TestUseStarter2ApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    private ISplitService splitServiceImpl;

    @Test
    public void splitTest() {
        log.info("split context: {}", JSON.toJSONString(splitServiceImpl.split("8888,6666666666")));
    }
}

 

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

原理,springboot注解@Configuration

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

见spring refresh和spring bean源码分析 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值