如何将本地jar文件(还不是Maven存储库的一部分)直接添加到项目的库源中?
当前回答
不是最初问题的答案,但它可能对某人有用
没有正确的方法可以使用Maven从文件夹中添加多个jar库。如果只有几个依赖项,那么配置maven安装插件可能更容易,如上面的答案所述。
然而,对于我的特殊情况,我有一个lib文件夹,其中包含100多个专有jar文件,我必须以某种方式添加这些文件。对我来说,将Maven项目转换为Gradle要容易得多。
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
flatDir {
dirs 'libs' // local libs folder
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'io.grpc:grpc-netty-shaded:1.29.0'
implementation 'io.grpc:grpc-protobuf:1.29.0'
implementation 'io.grpc:grpc-stub:1.29.0' // dependecies from maven central
implementation name: 'akka-actor_2.12-2.6.1' // dependecies from lib folder
implementation name: 'akka-protobuf-v3_2.12-2.6.1'
implementation name: 'akka-stream_2.12-2.6.1'
}
其他回答
这是较新版本的简短语法:
mvn install:install-file -Dfile=<path-to-file>
当JAR由ApacheMaven构建时,这是最常见的情况。然后它将在META-INF目录的子文件夹中包含一个pom.xml,默认情况下将读取该文件。
资料来源:http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
将JAR安装到本地Maven存储库中(通常位于主文件夹中的.m2),如下所示:
mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true
其中每一个都指:
<path to file>:要加载的文件的路径,例如→ c: \patcha-2.3.jar
<group id>:文件应在其下注册的组,例如→ com.google代码
<artifact-id>:文件的工件名称,例如→ 卡普查
<version>:文件的版本,例如→ 2.3
<package>:文件的打包,例如。→ 罐子
参考
Maven常见问题解答:我有一个jar,我想把它放到我的本地存储库中。我如何复制它?Maven安装插件用法:Install:Install文件目标
真正快速而肮脏的方法是指向本地文件,请注意“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>
要安装第三方jar,请调用如下命令
mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path
由于某种原因,在我正在维护的web应用程序中,Alireza Fattahi的解决方案和JJ Roman的解决方案都不能正常工作。在这两种情况下,编译都是正常的(它看到了jar),但包装没有将jar包含在战争中。
我设法使其工作的唯一方法是将jar放在/src/main/webapp/WEB-INF/lib/上,然后将其与Fattahis或Roman的解决方案结合。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap