我的环境是win10系统,命令行参数可能有些不同。
-
先创建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;
}
}` -
调整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>
- 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>
- 测试
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类正常执行
`