简体   繁体   English

如何将本地jar文件添加到Maven项目中?

[英]How to add local jar files to a Maven project?

How do I add local jar files (not yet part of the Maven repository) directly in my project's library sources?如何直接在项目的库源中添加本地 jar 文件(还不是 Maven 存储库的一部分)?

You can add local dependencies directly (as mentioned in build maven project with propriatery libraries included ) like this:您可以直接添加本地依赖项(如build maven project with propriatery libraries include中所述),如下所示:

<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>

Update更新

In new releases this feature is marked as deprecated but still working and not removed yet ( You just see warning in the log during maven start).在新版本中,此功能被标记为已弃用但仍在工作且尚未删除(您只会在 maven 启动期间在日志中看到警告)。 An issue is raised at maven group about this https://issues.apache.org/jira/browse/MNG-6523 ( You can participate and describe why this feature is helpful in some cases). maven 组提出了一个关于此https://issues.apache.org/jira/browse/MNG-6523的问题(您可以参与并描述为什么此功能在某些情况下有用)。 I hope this feature remains there!我希望这个功能仍然存在!

If you are asking me, as long as the feature is not removed, I use this to make dependency to only one naughty jar file in my project which is not fit in repository.如果您问我,只要不删除该功能,我就会使用它来仅依赖我的项目中一个不适合存储库的顽皮 jar 文件 If this feature is removed, well, there are lots of good answers here which I can chose from later!如果去掉这个功能,好吧,这里有很多好的答案,我以后可以从中选择!

Install the JAR into your local Maven repository (typically .m2 in your home folder) as follows:将 JAR 安装到本地 Maven 存储库(通常是主文件夹中的.m2 ),如下所示:

mvn install:install-file \
   -Dfile=<path-to-file> \
   -DgroupId=<group-id> \
   -DartifactId=<artifact-id> \
   -Dversion=<version> \
   -Dpackaging=<packaging> \
   -DgeneratePom=true

Where each refers to:其中每个指的是:

<path-to-file> : the path to the file to load eg → c:\kaptcha-2.3.jar <path-to-file> :要加载的文件的路径,例如 → c:\kaptcha-2.3.jar

<group-id> : the group that the file should be registered under eg → com.google.code <group-id> : 文件应该注册的组,例如 → com.google.code

<artifact-id> : the artifact name for the file eg → kaptcha <artifact-id> :文件的工件名称,例如 → kaptcha

<version> : the version of the file eg → 2.3 <version> :文件的版本,例如 → 2.3

<packaging> : the packaging of the file eg → jar <packaging> : 文件的包装 eg → jar

Reference参考

Firstly, I would like to give credit for this answer to an anonymous Stack Overflow user - I am pretty sure I've seen a similar answer here before - but now I cannot find it.首先,我想将这个答案归功于一个匿名的 Stack Overflow 用户——我很确定我以前在这里看到过类似的答案——但现在我找不到了。

The best option for having local JAR files as a dependency is to create a local Maven repository.将本地 JAR 文件作为依赖项的最佳选择是创建本地 Maven 存储库。 Such a repository is nothing more than a proper directory structure with pom files in it.这样的存储库只不过是一个适当的目录结构,其中包含 pom 文件。

For my example: I have my master project on ${master_project} location and subproject1 is on ${master_project}/${subproject1} .对于我的示例:我的主项目位于${master_project}位置,而 subproject1 位于${master_project}/${subproject1}

Then I create a Maven repository in: ${master_project}/local-maven-repo .然后我在${master_project}/local-maven-repo中创建一个 Maven 存储库。

In the pom file in subproject1 located at ${master_project}/${subproject1}/pom.xml , the repository needs to be specified which would take file path as a URL parameter:在位于${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>

The dependency can be specified as for any other repository.可以为任何其他存储库指定依赖项。 This makes your pom repository independent.这使您的 pom 存储库独立。 For instance, once the desired JAR is available in Maven central, you just need to delete it from your local repo and it will be pulled from the default repo.例如,一旦所需的 JAR 在 Maven 中心可用,您只需从本地存储库中删除它,它将从默认存储库中提取。

    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.servicebinder</artifactId>
        <version>0.9.0-SNAPSHOT</version>
    </dependency>

The last but not least thing to do is to add the JAR file to local repository using -DlocalRepositoryPath switch like so:最后但并非最不重要的事情是使用 -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

Once the JAR file is installed, your Maven repo can be committed to a code repository, and the whole set-up is system independent.安装 JAR 文件后,您的 Maven 存储库可以提交到代码存储库,并且整个设置与系统无关。 ( Working example in GitHub ). GitHub 中的工作示例)。

I agree that having JARs committed to source code repo is not a good practice, but in real life, quick and dirty solutions are sometimes better than a full blown Nexus repo to host one JAR that you cannot publish.我同意将 JAR 提交到源代码 repo 不是一个好习惯,但在现实生活中,快速而肮脏的解决方案有时比一个完整的 Nexus repo 更好地托管一个您无法发布的 JAR。

Create a new folder, let's say local-maven-repo at the root of your Maven project.创建一个新文件夹,比如在 Maven 项目的根目录下的local-maven-repo

Just add a local repo inside your <project> of your pom.xml :只需在pom.xml<project>中添加一个本地仓库:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file:///${project.basedir}/local-maven-repo</url>
    </repository>
</repositories>

Then for each external jar you want to install, go at the root of your project and execute:然后对于您要安装的每个外部 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]

I'd like such solution - use maven-install-plugin in pom file:我想要这样的解决方案 - 在 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>

In this case you can perform mvn initialize and jar will be installed in local maven repo.在这种情况下,您可以执行mvn initialize ,并且 jar 将安装在本地 maven repo 中。 Now this jar is available during any maven step on this machine (do not forget to include this dependency as any other maven dependency in pom with <dependency></dependency> tag).现在这个 jar 在这台机器上的任何 maven 步骤中都是可用的(不要忘记在 pom 中包含这个依赖项作为带有<dependency></dependency>标记的任何其他 maven 依赖项)。 It is also possible to bind jar install not to initialize step, but any other step you like.也可以绑定 jar install 而不是initialize步骤,而是您喜欢的任何其他步骤。

The really quick and dirty way is to point to a local file, please note "system" is deprecated by now:真正快速而肮脏的方法是指向本地文件,请注意“系统”现在已弃用:

<dependency>
    <groupId>com.sample</groupId>  
    <artifactId>samplifact</artifactId>  
    <version>1.0</version> 
    <scope>system</scope>
    <systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>

However this will only live on your machine (obviously), for sharing it usually makes sense to use a proper m2 archive (nexus/artifactory) or if you do not have any of these or don't want to set one up a local maven structured archive and configure a "repository" in your pom:但是,这只会存在于您的机器上(显然),对于共享,使用适当的 m2 存档(nexus/artifactory)通常是有意义的,或者如果您没有这些存档或不想设置一个本地 maven结构化存档并在您的 pom 中配置“存储库”:

local:当地的:

<repositories>
    <repository>
        <id>my-local-repo</id>
        <url>file://C:/DEV//mymvnrepo</url>
    </repository>
</repositories>

remote:偏僻的:

<repositories>
    <repository>
        <id>my-remote-repo</id>
        <url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
    </repository>
</repositories>

for this solution, a relative path is also possible using the basedir variable:对于这个解决方案,相对路径也可以使用 basedir 变量:

<url>file:${basedir}</url>
<dependency>
    <groupId>group id name</groupId>
    <artifactId>artifact name</artifactId>
    <version>version number</version>
    <scope>system</scope>
    <systemPath>jar location</systemPath>
</dependency>

Important part in dependency is: ${pom.basedir} (instead of just ${basedir})依赖的重要部分是: ${pom.basedir} (而不仅仅是 ${basedir})

<dependency>
    <groupId>org.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${pom.basedir}/src/lib/example.jar</systemPath>
</dependency>

