springboot打jar包并上传到Nexus仓库供其他项目使用

本文详细介绍了如何将Spring Boot项目打包为jar,剔除不需要的类和配置,并通过Maven发布到Nexus远程仓库。首先,调整Maven配置以排除特定文件,然后使用Maven的install和deploy命令将jar包发布到本地或Nexus仓库。最后,其他项目可以通过配置仓库地址和依赖来引入发布的jar包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在工作中会将搭建一个组件项目给到其他组或者项目使用。一般都是要maven项目打包成jar包并发布到Nexus的maven仓库供其他项目下载使用。


准备:

需要打成jar的项目;

Nexus搭建的maven远程仓库;

一个测试jar包的其他项目;


一、打成jar包

    <!-- 打成jar供其他项目使用
     1、不能使用默认的springboot打包方式 2、剔除启动类及测试类 3、剔除application配置文件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source> <!--指明源码用的Jdk版本-->
                    <target>1.8</target> <!--指明打包后的Jdk版本-->
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <!--打包时需要被排除的文件-->
                    <exclude>application.yml</exclude>
                </excludes>
            </resource>
        </resources>
    </build>

 

二、jar包发布(两种方式)

2.1 将jar发布在本地maven(install命令),其他本地项目直接pom引入即可。

2.2 jar包发布到Nexus搭建的maven中央仓库,其他项目配置远程仓库下载使用。

本地maven的setting.xml配置

	<server>
      <id>xx-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
	<server>
      <id>xx-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>	

项目pom配置

   <repositories>
        <repository>
            <id>xx-releases</id>
            <name>xx nexus</name>
            <url>http://192.168.0.166:8081/nexus/content/groups/xx-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <id>xx-releases</id>
            <name>releases</name>
            <url>http://ip:port/nexus/content/repositories/xx-releases/</url>
        </repository>
        <snapshotRepository>
            <id>xx-snapshots</id>
            <name>snapshots</name>
            <url>http://ip:port/nexus/content/repositories/xx-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
注意:
    1、setting.xml中的id要和pom中的id要对应。
    2、pom中的url分别是Nexus中的maven仓库对应的路径,根据自己实际情况配置。

执行maven deploy

 

三、其他项目引入jar包使用

    <!-- 远程仓库 -->
    <repositories>
        <repository>
            <id>xx-releases</id>
            <name>xx nexus</name>
            <url>http://ip:port/nexus/content/groups/xx-public/</url>
        </repository>
    </repositories>

    <!-- 引入依赖 对应的groupId和artifactId -->
    <dependencies>
        <dependency>
            <groupId>***</groupId>
            <artifactId>***</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

在 IntelliJ IDEA 中打 SpringBoot 项目通过阿里云镜像构建,可以按照以下步骤操作: 1. **配置Maven仓库**: 首先,确保你的 Maven 设置已含阿里云的中央仓库Maven Central Repository)。可以在 `settings.xml` 文件中添加阿里镜像地址,如: ```xml <mirrors> <mirror> <id>alimaven</id> <name>阿里巴巴开源镜像站</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ``` 2. **选择合适的插件**: 在你的 `pom.xml` 文件中,确保已经含了 Spring Boot 的依赖,可能需要 `maven-assembly-plugin` 或者 `maven-shade-plugin` 来打应用成jar或war文件。 示例: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 如果你需要自定义打 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.example.MainApplication</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 3. **运行打命令**: 在 IDEA 中,点击 "Run" 菜单,选择 "Run 'MainApplication'"(如果主类名不是 'MainApplication',请替换为实际名称),或者直接在终端中输入 `mvn clean package -DskipTests` 来打项目,如果启用了阿里镜像,Maven 应该能从那里下载依赖。 4. **部署到服务器**: 打完成后,你可以将生成的 `.jar` 或 `.war` 文件上传到阿里云服务器,比如 ECS、OSS,然后通过 SSH 登录服务器启动应用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值