美文网首页数客联盟
在maven项目中使用本地jar包的4种方法

在maven项目中使用本地jar包的4种方法

作者: tinyMonkey | 来源:发表于2018-06-06 21:34 被阅读109次

本文讲述在maven项目中使用本地jar包的4种方法:

手动添加jar到本地maven库

第一种方法是使用mvn命令将jar包添加到本地方法库,方法如下:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

参数如下:

  • <path-to-file>: Path to the JAR to install
  • <group-id>: Group id of the JAR to install
  • <artifact-id>: Artifact id of the JAR to install
  • <version>: Version of the JAR
    例子如下:
mvn install:install-file -DgroupId=com.example  -DartifactId=auth -Dversion=1.0.0 -Dpackaging=jar -Dfile=Athena-1.0.0-jar-with-dependencies.jar

使用 maven-install-plugin

这个方法是在pom.xml中使用maven-install-plugin,在“initialize”阶段安装jar包,可以把jar包放在一个指定的路径下,例子如下:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <packaging>jar</packaging>
                            <artifactId>Athena</artifactId>
                            <groupId>com.example.zodiac</groupId>
                            <version>1.0.0</version>
                            <file>${basedir}/lib/Athena-1.0.0-jar-with-dependencies.jar</file>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>

经过各种尝试,发现一种情况:在一个pom.xml文件中使用maven-install-plugin的同时定义对该jar包的依赖,这种方式编译无法通过。
变通方式是在root module的pom.xml中使用maven-install-plugin,然后在子module中使用dependency,例子如下:

        <dependency>
            <groupId>com.example.zodiac</groupId>
            <artifactId>Athena</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
        </dependency>

在dependency中使用system scope

这种方法比较简单,直接上例子:

      <dependency>
            <groupId>com.example.zodiac</groupId>
            <artifactId>Athena</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
           <scope>system</scope>
          <systemPath>${basedir}/lib/Athena-1.0.0-jar-with-dependencies.jar</systemPath>
        </dependency>

创建一个本地maven库

这种方法简单粗暴,在pom.xml文件中定义一个新的maven库,然后根据maven的存储方式将jar包放在指定的路径下:

<repositories>
        <repository>
            <id>local</id>
            <name>local</name>
            <url>file:///${pom.basedir}/lib/</url>
            <layout>default</layout>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>

然后将jar包放在路径:${pom.basedir}/lib/com/example/zodiac/Athena/1.0.0/Athena-1.0.0-.jar
ps:由于Athena-1.0.0-jar-with-dependencies.jar命名不符合maven的规范,所以需要将Athena-1.0.0-jar-with-dependencies.jar重命名为Athena-1.0.0-.jar或者修改依赖jar的version为:1.0.0-jar-with-dependencies

相关文章

网友评论

  • 7682fc36f2e6:可以使用maven的assembly插件把第三方jar包打到lib库下面,然后把本地jar包也放进lib下,执行主类Java cp启动时,把lib作为classpath就行了
    tinyMonkey:@WestC 😓
    tinyMonkey:@夏露_5411 这种办法如果引用了jar 包,那么编译可以通过?
    WestC:@夏露_5411 露总分分钟给你指点

本文标题:在maven项目中使用本地jar包的4种方法

本文链接:https://www.haomeiwen.com/subject/nwqysftx.html