Add your own local JAR in POM file and use that in maven build.在 POM 文件中添加您自己的本地 JAR 并在 maven 构建中使用它。

mvn install:install-file -Dfile=path-to-jar -DgroupId=owngroupid -DartifactId=ownartifactid -Dversion=ownversion -Dpackaging=jar

For example:例如:

mvn install:install-file -Dfile=path-to-jar -DgroupId=com.decompiler -DartifactId=jd-core-java -Dversion=1.2 -Dpackaging=jar

Then add it to the POM like this:然后像这样将它添加到 POM 中:

在此处输入图像描述

如何在项目的库源代码中直接添加本地jar文件(尚未成为Maven存储库的一部分)?

Step 1: Configure the maven-install-plugin with the goal install-file in your pom.xml第 1 步:使用pom.xml中的目标install-file配置maven-install-plugin

<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>

Make sure to edit the file path based on your actual file path (recommended is to place these external non-maven jars inside some folder, let's say lib , and place this lib folder inside your project so as to use project-specific relative path and avoid adding system specific absolute path.确保根据您的实际文件路径编辑file路径(建议将这些外部非 maven jar 放在某个文件夹中,比如说lib ,并将这个lib文件夹放在您的项目中,以便使用项目特定的相对路径和避免添加系统特定的绝对路径。

If you have multiple external jars, just repeat the <execution> for other jars within the same maven-install-plugin .如果您有多个外部 jar,只需对同一maven-install-plugin中的其他 jar 重复<execution>即可。

Step 2: Once you have configured the maven-install-plugin as shown above in your pom.xml file, you have to use these jars in your pom.xml as usual:第 2 步:一旦您在pom.xml文件中配置了如上所示的maven-install-plugin ,您必须像往常一样在pom.xml中使用这些 jar:

    <dependency>
        <groupId>com.amazonservices.mws</groupId>
        <artifactId>mws-client</artifactId>
        <version>1.0</version>
    </dependency>

Note that the maven-install-plugin only copies your external jars to your local .m2 maven repository.请注意, maven-install-plugin仅将您的外部 jar 复制到本地.m2 maven 存储库。 That's it.而已。 It doesn't automatically include these jars as maven dependencies to your project.它不会自动将这些 jars 作为 maven 依赖项包含到您的项目中。

It's a minor point, but sometimes easy to miss.这是一个小问题,但有时很容易错过。

One way is to upload it to your own Maven repository manager (such as Nexus).一种方法是将其上传到您自己的 Maven 存储库管理器(例如 Nexus)。 It's good practice to have an own repository manager anyway.无论如何,拥有自己的存储库管理器是一种很好的做法。

Another nice way I've recently seen is to include the Maven Install Plugin in your build lifecycle: You declare in the POM to install the files to the local repository.我最近看到的另一种好方法是在构建生命周期中包含 Maven 安装插件:您在 POM 中声明将文件安装到本地存储库。 It's a little but small overhead and no manual step involved.这是一个很小但很小的开销,并且不涉及手动步骤。

http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html

Of course you can add jars to that folder.当然,您可以将 jars 添加到该文件夹​​中。 But maybe it does not what you want to achieve...但也许它不是你想要实现的......

If you need these jars for compilation, check this related question: Can I add jars to maven 2 build classpath without installing them?如果您需要这些 jars 进行编译,请检查以下相关问题: 我可以将 jars 添加到 maven 2 build classpath 而不安装它们吗?

Also, before anyone suggests it, do NOT use the system scope.此外,在任何人建议之前,不要使用系统范围。

Another interesting case is when you want to have in your project private maven jars.另一个有趣的案例是当你想在你的项目中拥有私有的 maven jars。 You may want to keep the capabilities of Maven to resolve transitive dependencies.您可能希望保留 Maven 的功能来解决传递依赖关系。 The solution is fairly easy.解决方案相当简单。

  1. Create a folder libs in your project在你的项目中创建一个文件夹libs
  2. Add the following lines in your pom.xml file在 pom.xml 文件中添加以下行

    <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>
  3. Open the .m2/repository folder and copy the directory structure of the project you want to import into the libs folder.打开.m2/repository文件夹,将要导入的项目的目录结构复制到libs文件夹中。

Eg suppose you want to import the dependency例如,假设您要导入依赖项

<dependency>
    <groupId>com.mycompany.myproject</groupId>
    <artifactId>myproject</artifactId>
    <version>1.2.3</version>
</dependency>

Just go on .m2/repository and you will see the following folder只需继续.m2/repository ,您将看到以下文件夹

com/mycompany/myproject/1.2.3 com/mycompany/myproject/1.2.3

Copy everything in your libs folder (again, including the folders under .m2/repository ) and you are done.复制您的 libs 文件夹中的所有内容(同样,包括.m2/repository下的文件夹),您就完成了。

command line :命令行 :

mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar

Add local jar libraries, their sources and javadoc to a Maven project将本地jar库、它们的sourcesjavadoc添加到 Maven 项目

If you have pre-compiled jar files with libraries, their sources and javadoc , then you can install them to your local Maven repository like this:如果你已经预编译了带有库、它们的sourcesjavadocjar文件,那么你可以像这样将它们install到本地 Maven 存储库中:

mvn install:install-file
    -Dfile=awesomeapp-1.0.1.jar \
    -DpomFile=awesomeapp-1.0.1.pom \
    -Dsources=awesomeapp-1.0.1-sources.jar \
    -Djavadoc=awesomeapp-1.0.1-javadoc.jar \
    -DgroupId=com.example \
    -DartifactId=awesomeapp \
    -Dversion=1.0.1 \
    -Dpackaging=jar

Then in your project you can use this libraries:然后在您的项目中,您可以使用以下库:

<!-- com.example -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>awesomeapp</artifactId>
    <version>1.0.1</version>
</dependency>

See: maven-install-plugin usage.请参阅: maven-install-plugin用法。


Or you can build these libraries yourself with their sources and javadoc usingmaven-source-plugin and maven-javadoc-plugin , and then install them.或者您可以使用maven-source-pluginmaven-javadoc-plugin使用它们的sourcesjavadoc自己build这些库,然后install它们。

Example project: library示例项目:图书馆

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <url>https://example.com/awesomeapp</url>

    <groupId>com.example</groupId>
    <artifactId>awesomeapp</artifactId>
    <name>awesomeapp</name>
    <version>1.0.1</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>12</java.version>
    </properties>

    <build>
        <finalName>awesomeapp</finalName>
        <defaultGoal>install</defaultGoal>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals><goal>jar</goal></goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals><goal>jar</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Execute maven install goal:执行 maven install目标:

mvn install

Check your local Maven repository:检查您的本地 Maven 存储库:

~/.m2/repository/com/example/awesomeapp/1.0.1/
 ├─ _remote.repositories
 ├─ awesomeapp-1.0.1.jar
 ├─ awesomeapp-1.0.1.pom
 ├─ awesomeapp-1.0.1-javadoc.jar
 └─ awesomeapp-1.0.1-sources.jar

Then you can use this library:然后你可以使用这个库:

<!-- com.example -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>awesomeapp</artifactId>
    <version>1.0.1</version>
</dependency>

I think a better solution for this problem is to use maven-install-plugin to automatically install the files at install time.我认为这个问题更好的解决方案是使用maven-install-plugin在安装时自动安装文件。 This is how I set it up for my project.这就是我为我的项目设置它的方式。

First, add the path (where you store the local .jars) as a property.首先,将路径(存储本地 .jars 的位置)添加为属性。

<properties>
    <local.sdk>/path/to/jar</local.sdk>
</properties>

Then, under plugins add a plugin to install the jars when compiling.然后,在plugins下添加一个插件来在编译时安装 jars。

<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>

Finally, in dependencies, you can add the jars最后,在依赖项中,您可以添加罐子

<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>

By Setting up your project like this, the project will continue to build even when you take it to another computer (given that it has all the jar files in the path specified by the property local.sdk ).通过像这样设置您的项目,即使您将项目带到另一台计算机上,该项目也将继续构建(假设它具有属性local.sdk指定的路径中的所有 jar 文件)。

For groupId use a unique name just to make sure that there are no conflicts.对于groupId使用唯一的名称只是为了确保没有冲突。

Now when you mvn install or mvn test local jars will be added automatically.现在,当您mvn installmvn test时,将自动添加本地 jar。

This is a short syntax for newer versions:这是较新版本的简短语法:

mvn install:install-file -Dfile=<path-to-file>

It works when the JAR was built by Apache Maven - the most common case.当 JAR 由 Apache Maven 构建时它可以工作——最常见的情况。 Then it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.然后它将在 META-INF 目录的子文件夹中包含一个 pom.xml,默认情况下会读取该文件。

Source: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html来源: http ://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

I want to share a code where you can upload a folder full of jars.我想分享一个代码,您可以在其中上传一个装满 jar 的文件夹。 It's useful when a provider doesn't have a public repository and you need to add lots of libraries manually.当提供者没有公共存储库并且您需要手动添加大量库时,它很有用。 I've decided to build a .bat instead of call directly to maven because It could be Out of Memory errors.我决定构建一个 .bat 而不是直接调用 maven,因为它可能是内存不足错误。 It was prepared for a windows environment but is easy to adapt it to linux OS:它是为 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();
    }

}

After run this main from any IDE, run the update_repo_maven.bat.从任何 IDE 运行此 main 后,运行 update_repo_maven.bat。

The preferred way would be to create your own remote repository.首选方法是创建您自己的远程存储库。

See here for details on how to do it.有关如何执行此操作的详细信息,请参见此处 Have a look at the ' Uploading to a Remote Repository ' section.查看“上传到远程存储库”部分。

  1. Create a local Maven repository directory, Your project root should look something like this to start with:创建一个本地 Maven 存储库目录,您的项目根目录应如下所示:
yourproject
+- pom.xml
+- src
  1. Add a standard Maven repository directory called repo for the group com.example and version 1.0:为组 com.example 和版本 1.0 添加一个名为 repo 的标准 Maven 存储库目录:
yourproject
+- pom.xml
+- src
+- repo
  1. Deploy the Artifact Into the Repo, Maven can deploy the artifact for you using the mvn deploy:deploy-file goal:将工件部署到存储库中,Maven 可以使用 mvn deploy:deploy-file 目标为您部署工件:
mvn deploy:deploy-file -Durl=file:///pathtoyour/repo -Dfile=your.jar -DgroupId=your.group.id -DartifactId=yourid -Dpackaging=jar -Dversion=1.0
  1. install pom file corresponding to your jar so that your project can find jar during maven build from local repo:安装与您的 jar 对应的 pom 文件,以便您的项目可以在本地 repo 的 maven 构建期间找到 jar:
mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
  1. add repo in your pom file:在你的 pom 文件中添加 repo:
<repositories>
    <!--other repositories if any-->
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>
  1. add the dependency in your pom:在你的 pom 中添加依赖项:
<dependency>
    <groupId>com.groupid</groupId>
    <artifactId>myid</artifactId>
    <version>1.0</version>
</dependency>

Also take a look at...也来看看...

<scope>compile</scope>

Maven Dependencies . Maven 依赖项 This is the default but I've found in some cases explicitly setting that scope also Maven to find local libraries in the local repository.这是默认设置,但我发现在某些情况下明确设置该范围还 Maven 以在本地存储库中查找本地库。

要安装第三方 jar,请调用如下命令

mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path

For some reason, in the web application I'm giving maintenance to, neither Alireza Fattahi's solution nor JJ Roman's solution worked correctly.出于某种原因,在我正在维护的 Web 应用程序中, Alireza Fattahi 的解决方案JJ Roman 的解决方案都不能正常工作。 In both cases, the compilation goes okay (it sees the jar), but the packaging fails to include the jar inside the war.在这两种情况下,编译都正常(它看到了 jar),但打包未能将 jar 包含在 war 中。

The only way I managed to make it work was by putting the jar on /src/main/webapp/WEB-INF/lib/ and then combining it with either Fattahis's or Roman's solution.我设法使其工作的唯一方法是将 jar 放在/src/main/webapp/WEB-INF/lib/上,然后将其与 Fattahis 或 Roman 的解决方案结合使用。

Not an answer to the original question, however it might be useful for someone不是原始问题的答案,但它可能对某人有用

There is no proper way to add multiple jar libraries from the folder using Maven.没有使用 Maven 从文件夹中添加多个 jar 库的正确方法。 If there are only few dependencies, it is probably easier to configure maven-install-plugin as mentioned in the answers above.如果只有很少的依赖项,那么配置maven-install-plugin可能更容易,如上面的答案中所述。

However for my particular case, I had a lib folder with more than 100 proprietary jar files which I had to add somehow.但是对于我的特殊情况,我有一个包含 100 多个专有 jar 文件的lib文件夹,我必须以某种方式添加这些文件。 And for me it was much easier for me to convert my Maven project to Gradle .对我来说,将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'

 }

Note that it is NOT necessarily a good idea to use a local repo.请注意,使用本地存储库不一定是个好主意。 If this project is shared with others then everyone else will have problems and questions when it doesn't work, and the jar won't be available even in your source control system!如果这个项目与其他人共享,那么其他人在它不起作用时都会遇到问题和疑问,并且即使在您的源代码控制系统中也无法使用该 jar!

Although the shared repo is the best answer, if you cannot do this for some reason then embedding the jar is better than a local repo.尽管共享 repo 是最好的答案,但如果由于某种原因你不能这样做,那么嵌入 jar 比本地 repo 更好。 Local-only repo contents can cause lots of problems, especially over time.仅限本地的 repo 内容可能会导致很多问题,尤其是随着时间的推移。

On your local repository you can install your jar by issuing the commands在本地存储库中,您可以通过发出命令来安装 jar

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

Follow this useful link to do the same from mkyoung's website.按照这个有用的链接从 mkyoung 的网站上做同样的事情。 You can also check maven guide for the same您也可以查看Maven 指南以获取相同的信息

  1. mvn install mvn 安装

You can write code below in command line or if you're using eclipse builtin maven right click on project -> Run As -> run configurations... -> in left panel right click on Maven Build -> new configuration -> write the code in Goals & in base directory :${project_loc:NameOfYourProject} -> Run您可以在命令行中编写下面的代码,或者如果您使用 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

Where each refers to:其中每个指的是:

< path-to-file >: the path to the file to load eg -> c:\kaptcha-2.3.jar <path-to-file>:要加载的文件的路径 eg -> c:\kaptcha-2.3.jar

< group-id >: the group that the file should be registered under eg -> com.google.code <group-id>: 文件应该注册的组 eg -> com.google.code

< artifact-id >: the artifact name for the file eg -> kaptcha < artifact-id >:文件的工件名称 eg -> kaptcha

< version >: the version of the file eg -> 2.3 <version>: 文件的版本 eg -> 2.3

< packaging >: the packaging of the file eg -> jar <packaging>:文件的打包 eg -> jar

2.After installed, just declares jar in pom.xml. 2.安装后,只需在pom.xml中声明jar即可。

 <dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3</version>
 </dependency>

Perhaps someone will be interested in: https://github.com/Limraj/maven-artifact-generator也许有人会感兴趣: https ://github.com/Limraj/maven-artifact-generator

Console program to generate maven artifacts in the local repository, and configure dependencies for pom.xml, based on the path to the jars.控制台程序,用于在本地存储库中生成 maven 工件,并根据 jar 的路径为 pom.xml 配置依赖项。 You can do this for one file, but it's most useful if you have multiple jar files.您可以对一个文件执行此操作,但如果您有多个 jar 文件,它最有用。

