maven 项目加载本地JAR

 将jar安装到本地的maven仓库

1.首先确定本地有maven环境。

2.安装本地jar

模板: 
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
示例:
mvn install:install-file -Dfile=F:jave-ffmpegjave-1.0.2.jar -DgroupId=ffmpegjave -DartifactId=java-ffmpegjave -Dversion=1.0.2 -Dpackaging=jar
<path-to-file>: 要安装的JAR的本地路径 
<group-id>:要安装的JAR的Group Id 
<artifact-id>: 要安装的JAR的 Artificial Id 
<version>: JAR 版本 
<packaging>: 打包类型,例如JAR

  注意:最好在pom.xml文件所在的目录运行上述命令,个人经验不在根目录运行有时会安装不成功

     如图出现SUCCESS就表示安装成功。

3.引用jar

找到安装的pom,打开复制引用

如:

        <dependency>
            <groupId>ffmpegjave</groupId>
            <artifactId>java-ffmpegjave</artifactId>
            <version>1.0.2</version> 
        </dependency>

这种方法弊端较大,程序的可维护性以及移植性较低。例如当你改变本地Maven仓库时需要重新安装。如果引用此JAR的项目是多人协调工作的项目,则每个人都要将其安装在自己的本地仓库。

解决办法

可以将此JAR文件放在工程的根目录下,让其随着项目走,然后在pom.xml文件中使用maven-install-plugin在Maven初始化阶段完成安装。

如图

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.watch.parent</groupId>
    <artifactId>children-watch-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <!-- 公共项目 -->
  <groupId>com.watch.commons</groupId>
  <artifactId>children-watch-commons</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <name>children-watch-commons</name>
  <url>http://maven.apache.org</url>
  <dependencies>
          <!-- amr录音转换为mp3 -->
        <dependency>
            <groupId>ffmpegjave</groupId>
            <artifactId>java-ffmpegjave</artifactId>
            <version>1.0.2</version> 
        </dependency>
        
  </dependencies>
  <build>
    <pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <groupId>ffmpegjave</groupId>
                        <artifactId>java-ffmpegjave</artifactId>
                        <version>1.0.2</version> 
                        <packaging>jar</packaging>
                        <file>${basedir}/lib/java-ffmpegjave-1.0.2.jar</file>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    <!--如果使用Eclipse报错的话,加入如下代码-->
        <!--This plugin's configuration is used to store Eclipse m2e settings only. 
            It has no influence on the Maven build itself. -->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>aspectj-maven-plugin</artifactId>
                                <versionRange>[1.0,)</versionRange>
                                <goals>
                                    <goal>test-compile</goal>
                                    <goal>compile</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>
                                    org.apache.maven.plugins
                                </groupId>
                                <artifactId>
                                    maven-install-plugin
                                </artifactId>
                                <versionRange>
                                    [2.5,)
                                </versionRange>
                                <goals>
                                    <goal>install-file</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnIncremental>false</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>
    </build>
</project>

 SpringBoot的配置

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                  <groupId>ffmpegjave</groupId>
          <artifactId>java-ffmpegjave</artifactId>
          <version>1.0.2</version> 
          <packaging>jar</packaging>
          <file>${basedir}/lib/java-ffmpegjave-1.0.2.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

${basedir}表示pom.xml文件所在的目录

然后打包测试看是否能引用到。如图

 

 

我这里是聚合工程,jar是在公共项目中引用的,我netty项目要用到只需要引用公共项目就可以了,jar也会一起引用过来的。

原文地址:https://www.cnblogs.com/iathanasy/p/8566908.html