Add external library .jar to Spring boot .jar internal /lib

前端 未结 7 1317
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 23:04

I have an external .jar that cannot be imported from public repositories using pom.xml, it\'s sqljdbc41.jar.

I can run the project locally from my IDE,

相关标签:
7条回答
  • 2020-11-29 23:17

    It works for me:

    project {root folder}/libs/ojdbc-11.2.0.3.jar
    

    pom.xml:

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc</artifactId>
        <version>11.2.0.3</version>
        <scope>system</scope>
        <systemPath>${basedir}/libs/ojdbc-11.2.0.3.jar</systemPath>
    </dependency>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <includeSystemScope>true</includeSystemScope>
        </configuration>
    </plugin>
    
    0 讨论(0)
  • 2020-11-29 23:29

    In Spring Boot: I also faced similar issue and below code helped me.

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.7.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    0 讨论(0)
  • 2020-11-29 23:30

    When Spring-Boot projects are used with maven or gradle plugins they packaged the applicaiton by default as executable jars. These executable jars cannot be used as dependency in any another Spring-Boot project because the executable jar add classes in BOOT-INF/classes folder. This means that they cannot be found when the executable jar is used as a dependency because the dependency jar will also have the same class path structure as shown below.

    If we want to use project-A as a maven dependency in project-B then we must have two artifacts. To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency. To configure a classifier of exec in Maven, you can use the following configuration:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    So the MAJIC WORD here is <classifier>exec</classifier> this will create a jar structure as below and then it could easily be conusmed by spring-boot project as maven dependency jar on class path.

    The above plugin need to be add in project-A pom that is going to be used as dependency in project-B. Same is explained in spring documentation section 16.5. as well.

    0 讨论(0)
  • 2020-11-29 23:32

    In my case, the fault was providing a version number without "dot" in tag:

            <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <scope>system</scope>
            <version>1</version>
            <systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
        </dependency>
    

    This one works:

            <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <scope>system</scope>
            <version>1.8</version>
            <systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
        </dependency>
    
    0 讨论(0)
  • 2020-11-29 23:38

    you can set 'includeSystemScope' to true.

    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <includeSystemScope>true</includeSystemScope>
      </configuration>
    </plugin>
    
    0 讨论(0)
  • 2020-11-29 23:40

    You could install the sqljdbc41.jar in your local repository :

    mvn install:install-file -Dfile=path/to/sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc41 -Dversion=4.1 -Dpackaging=jar
    

    And then declare the dependency as a standard dependency :

    <dependency>
       <groupId>com.microsoft.sqlserver</groupId>
       <artifactId>sqljdbc41</artifactId>
       <version>4.1</version>
    </dependency>
    

    If you use a remote artifact repository (nexus, archiva...) you also need to deploy the artifact on this repository. You can find more here : https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html

    0 讨论(0)
提交回复
热议问题