如何直接在我的项目的库源添加本地jar文件(Maven仓库中没有的部分)?
安装JAR到你的本地仓库如下:
mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true
其中每个指:
<path-to-file>
:该文件路径来加载e.g→c:\kaptcha-2.3.jar
<group-id>
:该文件应在e.g→com.google.code
注册的组
<artifact-id>
:文件e.g→kaptcha
的工件名称
<version>
:文件的e.g→2.3
版本
<packaging>
:该文件的包装例如→jar
参考
install:install-file
goal真正快速和肮脏的方式是指向一个本地文件:
<dependency>
<groupId>sample</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>
然而,这只能住在你的机器(显然)上,共享它通常是有意义的使用恰当的M2存档(关系/ 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>
当然,你可以添加罐子到该文件夹。但也许它并不想达到什么样的...
如果你需要这些罐子进行编译,检查此相关的问题:Can I add jars to maven 2 build classpath without installing them?
此外,有人建议之前,不要使用系统范围。
命令行 :
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
另一个有趣的例子是,当你想在你的项目中私人Maven的罐子。你可能想保留的Maven的能力来解决传递依赖。该解决方案是相当容易的。
<properties><local.repository.folder>${pom.basedir}/libs/</local.repository.folder>
</properties>
<repositories>
<repository>
<id>local-maven-repository</id>
<url>file://${local.repository.folder}</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
例如。假设你想导入的依赖
<dependency>
<groupId>com.mycompany.myproject</groupId>
<artifactId>myproject</artifactId>
<version>1.2.3</version>
</dependency>
只要继续.m2目录/库,你会看到下面的文件夹
COM / myCompany的/ myproject的/ 1.2.3
在你的库文件夹复制的一切(同样,包括在.m2目录/存储库中的文件夹),和你做。
的首选方法是创建自己的远程存储库。
见here关于如何做到这一点的详细信息。看看在“上传到远程存储库”部分。
我想分享一个代码,您可以上传一个完整的罐子的文件夹中。当供应商没有一个公共仓库,你需要手动添加大量的库这是非常有用。我决定直接建立一个蝙蝠,而不是调用到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后,运行update_repo_maven.bat。
这是一个较新的版本很短的语法:
mvn install:install-file -Dfile=<path-to-file>
它的工作原理,当瓶子是与Apache Maven构建 - 最常见的情况。然后,它会包含在META-INF目录,该目录将被默认读取的子文件夹一个pom.xml。
来源:http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
我认为这个问题的更好的解决方案是使用maven-install-plugin自动安装文件,在安装时。这是我将它设置为我的项目。
首先,添加路径(您存储在本地.jar文件)作为属性。
<properties>
<local.sdk>/path/to/jar</local.sdk>
</properties>
然后,在plugins
添加一个插件安装编译时的罐子。
<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>
最后,在相关性,你可以添加的罐子
<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 install
或mvn test
当地罐将被自动添加。
请注意,是不是一定要使用本地回购是一个好主意。如果这个项目是与他人分享,然后每个人都会有问题和疑问,当它不工作,甚至在你的源代码控制系统的罐子将无法使用!
虽然共享回购是最好的答案,如果你不能因为某些原因,然后嵌入罐子做,这是比本地回购更好。仅本地回购的内容可能会导致很多问题,特别是随着时间的推移。
您可以直接添加本地的依赖关系(如build maven project with propriatery libraries included提到)是这样的:
<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>
更新
在新版本为过时,但仍在工作,但不会被删除(你只看到日志中的警告期间行家开始)此功能被标记。一个问题是在这个https://issues.apache.org/jira/browse/MNG-6523(您可以参加,并说明这项功能在某些情况下是有帮助的)行家组提出的。我希望这个功能依然存在!
如果你问我,只要功能不除,我用这个做依赖于我的项目只有一个顽皮的jar文件它不适合存储库。如果此功能被去除,那么,那里有许多很好的答案,我可以从后来选择的!
在你的本地库,你可以通过发出命令安装您的罐子
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
按照这个有用link做从mkyoung的网站一样。您还可以检查maven guide for the same
出于某种原因,在Web应用程序中我给维修,既不Alireza Fattahi's solution也不JJ Roman's solution工作正常。在这两种情况下,编译去好(它看到坛子),但包装上没有包括战争里面的罐子。
我设法使它工作的唯一方法是通过将上/src/main/webapp/WEB-INF/lib/
罐子,然后将其与任何Fattahis的或罗马的解决方案相结合。
这个回答为Eclipse用户:
如果您使用的是Eclipse,放在LIB /罐,右击罐子名,然后单击“添加到构建路径”。 Eclipse将创建一个“引用的库”,并把罐子给你
它解决了罐子的进口在节目马上给我
安装第三方JAR,请致电像下面的命令
mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path
我曾在我的pom.xml一组依赖的同样的错误原来依赖的版本在pom.xml未指定,并在父仓库被提及。出于某种原因,该版本的细节未在本回购同步。因此,我手动输入使用标记的版本和它的工作就像一个魅力。时间一点一点需要查找父版本,并指定在这里。但是,这是可以做到只针对那些显示的artifactId错误的罐子和它的作品。希望这可以帮助别人。
- MVN安装
你可以在命令行下面写代码,或者如果你使用Eclipse内置的maven右键单击项目 - >运行方式 - >运行配置... - >在左侧面板中右键单击Maven构建 - >新的配置 - >写码在目标与在基本目录:$ {project_loc:NameOfYourProject} - >运行
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
其中每个指:
<路径到文件>:所述文件路径来加载e.g - > C:\ kaptcha-2.3.jar
<组ID>:该文件应在e.g被注册的组 - > com.google.code
<神器-ID>:文件e.g神器的名字 - > kaptcha
<版本>:该文件的版本e.g - > 2.3
<包装>:文件的包装例如 - >罐子
2.After安装,只是声明罐子在pom.xml中。
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>
在Apache中的Maven 3.5.4,我不得不加双引号。如果没有双引号它不是为我工作。
例如:MVN安装:安装文件 “-Dfile =位置到jar文件”, “-DgroupId =组ID”, “-DartifactId =工件ID”, “-Dversion =版” “-Dpackaging =包类型”
我曾与ojdbc6同样的问题。我看到这个链接,但没有奏效。该命令是正确的,但我需要一个参数多,
这是链接:http://roufid.com/3-ways-to-add-local-jar-to-maven-project/
这里是例子:
install:install-file -Dfile=C:\driversDB\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar
首先,我想给信贷这个答案匿名堆栈溢出的用户 - 我敢肯定我以前在这里看到了类似的回答 - 但现在我找不到它。
对具有本地JAR文件作为依赖的最好的办法是建立一个本地的仓库。这样的存储库只不过是在它的pom文件正确的目录结构更多。
在我的例子:我有${master_project}
位置和subproject1我的主项目是${master_project}/${subproject1}
。
然后,我创建一个Maven仓库:${master_project}/local-maven-repo
。
在位于${master_project}/${subproject1}/pom.xml
subproject1的POM文件,资料库,需要指定其将采取的URL参数文件路径:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
</repositories>
依赖关系可以被指定为任何其他仓库。这使得独立的POM库。例如,一旦所需的JAR在Maven是可用的中央,你只需要从本地回购删除它,它会从默认的回购拉。
<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回购可以被提交到一个代码库,整个设置是系统无关。 (Working example in GitHub)。
我同意,犯有对源代码的JAR回购是不是一个好的做法,但在现实生活中,快速和肮脏的解决方案有时比一个完全成熟的Nexus回购举办一个JAR,你不能发布更好。
创建一个新的文件夹,让我们说local-maven-repo
在你的Maven项目的根目录。
只需添加您的<project>
的pom.xml
内的本地回购:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.basedir}/local-maven-repo</url>
</repository>
</repositories>
然后你要安装的每个外部JAR,请在您的项目和执行的根源:
mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]
我想这样的解决方案 - 在POM文件中使用maven-install-plugin
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>lib/yourJar.jar</file>
<groupId>com.somegroup.id</groupId>
<artifactId>artefact-id</artifactId>
<version>x.y.z</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
在这种情况下,您可以执行mvn initialize
和jar将被安装在本地Maven回购。现在,这个罐子是在这台机器上的任何一步行家可用(不要忘了包括这种依赖性,如聚甲醛与<dependency></dependency>
标签其他任何Maven的依赖)。也可以绑定JAR安装不initialize
一步,但你喜欢的任何其它步骤。
<dependency>
<groupId>group id name</groupId>
<artifactId>artifact name</artifactId>
<version>version number</version>
<scope>system</scope>
<systemPath>jar location</systemPath>
</dependency>
是的,你可以有,但它不是好主意。
相反,安装所有这些罐子到Maven回购
另请参见
一种方法是把它上传到你自己的Maven仓库管理(如的Nexus)。这是很好的做法,有自己的仓库经理反正。
我最近看到的另一个好方法是包括的Maven在构建生命周期中安装插件:您在POM宣布将文件安装到本地仓库。这是一个小而小的开销,并没有涉及手动工序。
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html
添加您自己的本地JAR在POM文件和使用,在Maven构建。
mvn install:install-file -Dfile=path-to-jar -DgroupId=owngroupid -DartifactId=ownartifactid -Dversion=ownversion -Dpackaging=jar
例如:
mvn install:install-file -Dfile=path-to-jar -DgroupId=com.decompiler -DartifactId=jd-core-java -Dversion=1.2 -Dpackaging=jar
然后将其添加到这样的POM: