如何将本地jar文件(还不是Maven存储库的一部分)直接添加到项目的库源中?
当前回答
将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文件目标
其他回答
我想分享一个代码,你可以上传一个装满jar的文件夹。当提供者没有公共存储库并且您需要手动添加大量库时,这非常有用。我决定构建一个.bat而不是直接调用maven,因为这可能是内存不足错误。它是为windows环境准备的,但很容易适应linux操作系统:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class CreateMavenRepoApp {
private static final String OCB_PLUGIN_FOLDER = "C://your_folder_with_jars";
public static void main(String[] args) throws IOException {
File directory = new File();
//get all the files from a directory
PrintWriter writer = new PrintWriter("update_repo_maven.bat", "UTF-8");
writer.println("rem "+ new Date());
File[] fList = directory.listFiles();
for (File file : fList){
if (file.isFile()){
String absolutePath = file.getAbsolutePath() ;
Manifest m = new JarFile(absolutePath).getManifest();
Attributes attributes = m.getMainAttributes();
String symbolicName = attributes.getValue("Bundle-SymbolicName");
if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) {
String[] parts =symbolicName.split("\\.");
String artifactId = parts[parts.length-1];
String groupId = symbolicName.substring(0,symbolicName.length()-artifactId.length()-1);
String version = attributes.getValue("Bundle-Version");
String mavenLine= "call mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile="+ absolutePath+" -DgroupId="+ groupId+" -DartifactId="+ artifactId+" -Dversion="+ version+" -Dpackaging=jar ";
writer.println(mavenLine);
}
}
}
writer.close();
}
}
从任何IDE运行此main之后,运行update_repo_maven.bat。
另一个有趣的例子是,当您希望在项目中拥有私有maven jar时。您可能希望保留Maven的功能来解决可传递的依赖关系。解决方案相当简单。
在项目中创建文件夹库在pom.xml文件中添加以下行<properties(财产)><local.repository.folder>${pom.basedir}/libs/</local.reppository.folder></财产><存储库><存储库><id>本地maven存储库</id><url>文件://${local.resource.folder}</url><发布><enabled>真</enabled></releases><快照><enabled>真</enabled></snapshot></repository></存储库>打开.m2/repository文件夹,将要导入的项目的目录结构复制到libs文件夹中。
例如,假设您要导入依赖项
<dependency>
<groupId>com.mycompany.myproject</groupId>
<artifactId>myproject</artifactId>
<version>1.2.3</version>
</dependency>
只需转到.m2/repository,您将看到以下文件夹
com/mycompany/myproject/1.2.3
复制libs文件夹中的所有内容(同样,包括.m2/repository下的文件夹),就完成了。
不是最初问题的答案,但它可能对某人有用
没有正确的方法可以使用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'
}
下载jar文件将jar文件复制到项目文件夹get inteliJ idea Maven命令区键入以下命令
mvn安装:安装文件-Dfile=YOUR_JAR_file_LOCATION*JARNAME.JAR-DgroupId=org.primefaces.themes-DastifactId=iMetro-Dversion=1.0.1-Dpackage=JAR*
例子:
mvn安装:安装文件-Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar-DgroupId=org.primefaces.themes-DartifactId=iMetro-Dversion=1.0.1-Dpackage=jar
根项目上进行构建
您可以在命令行中编写以下代码,或者如果您使用eclipse内置maven,右键单击项目->运行方式->运行配置…->在左侧面板中,右键单击Maven Build->new configuration->在基本目录中的Goals&中编写代码:${project_loc:NameOfYourProject}->运行
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
其中每一个都指:
<path to file>:要加载的文件的路径,例如->c:\kapcha-2.3jar
<group id>:文件应在例如com.google.code下注册的组
<artifact-id>:文件的工件名称,例如->kappcha
<version>:文件的版本,例如->2.3
<package>:文件的打包,例如->jar
2.安装后,只需在pom.xml中声明jar。
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>
推荐文章
- 在流中使用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