• Maven pom.xml 添加本地jar包依赖以及打包方法


    Maven项目打包时,如果遇到需要添加本地jar包依赖的时候,可以选择两种方法:

    1. 安装到本地仓库

    第一种方法比较常规,适用于需要添加的jar包也是由maven项目导出,含有pom文件的时候。只需要将jar包安装到本地maven仓库下,然后添加依赖即可。

    (1)安装到本地仓库,执行以下命令(其中的-Dfile/-DgroupId/-DartifactId/-Dversion项根据pom文件内容填写):

    mvn install:install-file -Dfile=xxxxx.jar -DgroupId=xxx.xxx.xxx -DartifactId=xxxxx -Dversion=1.0.0 -Dpackaging=jar

    (2)安装之后可以在本地仓库中找到对应的jar包。然后将对应的依赖信息插入到工程的pom文件即可:

    <dependency>
        <groupId>xxx.xxx.xxx</groupId>
        <artifactId>xxxxx</artifactId>
        <version>1.0.0</version>
    </dependency>

    2. dependency中指定scope="system"和本地jar包路径

    这种方法适用于其他方式导出的jar包,jar包中不含有pom信息,从而无法安装进本地仓库的情况。做法是:先配置本地jar包依赖,然后在build时将设置将jar包导出,同时配置manifest。

    (1)配置本地jar包依赖(systemPath指向本地jar包路径):

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/xxx.jar</systemPath>
    </dependency>

    (2)在<build>的spring-boot-maven-plugin中设置将本地jar包导出到项目最终的依赖库中:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <includeSystemScope>true</includeSystemScope>
        </configuration>
    </plugin>

    (3)如果项目使用maven-jar-plugin插件打包的话,还需要在manifectEntries中添加对应的jar包信息;否则虽然jar包导出了,但是项目生成的MANIFEST.MF文件中没有对应的依赖信息,也会导致运行时找不到对应的class。

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>XXXX</mainClass>
                </manifest>
                <manifestEntries>
                    <Class-Path>./ lib/xxxxx.jar</Class-Path>
                </manifestEntries>
            </archive>
            <outputDirectory>
                ${project.build.directory}/XXXXX
            </outputDirectory>
        </configuration>
    </plugin>

    (4)最后附上一个项目完整的<build>配置(该配置可以将最终生成的jar包和依赖库、配置文件分开)。

    <build>
        <finalName>XXXXX</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}/XXXXX</targetPath>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
    
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>
    
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}XXXXX/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>xxx.xxx.XXXXX</mainClass>
                            </manifest>
                            <manifestEntries>
                                <Class-Path>./ lib/xxxxx.jar</Class-Path>
                            </manifestEntries>
                        </archive>
                        <outputDirectory>
                            ${project.build.directory}/XXXXX
                        </outputDirectory>
                    </configuration>
                </plugin>
        </plugins>
    </build>
  • 相关阅读:
    SQLserver 逗号隔开字段分开
    css实现鼠标移动到div,改变另一个div 样式
    selenium账号密码模拟登陆豆瓣
    HTML+CSS基础
    Nginx 报错 413 Request Entity Too Large 的解决方法
    Nginx 报错 504 Gateway Time-out 的解决方法
    Object定时器
    Object标签遮挡
    winRAR打包EXE
    jqGrid API
  • 原文地址:https://www.cnblogs.com/strugglion/p/12513956.html
Copyright © 2020-2023  润新知