ChatGPT解决这个技术问题 Extra ChatGPT

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

如何直接在项目的库源中添加本地 jar 文件(还不是 Maven 存储库的一部分)?

嗨@Praneel PIDIKITI,您能否将接受的答案更改为得票最多的答案?
@nslntmnx 这不是更好的解决方案,因为所有解决方案都有缺点stackoverflow.com/questions/364114/…
如果您的库有时会更新或扩展,请参阅 this answerI want to load all JARs from my libs project folder with maven 以了解“pomified”方式,从而避免额外的 repo 文件夹和繁琐的 cmd 行或 install-file 脚本。

N
Nicolás Alarcón Rapela

您可以像这样直接添加本地依赖项(如 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>

更新

在新版本中,此功能被标记为已弃用但仍在工作且尚未删除(您只会在 maven 启动期间在日志中看到警告)。 maven group 提出了关于此 https://issues.apache.org/jira/browse/MNG-6523 的问题(您可以参与并描述此功能在某些情况下有用的原因)。我希望这个功能仍然存在!

如果您问我,只要不删除该功能,我就会使用它来仅依赖我的项目中一个不适合存储库的顽皮 jar 文件。如果去掉这个功能,好吧,这里有很多好的答案,我以后可以从中选择!


例如,有时您想专门测试一个旧罐子,我认为这个答案非常适合。这是我需要的。赞成
尽管看起来很简单,但这个解决方案的问题是 yourJar.jar 不会包含在您的应用程序的 WAR 文件中。
上述解决方案不再起作用,它返回:“'dependencies.dependency.systemPath' for xx.jar 不应指向项目目录中的文件” 这已在 stackoverflow.com/questions/10935135/… 中处理
在这个答案中,artifactIdgroupId 不是错误的方式吗?
根据 Maven 文档 (maven.apache.org/guides/introduction/…):重要说明:这已标记为已弃用。
T
Thorbjørn Ravn Andersen

将 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:\kaptcha-2.3.jar

<group-id>:文件应注册的组,例如 → com.google.code

<artifact-id>:文件的工件名称,例如 → kaptcha

<version>:文件的版本,例如 → 2.3

<packaging>:文件的打包如 → jar

参考

Maven 常见问题解答:我有一个想要放入本地存储库的 jar。怎么复制进去?

Maven 安装插件用法: install:install-file 目标


除了 generatePom 部分之外,在我的构建上安装的说明包含所有内容。这似乎至关重要。
答案没有提到自述文件或罐子被带来。但是,如果项目带来了 jars,那么您不妨将 repo 放入项目中,如此处提到的 stackoverflow.com/a/36602256/1000011 那么您不需要 README,因为该项目就像 jars 在 maven Central 中一样工作,没有任何额外的手动步骤。
@opticyclic 你的评论需要更多的支持,或者这个答案需要编辑。对于没有意识到安装到本地 Maven 存储库不会包含其他所有人的新手来说,这是一个灾难的秘诀。
有没有办法安装本地 java 文档和源代码?
10 岁了……而且很有用!非常感谢!
0
0xCursor

首先,我想将这个答案归功于一个匿名的 Stack Overflow 用户——我很确定我以前在这里看到过类似的答案——但现在我找不到了。

将本地 JAR 文件作为依赖项的最佳选择是创建本地 Maven 存储库。这样的存储库只不过是一个适当的目录结构,其中包含 pom 文件。

对于我的示例:我的主项目位于 ${master_project} 位置,而 subproject1 位于 ${master_project}/${subproject1}

然后我在 ${master_project}/local-maven-repo 中创建一个 Maven 存储库。

在位于 ${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 提交到源代码 repo 不是一个好习惯,但在现实生活中,快速而肮脏的解决方案有时比一个完整的 Nexus repo 更好地托管一个您无法发布的 JAR。


本地相对目录作为 maven 存储库...帮助很大
如果您想在 pom.xml 中执行此操作,请查看 baeldung.com/install-local-jar-with-maven
由于 ${project.parent.basedir} 现在似乎无法解决任何问题,因此我使用了 ${project.basedir}/.. 并且效果很好。
OP找不到的stackoverflow答案可能在这里:stackoverflow.com/a/19226672/2364405
提示:请检查 $HOME/.m2/settings.xml ,避免 "local-maven-repo" 被 settings/mirrors/mirror <mirrorOf>*</mirrorOf> 镜像。
V
Vinay Veluri

在 Maven 项目的根目录下创建一个新文件夹,例如 local-maven-repo

只需在 pom.xml<project> 中添加一个本地存储库:

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

这是此处唯一正确的答案,因为它会在使用部署时正确创建您的存储库。
@user2748659 是的,如果在您的 CI 构建服务器中,文件夹 local-maven-repo 包含在您的源文件夹中(在此示例中为子文件夹)
这个答案对我有用。对于共享项目,将 repo 放在项目目录中并添加到版本控制中可以确保签出项目的任何人都不会缺少依赖项。如果您有很多依赖项,那么共享的远程 repo 可能是更好的解决方案,否则将 repo 保存在项目目录中是非常好的。
我们还需要将其添加为依赖项吗?
请注意,为此您可能需要添加 -Dpackaging=jar。否则你会得到“工件信息不完整或无效:包装丢失”。
M
Miss Chanandler Bong

我想要这样的解决方案 - 在 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 repo 中。现在这个 jar 在这台机器上的任何 maven 步骤中都可用(不要忘记在 pom 中包含这个依赖项作为带有 <dependency></dependency> 标记的任何其他 maven 依赖项)。也可以不将 jar install 绑定到 initialize 步骤,而是绑定到您喜欢的任何其他步骤。


这对我很有效,但前提是我在 mvn package 之前运行 mvn initialize:我不能 mvn initialize package 否则它会尝试从中央存储库下载 JAR。为什么是这样?我认为它会按顺序运行这个目标/阶段。
实际上,它们应该按顺序运行。查看默认生命周期列表:maven.apache.org/guides/introduction/…您可以使用另一个步骤进行绑定。
我尝试了所有方法,但最后我不得不求助于这个解决方案。主要原因是,我希望能够在离线模式下本地构建包。如果我将其声明为具有本地定义的存储库的依赖项,则始终将其视为另一个在线存储库,并且 maven 构建抱怨没有对工件进行优化。此解决方案适用于每种情况。
我认为最好使用 clean 阶段,因为每次我们使用 mvn package 时都会执行初始化,而这是不必要的。最后,如果我们只需要生成 jar/war 我们可以直接使用 mvn clean 包。
也可以绑定 jar install 不是初始化步骤,而是你喜欢的任何其他步骤。”不一定是真的。如果依赖项尚未在 repo 中,并且使用了在解决依赖项的阶段之后的阶段(例如 compile),则构建将失败。
f
fl0w

真正快速而肮脏的方法是指向本地文件,请注意“系统”现在已弃用:

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

对于本地存储库 URL,它们可以是相对的还是必须是绝对的?
@Dragas 我没试过,如果你试过,请告诉我们。
是的,显然您需要使用 <url>file:${basedir}</url> 作为基本网址。
<scope>system 已弃用。
@GeroldBroser 感谢您的输入,我添加了一条注释。干杯!
Y
Youcef Laidani
<dependency>
    <groupId>group id name</groupId>
    <artifactId>artifact name</artifactId>
    <version>version number</version>
    <scope>system</scope>
    <systemPath>jar location</systemPath>
</dependency>

<scope>system 现在已弃用。
@GeroldBroser - 好的。我们可以用什么代替它?
@MasterJoe2 正如 the accepted answer install:install-file 中提到的,将工件添加到本地 repo 并将其用作“正常”依赖项(具有默认范围 compile)或使用 in-project repository solution
k
kolle_86

依赖的重要部分是:${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>

<scope>system 已弃用。
A
Aurasphere

在 POM 文件中添加您自己的本地 JAR 并在 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 中:

https://i.stack.imgur.com/ywD6y.png


我收到错误无法安装工件(访问被拒绝)。我该如何解决这个问题? @Aurasphere
@RamzahRehman 尝试通过右键单击以管理员权限打开命令提示符,然后选择“以管理员身份运行”
A
Amit

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

确保根据您的实际文件路径编辑 file 路径(建议将这些外部非 maven jar 放在某个文件夹中,比如说 lib,并将此 lib 文件夹放在您的项目中以便使用项目特定的相对路径,避免添加系统特定的绝对路径。

如果您有多个外部 jar,只需对同一 maven-install-plugin 中的其他 jar 重复 <execution>

第 2 步:pom.xml 文件中如上所示配置 maven-install-plugin 后,您必须照常在 pom.xml 中使用这些 jar:

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

请注意,maven-install-plugin 仅将您的外部 jar 复制到您的本地 .m2 maven 存储库。而已。它不会自动将这些 jars 作为 maven 依赖项包含到您的项目中。

这是一个小问题,但有时很容易错过。


如果您运行例如 mvn compile 并且如果在构建之前没有 mvn clean 将失败,则没有 <phase>clean
C
Community

一种方法是将其上传到您自己的 Maven 存储库管理器(例如 Nexus)。无论如何,拥有自己的存储库管理器是一种很好的做法。

我最近看到的另一种好方法是在构建生命周期中包含 Maven 安装插件:您在 POM 中声明将文件安装到本地存储库。这是一个很小但很小的开销,并且不涉及手动步骤。

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


最终切换到 gradle。不起作用如果本地jar被定义为依赖,maven在解析依赖之前不会执行插件,手动安装是不可避免的。找到了关于这种情况的讨论:stackoverflow.com/questions/5951999/…
C
Community

当然,您可以将 jars 添加到该文件夹中。但也许它不是你想要实现的......

如果您需要这些 jar 进行编译,请检查以下相关问题:Can I add jars to maven 2 build classpath without installing them?

此外,在任何人建议之前,不要使用系统范围。


N
Nic Cottrell

另一个有趣的案例是当你想在你的项目中拥有私有的 maven jars。您可能希望保留 Maven 的功能来解决传递依赖关系。解决方案相当简单。

在您的项目中创建一个文件夹 libs 在您的 pom.xml 文件中添加以下行 ${pom.basedir}/libs/ local-maven-repository file://${local.repository.folder} true true 打开 .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 下的文件夹),您就完成了。


H
Hassani

命令行 :

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

佚名

将本地 jar 库、它们的源代码和 javadoc 添加到 Maven 项目

如果您使用库、它们的 sourcesjavadoc 预编译了 jar 文件,那么您可以将它们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

然后在您的项目中,您可以使用以下库:

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

请参阅:maven-install-plugin 用法。

或者,您可以使用 maven-source-pluginmaven-javadoc-plugin 自己build 这些库及其 sourcesjavadoc,然后install 它们。

示例项目:图书馆

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

执行 maven install 目标:

mvn install

检查您的本地 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

然后你可以使用这个库:

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

S
Sudara

我认为解决此问题的更好方法是使用 maven-install-plugin 在安装时自动安装文件。这就是我为我的项目设置它的方式。

首先,将路径(存储本地 .jars 的位置)添加为属性。

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

然后,在 plugins 下添加一个插件以在编译时安装 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>

最后,在依赖项中,您可以添加罐子

<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 installmvn test 时,将自动添加本地 jar。


j
joro

这是较新版本的简短语法:

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

当 JAR 由 Apache Maven 构建时它可以工作——最常见的情况。然后它将在 META-INF 目录的子文件夹中包含一个 pom.xml,默认情况下会读取该文件。

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


S
Simeon

首选方法是创建您自己的远程存储库。

有关如何执行此操作的详细信息,请参阅 here。查看“上传到远程存储库”部分。


L
Luis Muzikant

我想分享一个代码,您可以在其中上传一个装满 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。


您的代码 String symbolicName = attributes.getValue("Bundle-SymbolicName"); if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) 似乎表明仅支持 custom jar。这不是我们需要的:而是一堆第三方罐子。您对如何以这种方式安装 any jar 有什么建议吗?
我已经修复了您的代码:我在底部给出了答案。
D
Dean Jain

创建一个本地 Maven 存储库目录,您的项目根目录应如下所示:

yourproject
+- pom.xml
+- src

为组 com.example 和版本 1.0 添加一个名为 repo 的标准 Maven 存储库目录:

yourproject
+- pom.xml
+- src
+- repo

将工件部署到存储库中,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

安装与您的 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

在你的 pom 文件中添加 repo:

<repositories>
    <!--other repositories if any-->
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>

在你的 pom 中添加依赖项:

<dependency>
    <groupId>com.groupid</groupId>
    <artifactId>myid</artifactId>
    <version>1.0</version>
</dependency>

k
kervin

也来看看...

<scope>compile</scope>

Maven Dependencies。这是默认设置,但我发现在某些情况下明确设置该范围还 Maven 以在本地存储库中查找本地库。


A
Alexander Farber

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

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

H
Haroldo_OK

出于某种原因,在我进行维护的 Web 应用程序中,Alireza Fattahi's solutionJJ Roman's solution 都不能正常工作。在这两种情况下,编译都正常(它看到了 jar),但打包未能将 jar 包含在 war 中。

我设法使它工作的唯一方法是将罐子放在 /src/main/webapp/WEB-INF/lib/ 上,然后将它与 Fattahis 或 Roman 的解决方案结合起来。


A
Anton

不是原始问题的答案,但它可能对某人有用

没有使用 Maven 从文件夹中添加多个 jar 库的正确方法。如果只有很少的依赖项,那么配置 maven-install-plugin 可能更容易,如上面的答案中所述。

但是对于我的特殊情况,我有一个 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'

 }

j
jwvh

请注意,使用本地存储库不一定是个好主意。如果这个项目与其他人共享,那么其他人在它不起作用时都会遇到问题和疑问,并且即使在您的源代码控制系统中也无法使用该 jar!

尽管共享 repo 是最好的答案,但如果由于某种原因你不能这样做,那么嵌入 jar 比本地 repo 更好。仅限本地的 repo 内容可能会导致很多问题,尤其是随着时间的推移。


如果您在源代码控制系统中添加 jar,则库将始终与源代码一起可用。没有来源,没有图书馆。使用 maven,源可能没问题,但存储库不可用。
@Frank ...关于如何在不包含外部依赖项(库文件)的情况下创建可执行 jar 的任何想法?
对于刚接触 Maven 并正在寻找原始问题的答案的人来说,“嵌入 jar”是什么意思?
b
bpjoshi

在本地存储库中,您可以通过发出命令来安装 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


M
Mazen Embaby

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

其中每个指的是:

:要加载的文件的路径 eg -> c:\kaptcha-2.3.jar

: 文件应该注册的组 eg -> com.google.code

< artifact-id >:文件的工件名称 eg -> kaptcha

: 文件的版本 eg -> 2.3

:文件的打包 eg -> jar

2.安装后,只需在pom.xml中声明jar即可。

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

K
Kamil Tomasz Jarmusik

也许有人会对:https://github.com/Limraj/maven-artifact-generator

控制台程序,用于在本地存储库中生成 maven 工件,并根据 jar 的路径为 pom.xml 配置依赖项。您可以对一个文件执行此操作,但如果您有多个 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

这将在本地 maven 存储库中生成一个工件,并在 gen.log 中为 pom.xml 生成依赖项。 ArtifactId 是 jar 文件的名称。

需要已安装的 Maven。在 widnows 7 和 macOS X (unix/linux) 上进行测试。


R
Ranjan Rozario

我的 pom.xml 中的一组依赖项出现了相同的错误,结果是 pom.xml 中没有指定依赖项的版本,并且在父存储库中提到了这些版本。由于某种原因,版本详细信息未与此 repo 同步。因此,我使用标签手动输入了版本,它就像一个魅力。在父项中查找版本并在此处指定需要一点时间。但这仅适用于显示 artifactid 错误的 jar 并且有效。希望这可以帮助某人。


K
Kavindu Gayan

在 Apache Maven 3.5.4 中,我不得不添加双引号。没有双引号,它对我不起作用。

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