如何将本地jar文件(还不是Maven存储库的一部分)直接添加到项目的库源中?
当前回答
命令行:
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
其他回答
步骤1:使用pom.xml中的目标安装文件配置maven安装插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>com.amazonservices.mws</groupId>
<artifactId>mws-client</artifactId>
<version>1.0</version>
<file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
确保根据您的实际文件路径编辑文件路径(建议将这些外部非maven jar放在某个文件夹中,比如说lib,并将该lib文件夹放在项目中,以便使用项目特定的相对路径,避免添加系统特定的绝对路径)。
如果您有多个外部jar,只需对同一maven安装插件中的其他jar重复<execution>即可。
步骤2:在pom.xml文件中如上所示配置maven安装插件后,您必须像往常一样使用pom.xml中的这些jar:
<dependency>
<groupId>com.amazonservices.mws</groupId>
<artifactId>mws-client</artifactId>
<version>1.0</version>
</dependency>
注意,maven安装插件只将外部jar复制到本地的.m2 maven存储库中。就是这样。它不会自动将这些jar作为maven依赖项包含到项目中。
这是一个小问题,但有时很容易忽略。
首先,我想把这个答案归功于一位匿名StackOverflow用户——我很肯定我以前在这里也看到过类似的答案——但现在我找不到了。
将本地JAR文件作为依赖项的最佳选择是创建本地Maven存储库。这样的存储库只不过是一个适当的目录结构,其中包含pom文件。
例如:我的主项目位于${master_project}位置,子项目1位于${master _project}/${subject1}位置。
然后,我在以下位置创建Maven存储库:${master_project}/本地maven repo。
在子项目1中位于${master_project}/${subject1}/pom.xml的pom文件中,需要指定存储库,该存储库将文件路径作为URL参数:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
</repositories>
可以为任何其他存储库指定依赖项。这使pom存储库独立。例如,一旦所需的JAR在Maven central中可用,您只需从本地回购中删除它,它将从默认回购中取出。
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.servicebinder</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
最后但并非最不重要的一件事是使用-DlocalRepositoryPath开关将JAR文件添加到本地存储库,如下所示:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=/some/path/on/my/local/filesystem/felix/servicebinder/target/org.apache.felix.servicebinder-0.9.0-SNAPSHOT.jar \
-DgroupId=org.apache.felix -DartifactId=org.apache.felix.servicebinder \
-Dversion=0.9.0-SNAPSHOT -Dpackaging=jar \
-DlocalRepositoryPath=${master_project}/local-maven-repo
一旦安装了JAR文件,就可以将Maven repo提交到代码存储库,并且整个设置与系统无关。(GitHub中的工作示例)。
我同意将JAR提交给源代码回购并不是一个好的做法,但在现实生活中,快速而肮脏的解决方案有时比一个完整的Nexus回购托管一个无法发布的JAR要好。
您可以直接添加本地依赖项(如build maven项目中所述,包含专有库),如下所示:
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>
使现代化
在新版本中,此功能被标记为已弃用,但仍在工作,尚未删除(您只需在maven启动期间的日志中看到警告)。maven集团对此提出了一个问题https://issues.apache.org/jira/browse/MNG-6523(您可以参与并描述此功能在某些情况下有帮助的原因)。我希望这一功能仍然存在!
若你们问我,只要这个特性没有被删除,我就用它来依赖我项目中的一个不适合存储库的jar文件。如果删除此功能,那么,这里有很多好的答案,我可以稍后选择!
真正快速而肮脏的方法是指向本地文件,请注意“system”现在已被弃用:
<dependency>
<groupId>com.sample</groupId>
<artifactId>samplifact</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>
然而,这只会存在于您的机器上(显然),因为共享通常使用适当的m2存档(nexus/artifactory)是有意义的,或者如果您没有这些存档或不想设置本地maven结构化存档并在pom中配置“存储库”:
本地:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://C:/DEV//mymvnrepo</url>
</repository>
</repositories>
远程:
<repositories>
<repository>
<id>my-remote-repo</id>
<url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
</repository>
</repositories>
对于此解决方案,还可以使用basedir变量创建相对路径:
<url>file:${basedir}</url>
我认为这个问题的更好解决方案是使用maven安装插件在安装时自动安装文件。这是我为项目设置的方式。
首先,将路径(存储local.jar的位置)添加为属性。
<properties>
<local.sdk>/path/to/jar</local.sdk>
</properties>
然后,在插件下添加一个插件,以便在编译时安装jar。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>1</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${local.sdk}/lib/impl/appengine-api.jar</file>
</configuration>
</execution>
<execution>
<id>appengine-api-stubs</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${local.sdk}/lib/impl/appengine-api-stubs.jar</file>
</configuration>
</execution>
</executions>
</plugin>
最后,在依赖项中,可以添加jar
<dependency>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
通过这样设置项目,即使您将其带到另一台计算机,项目也将继续构建(假定它在local.sdk属性指定的路径中包含所有jar文件)。
对于groupId,请使用唯一的名称,以确保没有冲突。
现在,当您进行mvn安装或mvn测试时,将自动添加本地jar。
推荐文章
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?