path jars: java -jar maven-artifact-generator-XXXjar -p path_to_jars -g com.test -V 1.2.3 -P jar路径罐子:java -jar maven-artifact-generator-XXXjar -p path_to_jars -g com.test -V 1.2.3 -P jar

jar: java -jar maven-artifact-generator-XXXjar -f file_jar -g com.test -V 1.2.3 -P jar jar: java -jar maven-artifact-generator-XXXjar -f file_jar -g com.test -V 1.2.3 -P jar

This will generate an artifact in the local maven repository, and generate dependecies for pom.xml in gen.log.这将在本地 maven 存储库中生成一个工件,并在 gen.log 中为 pom.xml 生成依赖项。 ArtifactId is the name of the jar file. ArtifactId 是 jar 文件的名称。

Requires an installed maven.需要已安装的 Maven。 Testing on widnows 7 and macOS X (unix/linux).在 widnows 7 和 macOS X (unix/linux) 上进行测试。

  1. Download jar file下载jar文件
  2. copy jar file to the project folder复制jar文件到项目文件夹
  3. get inteliJ idea Maven command area获取inteliJ idea Maven命令区
  4. type below command输入以下命令

mvn install:install-file -Dfile= YOUR_JAR_FILE_LOCATION *JARNAME .jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar * mvn install:install-file -Dfile= YOUR_JAR_FILE_LOCATION *JARNAME .jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar *


example:例子:

mvn install:install-file -Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar mvn install:install-file -Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=罐

THIS ANSWER IS ONLY FOR ECLIPSE USERS: 此答案仅适用于日食用户:

If you are using Eclipse, place the jar in lib/, right click on the jar name and click "add to build path". 如果使用的是Eclipse,请将jar放在lib /中,右键单击jar名称,然后单击“添加到构建路径”。 Eclipse will create a "referenced libraries" and place the jar for you Eclipse将创建一个“引用库”并为您放置jar

It resolved the import of the jar right away in the program for me 它立即为我解决了程序中jar的导入

I had the same error for a set of dependencies in my pom.xml turns out the versions of the dependencies was not specified in the pom.xml and was mentioned in the parent repository.我的 pom.xml 中的一组依赖项出现了相同的错误,结果是 pom.xml 中没有指定依赖项的版本,并且在父存储库中提到了这些版本。 For some reason the version details was not syncing with this repo.由于某种原因,版本详细信息未与此 repo 同步。 Hence i manually entered the versions using the tag and it worked like a charm.因此,我使用标签手动输入了版本,它就像一个魅力。 Little bit of time needed to look up the versions in the parent and specify here.在父项中查找版本并在此处指定需要一点时间。 But this can be done just for the jars that are showing the artifactid error and it works.但这仅适用于显示 artifactid 错误的 jar 并且有效。 Hope this helps someone.希望这可以帮助某人。

In Apache Maven 3.5.4, I had to add double quotation.在 Apache Maven 3.5.4 中,我不得不添加双引号。 Without double quotation it wasn't worked for me.没有双引号,它对我不起作用。

example: mvn install:install-file "-Dfile=location to the jar file" "-DgroupId=group id" "-DartifactId=artifact id" "-Dversion=version" "-Dpackaging=package type"例如: mvn install:install-file "-Dfile=位置到 jar 文件" "-DgroupId=group id" "-DartifactId=artifact id" "-Dversion=version" "-Dpackaging=package type"

My shaded jar file did not contain the third-party library using AlirezaFattahi's solution.我的阴影 jar 文件不包含使用 AlirezaFattahi 解决方案的第三方库。 however, I remeber that it was working once I had tried it for the same project last time.但是,我记得上次我为同一个项目尝试过它后它就可以工作了。 So, I tried my own solution:所以,我尝试了自己的解决方案:

  1. mkdir the project's path under .m2/repositories directory (similar to other maven dependencies directory at that directory) mkdir .m2/repositories 目录下的项目路径(类似于该目录下的其他 maven 依赖项目录)
  2. put the third-party jar file in it.将第三方jar文件放入其中。
  3. add the dependency as like as the libraries on a maven repository.添加依赖项,就像 Maven 存储库上的库一样。

