IDEA imports the third-party jar package and packages the jar package in Maven

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.

  1. 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.
    Insert image description here

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

  1. Enter and File-> Project Structure-> Libraries -> 点击 + 号-> 选择java select the selected jar package (add one by one if there are multiple)
    Insert image description here
  2. When selecting jar, specify the module (Modules). The module specified here is the artifactId in your own pom file.
    Insert image description here
  3. After the addition is completed, Project Structure-> Modulesthere 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.
    Insert image description here
    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.

Insert image description here

  1. In the pom file, add a reference to the third-party jar
    Insert image description here

       <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,artifactIdit 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

  2. Incorporate the Maven coordinates of the added third-party jar into the packaging plug-in
    Insert image description here

          <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 -jarthe 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

Guess you like

Origin blog.csdn.net/qq_43371556/article/details/131954225