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

发布于 2024-10-16 18:34:51 字数 50 浏览 5 评论 0 原文

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

How do I add local jar files (not yet part of the Maven repository) directly in my project's library sources?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

偏闹i 2024-10-23 18:34:52

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

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

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:

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:

<url>file:${basedir}</url>
七秒鱼° 2024-10-23 18:34:52
<dependency>
    <groupId>group id name</groupId>
    <artifactId>artifact name</artifactId>
    <version>version number</version>
    <scope>system</scope>
    <systemPath>jar location</systemPath>
</dependency>
<dependency>
    <groupId>group id name</groupId>
    <artifactId>artifact name</artifactId>
    <version>version number</version>
    <scope>system</scope>
    <systemPath>jar location</systemPath>
</dependency>
最后的乘客 2024-10-23 18:34:52

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

Important part in dependency is:
${pom.basedir} (instead of just ${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>
尐籹人 2024-10-23 18:34:52

在 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 中,如下所示:

在此处输入图像描述

Add your own local JAR in POM file and use that in maven build.

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:

enter image description here

长亭外,古道边 2024-10-23 18:34:52

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>

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

如果您有多个外部 jar,只需对其他项目重复 即可。 jar 位于同一个 maven-install-plugin

第 2 步: 一旦您配置了 maven-install-plugin,如上面的 < code>pom.xml 文件,您必须像平常一样在 pom.xml 中使用这些 jar:

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

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

这是一个小问题,但有时很容易被忽视。

Step 1: Configure the maven-install-plugin with the goal install-file in your pom.xml

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

If you have multiple external jars, just repeat the <execution> for other jars within the same maven-install-plugin.

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:

    <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. That's it. It doesn't automatically include these jars as maven dependencies to your project.

It's a minor point, but sometimes easy to miss.

九歌凝 2024-10-23 18:34:52

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

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

http://maven.apache.org/plugins/maven -安装插件/安装文件-mojo.html

One way is to upload it to your own Maven repository manager (such as 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. It's a little but small overhead and no manual step involved.

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

薯片软お妹 2024-10-23 18:34:52

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

如果您需要这些 jar 进行编译,请检查此相关问题: 我可以将 jar 添加到 maven 2 构建类路径而不安装它们吗?

另外,在有人建议之前,不要使用系统范围。

Of course you can add jars to that folder. 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?

Also, before anyone suggests it, do NOT use the system scope.

別甾虛僞 2024-10-23 18:34:52

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

  1. 在项目中创建一个文件夹 libs
  2. 在 pom.xml 文件中添加以下行

    ${pom.basedir}/libs/;
    
    
    <存储库>
       <存储库>
            本地-maven-存储库
            文件://${local.repository.folder}
            <发布>
                <启用>true
            
            <快照>
                <启用>true
            
       
    
    
  3. 打开 .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

复制库中的所有内容文件夹(同样包括 .m2/repository 下的文件夹),您就完成了。

Another interesting case is when you want to have in your project private maven jars. You may want to keep the capabilities of Maven to resolve transitive dependencies. The solution is fairly easy.

  1. Create a folder libs in your project
  2. Add the following lines in your pom.xml file

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

E.g. 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

com/mycompany/myproject/1.2.3

Copy everything in your libs folder (again, including the folders under .m2/repository) and you are done.

小伙你站住 2024-10-23 18:34:52

将本地 jar 库、其 sourcesjavadoc 添加到 Maven 项目

如果您已使用以下命令预编译了 jar 文件库,它们的javadoc,然后你可以将它们安装到你的本地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用法。


或者您可以使用 源 和 javadoc 自己构建这些库/maven-source-plugin/usage.html" rel="noreferrer">maven-source-pluginmaven-javadoc-plugin,然后安装它们。

示例项目:

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

Add local jar libraries, their sources and javadoc to a Maven project

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:

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.


Or you can build these libraries yourself with their sources and javadoc using maven-source-plugin and maven-javadoc-plugin, and then install them.

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:

mvn install

Check your local Maven repository:

~/.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>
凝望流年 2024-10-23 18:34:52

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

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

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

来添加 jar

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

对于 groupId 使用唯一的名称只是为了确保不存在冲突。

现在,当您 mvn installmvn test 时,将自动添加本地 jar。

I think a better solution for this problem is to use maven-install-plugin to automatically install the files at install time. This is how I set it up for my project.

First, add the path (where you store the local .jars) as a property.

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

Then, under plugins add a plugin to install the jars when compiling.

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

For groupId use a unique name just to make sure that there are no conflicts.

Now when you mvn install or mvn test local jars will be added automatically.

青衫负雪 2024-10-23 18:34:52

命令行:

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

command line :

mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
み零 2024-10-23 18:34:52

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

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

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. Then it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.

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

ˇ宁静的妩媚 2024-10-23 18:34:52

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

没有正确的方法可以使用 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'

 }

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. If there are only few dependencies, it is probably easier to configure maven-install-plugin as mentioned in the answers above.

However for my particular case, I had a lib folder with more than 100 proprietary jar files which I had to add somehow. And for me it was much easier for me to convert my Maven project to 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'

 }
土豪 2024-10-23 18:34:52

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

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

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.

不美如何 2024-10-23 18:34:52

我想分享一个代码,您可以在其中上传一个装满罐子的文件夹。当提供商没有公共存储库并且您需要手动添加大量库时,它非常有用。我决定构建一个 .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。

I want to share a code where you can upload a folder full of jars. 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. It was prepared for a windows environment but is easy to adapt it to linux OS:

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.

温柔女人霸气范 2024-10-23 18:34:52
  1. 创建本地 Maven 存储库目录,您的项目根目录应该如下所示:
yourproject
+- pom.xml
+- src
  1. 为 com.example 组和版本 1.0 添加一个名为 repo 的标准 Maven 存储库目录:
yourproject
+- pom.xml
+- src
+- repo
  1. 将 Artifact 部署到 Repo 中,Maven 可以为您使用mvn部署:部署文件目标:
mvn deploy:deploy-file -Durl=file:///pathtoyour/repo -Dfile=your.jar -DgroupId=your.group.id -DartifactId=yourid -Dpackaging=jar -Dversion=1.0
  1. 安装与您的jar相对应的pom文件,以便您的项目可以在maven构建期间从本地存储库找到jar:
mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
  1. 在您的pom文件中添加存储库:
<repositories>
    <!--other repositories if any-->
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>
  1. 在您的pom中添加依赖项:
<dependency>
    <groupId>com.groupid</groupId>
    <artifactId>myid</artifactId>
    <version>1.0</version>
</dependency>
  1. Create a local Maven repository directory, Your project root should look something like this to start with:
yourproject
+- pom.xml
+- src
  1. Add a standard Maven repository directory called repo for the group com.example and version 1.0:
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:
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:
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:
<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:
<dependency>
    <groupId>com.groupid</groupId>
    <artifactId>myid</artifactId>
    <version>1.0</version>
</dependency>
北座城市 2024-10-23 18:34:52

另请查看...

<scope>compile</scope>

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

Also take a look at...

<scope>compile</scope>

Maven Dependencies. 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.

所谓喜欢 2024-10-23 18:34:52

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

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

To install third party jar, Please call the command like below

mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path
許願樹丅啲祈禱 2024-10-23 18:34:52

由于某种原因,在我要维护的网络应用程序中,Alireza Fattahi 的解决方案JJ Roman 的解决方案工作正常。在这两种情况下,编译都顺利(它看到了 jar),但打包未能将 jar 包含在 war 中。

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

For some reason, in the web application I'm giving maintenance to, neither Alireza Fattahi's solution nor JJ Roman's solution worked correctly. In both cases, the compilation goes okay (it sees the jar), but the packaging fails to include the jar inside the 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.

梨涡 2024-10-23 18:34:52
  1. 下载 jar 文件
  2. 将 jar 文件复制到项目文件夹
  3. get inteliJ idea Maven 命令区
  4. 在命令下面键入

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:安装文件
-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

  1. Download jar file
  2. copy jar file to the project folder
  3. get inteliJ idea Maven command area
  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*


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

|煩躁 2024-10-23 18:34:52

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

尽管共享存储库是最好的答案,但如果由于某种原因无法执行此操作,那么嵌入 jar 比本地存储库更好。仅限本地的存储库内容可能会导致很多问题,尤其是随着时间的推移。

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!

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. Local-only repo contents can cause lots of problems, especially over time.

世界和平 2024-10-23 18:34:52

在本地存储库上,您可以通过发出命令来安装 jar

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

遵循这个有用的 链接,从 mkyoung 的网站执行相同操作。您还可以查看 maven 指南以获取相同的内容

On your local repository you can install your jar by issuing the commands

 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. You can also check maven guide for the same

梦言归人 2024-10-23 18:34:52
  1. mvn 安装

您可以在命令行中编写下面的代码,或者如果您使用的是 eclipse 内置 maven,请右键单击项目 ->运行方式->运行配置...->在左侧面板中右键单击 Maven Build ->新配置->在“目标和”中编写代码在基本目录中:${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:\kaptcha-2.3.jar

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

< artifact-id >:文件的工件名称,例如 ->卡普

查version >:文件的版本,例如-> 2.3

<打包>:文件的打包例如->罐

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

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

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

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 e.g -> c:\kaptcha-2.3.jar

< group-id >: the group that the file should be registered under e.g -> com.google.code

< artifact-id >: the artifact name for the file e.g -> kaptcha

< version >: the version of the file e.g -> 2.3

< packaging >: the packaging of the file e.g. -> jar

2.After installed, just declares jar in pom.xml.

 <dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3</version>
 </dependency>
清晨说晚安 2024-10-23 18:34:52

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

例子:
mvn install:install-file "-Dfile=jar 文件的位置" "-DgroupId=组 id" "-DartifactId=artifact id" "-Dversion=版本" "-Dpackaging=包类型"

In Apache Maven 3.5.4, I had to add double quotation. 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"

森罗 2024-10-23 18:34:52

也许有人会感兴趣:
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) 上进行测试。

Perhaps someone will be interested in:
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.
You can do this for one file, but it's most useful if you have multiple jar files.

path jars:
java -jar maven-artifact-generator-X.X.X.jar -p path_to_jars -g com.test -V 1.2.3 -P jar

jar:
java -jar maven-artifact-generator-X.X.X.jar -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. ArtifactId is the name of the jar file.

Requires an installed maven.
Testing on widnows 7 and macOS X (unix/linux).

三岁铭 2024-10-23 18:34:51

您可以直接添加本地依赖项(如构建maven项目中所述包含专有库),如下所示:

<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 组中提出了关于此的问题 https://issues.apache.org/jira/ browser/MNG-6523(您可以参与并描述为什么此功能在某些情况下很有帮助)。我希望这个功能仍然存在!

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

You can add local dependencies directly (as mentioned in build maven project with propriatery libraries included) like this:

<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). 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). 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. If this feature is removed, well, there are lots of good answers here which I can chose from later!