Finally, It worked for me.最后,它对我有用。 :) :)

In my case, it turned out my existing maven build was already installing the jar file in question into my local repository, but I wasn't seeing the expected results because local builds produce artifacts with suffix '-SNAPSHOT' in them.就我而言,事实证明我现有的 maven 构建已经将有问题的 jar 文件安装到我的本地存储库中,但我没有看到预期的结果,因为本地构建会生成带有后缀“-SNAPSHOT”的工件。 The very simple solution for me was to update the pom.xml file of the projects that make use of the locally build jar file, to match the name.对我来说非常简单的解决方案是更新使用本地构建 jar 文件的项目的 pom.xml 文件,以匹配名称。

<myproject.foo.version>1.88-SNAPSHOT</myproject.foo.version>

instead of代替

<myproject.foo.version>1.88</myproject.foo.version>

The takeaway is that if it seems like your jar file should already be in your local .m2/maven repo, it very well may be, and you just need to update your dependency accordingly.要点是,如果您的 jar 文件似乎应该已经在您的本地 .m2/maven 存储库中,那么很可能是,您只需要相应地更新您的依赖项。

If you are a Spring developer, you will find that the JAR you produce from a Spring Boot build will not work as a dependency JAR in another build.如果您是 Spring 开发人员,您会发现您从 Spring Boot 构建生成的 JAR 不会在另一个构建中作为依赖项 JAR 工作。 Check if your POM has this:检查你的 POM 是否有这个:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

And also check, if you run mvn package whether you see a .jar.original file as well as the .jar in the target/ directory.并且还要检查,如果您运行mvn package ,您是否在target/目录中看到 .jar.original 文件以及 .jar 文件。 If so, you need to use that .jar.original file for any local development (putting it into a lib/ or something.)如果是这样,您需要将该 .jar.original 文件用于任何本地开发(将其放入 lib/ 或其他东西中。)

You can work around this of course;你当然可以解决这个问题; see the original post which references the plugin manual at Spring.请参阅在 Spring 中引用插件手册的原始帖子 I think, you would need the <configuration> <attach>false</attach> <configuration> and then you would have to deploy and Maven should use the .jar.original file.我认为,你需要<configuration> <attach>false</attach> <configuration>然后你必须部署并且 Maven 应该使用 .jar.original 文件。 I have not tried this myself so do let me know!我自己没有尝试过,所以请告诉我!

a pom repository pointing to ${project.directory}/repo/ or a file:// uri can work with source-resident jarfiles for a trivial project's deps frozen in time.指向${project.directory}/repo/或 file:// uri 的 pom 存储库可以与源驻留的 jarfile 一起使用,以便及时冻结简单项目的 deps。 these will not leak into the system ~/.m2 repo but will affect the choice of maven version selection against the (typically more recent) top-level when unspecified.这些不会泄漏到系统 ~/.m2 存储库中,但会在未指定时影响对(通常是较新的)顶级的 Maven 版本选择的选择。

for existing deps being bundled at distribution-time similar to shade but more direct, if you can run with "target" being your favorite structure or maven's default:对于在分发时捆绑的现有 deps,类似于 shade 但更直接,如果您可以将“target”作为您最喜欢的结构或 maven 的默认值运行:

-cp "$PWD/target/classes:$PWD/target/lib/*" pkg.random.ClassWithMain

then然后

<build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

I had the same problem with ojdbc6. 我对ojdbc6有同样的问题。 I saw this link but, it did'nt work. 我看到了此链接,但是没有用。 The command was correct but I needed one parameter more, 该命令是正确的,但我还需要一个参数,

This is the link: http://roufid.com/3-ways-to-add-local-jar-to-maven-project/ 这是链接: http : //roufid.com/3-ways-to-add-local-jar-to-maven-project/

Here is the example: 这是示例:

install:install-file -Dfile=C:\driversDB\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM