mvn打包并且另外一个项目引用该包

我的环境是win10系统,命令行参数可能有些不同。

  1. 先创建springboot项目TestA,新建一个Package,命名为service,然后在下面创建一个Student的类,
    `public class Student {

    public int sum(int a, int b) {
    return a + b;
    }

    public boolean isEmptyOrNull(String str) {
    if (str == null || str.length() < 1) {
    return true;
    }
    return false;
    }
    }`

  2. 调整pom.xml

<build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                        <encoding>UTF-8</encoding>
    
                        <excludes>
                            <exclude>
                                org/huha/testa/TestAApplication.java
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <!--<plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <configuration>
                        <mainClass>org.huha.testa.TestAApplication</mainClass>
                        <skip>true</skip>
                    </configuration>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>-->
            </plugins>
        </build>

注释了spring-boot-maven-plugin插件,
4. 打包:命令行切换到TestA pom.xml文件所在目录下,执行mvn install命令

[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ TestA ---
[INFO] Building jar: D:\workspace_java\git\TestA\target\TestA-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ TestA ---
[INFO] Installing D:\workspace_java\git\TestA\target\TestA-0.0.1-SNAPSHOT.jar to C:\Users\Administrator\.m2\repository\org\huha\TestA\0.0.1-SNAPSHOT\TestA-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\workspace_java\git\TestA\pom.xml to C:\Users\Administrator\.m2\repository\org\huha\TestA\0.0.1-SNAPSHOT\TestA-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.364 s
[INFO] Finished at: 2024-03-21T00:26:12+08:00
[INFO] ------------------------------------------------------------------------

现在开始引用:
创建springboot 项目TestB
1.引入TestA包, 在项目pom.xml目录下执行
mvn install:install-file -Dfile=“D:\workspace_java\git\TestA\target\TestA-0.0.1-SNAPSHOT.jar” -DgroupId=“org.huha” -DartifactId=TestA -Dversion=“0.0.1-SNAPSHOT” -Dpackaging=jar -s “D:\program\apache-maven-3.8.7\conf\settings.xml”

在win10下 上面 -Dfile的参数好像得用双引号包起来,不然报错。
若安装得位置到C:\Users\Administrator.m2\repository\ 和你IDEA设置得不一样,那么应该指定maven配置文件,并在settings.xml指定D:\program\apache-maven-3.8.7\repository

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
   -->
  <localRepository>D:\program\apache-maven-3.8.7\repository</localRepository>
  

  1. pom文件加入依赖:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.huha</groupId>
            <artifactId>TestA</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
  1. 测试
public static void main(String[] args) {

        Student stu = new Student();
        boolean b1 = stu.isEmptyOrNull("hello");
        boolean b2 = stu.isEmptyOrNull("");
        int sum = stu.sum(10, 30);

        System.out.println("b1" + b1 + "b2" + b2 + "sum:" + sum);
        SpringApplication.run(TestBApplication.class, args);
    }
    代码执行结果:
    b1falseb2truesum:40

Student类正常执行

`

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值