暮倦 2024-10-23 18:34:51

将 JAR 安装到本地 Maven 存储库(通常是主文件夹中的 .m2),如下所示:

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

其中each指的是:

:路径到要加载的文件 例如 → c:\kaptcha-2.3.jar

:文件应注册的组 例如 → com.google.code

:文件的工件名称,例如 → kaptcha

:文件的版本 例如 → 2.3

:文件的打包 例如 → jar

参考

Install the JAR into your local Maven repository (typically .m2 in your home folder) as follows:

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 e.g → c:\kaptcha-2.3.jar

<group-id>: the group that the file should be registered under e.g → com.google.code

<artifact-id>: the artifact name for the file e.g → kaptcha

<version>: the version of the file e.g → 2.3

<packaging>: the packaging of the file e.g. → jar

Reference

度的依靠╰つ 2024-10-23 18:34:51

首先,我想将此答案归功于匿名 Stack Overflow 用户 - 我很确定我以前在这里见过类似的答案 - 但现在我找不到它。

将本地 JAR 文件作为依赖项的最佳选择是创建本地 Maven 存储库。这样的存储库只不过是一个包含 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 存储库可以提交到代码存储库,整个设置是系统的独立的。 (GitHub 中的工作示例)。

我同意将 JAR 提交到源代码存储库并不是一种好的做法,但在现实生活中,快速而肮脏的解决方案有时比一个完整的 Nexus 存储库来托管一个您无法发布的 JAR 更好。

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.

The best option for having local JAR files as a dependency is to create a local Maven repository. Such a repository is nothing more than a proper directory structure with pom files in it.

For my example:
I have my master project on ${master_project} location and subproject1 is on ${master_project}/${subproject1}.

Then I create a Maven repository in:
${master_project}/local-maven-repo.

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:

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

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

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. (Working example in 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.

夏见 2024-10-23 18:34:51

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

只需在 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]

Create a new folder, let's say local-maven-repo at the root of your Maven project.

Just add a local repo inside your <project> of your pom.xml:

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

mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]
似狗非友 2024-10-23 18:34:51

我想要这样的解决方案 - 在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初始化并且jar将安装在本地maven存储库中。现在,此 jar 在本机上的任何 Maven 步骤中都可用(不要忘记将此依赖项与 pom 中的任何其他 Maven 依赖项一样包含在 标记中)。也可以将 jar 安装绑定到 initialize 步骤,而是绑定到您喜欢的任何其他步骤。

I'd like such solution - use maven-install-plugin in pom file:

<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. 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). It is also possible to bind jar install not to initialize step, but any other step you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文