IDEA imports the jar package and packages the jar package in Maven
background
For some reasons, the project needs to introduce the code in the demo, but the demo uses java and does not use any framework, and also references third-party jars. Therefore,
when integrating the demo in your own project (SpringBoot+Maven), the entire integration process is quite complicated. For the sake of trouble,
I will record this step in case it is needed in the future.
step
If it is a project based on Maven, you can directly skip the IDEA import jar package part and directly configure the subsequent parts.
- Create a jar storage directory in the project.
My method is to create a lib directory in the project root directory to store the jar.
IDEA import jar package
At the beginning, the reference to the third-party jar (the import part above the class) reported red, so it needed to be configured on the idea to enable it to run the demo normally, so I
found a similar solution online. The steps are as follows
- Enter and
File-> Project Structure-> Libraries -> 点击 + 号-> 选择java
select the selected jar package (add one by one if there are multiple)
- When selecting jar, specify the module (Modules). The module specified here is the artifactId in your own pom file.
- After the addition is completed,
Project Structure-> Modules
there will be the jar we added below
. If it is still not there, we+
can click to manually add the library configured in the previous step.
The main thing to note is that+
after clicking, you choose to add the library Libraries, not the jar or directory.
After confirming the configuration , click OK, and you will find that the demo can run normally.
Packaging of third-party jar packages in Maven
Although it can run, I encountered a problem in Maven packaging.
Although it was added to the module in the idea, the third-party jar package cannot be referenced when packaging. As shown in the figure below,
in Maven, the third-party referenced The jar has not been imported, so we need to perform secondary configuration.
-
In the pom file, add a reference to the third-party jar
<dependency> <groupId>com.time.pause</groupId> <artifactId>okhttp</artifactId> <!--依赖范围--> <scope>system</scope> <version>3.12.0</version> <!--依赖所在位置--> <systemPath>${project.basedir}/lib/okhttp-3.12.0.jar</systemPath> </dependency>
It should be noted that:
groupId,artifactId
it does not need to be completely consistent with the jar package, we only need to customize it. We will add the version according to the actual situation -
Incorporate the Maven coordinates of the added third-party jar into the packaging plug-in
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--让maven编译的时候能将第三方包编入--> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin>
It should be noted that if there is no configuration in step 2, although no error will be reported when packaging,
java -jar
the package will still not be found when running the jar locally ,
because the third-party jar is not actually entered by the packaging plug-in.
Reference blog
https://blog.csdn.net/qq_43599841/article/details/127368168
https://blog.csdn.net/weixin_43888891/article/details/130611728