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


当前回答

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

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

当JAR由ApacheMaven构建时,这是最常见的情况。然后它将在META-INF目录的子文件夹中包含一个pom.xml,默认情况下将读取该文件。

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

其他回答

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

yourproject
+- pom.xml
+- src

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

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

将工件部署到Repo中,Maven可以使用mvn Deploy:Deploy文件目标为您部署工件:

mvn deploy:deploy-file -Durl=file:///pathtoyour/repo -Dfile=your.jar -DgroupId=your.group.id -DartifactId=yourid -Dpackaging=jar -Dversion=1.0

安装与jar对应的pom文件,以便您的项目可以在maven构建过程中从本地repo找到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>

步骤1:使用pom.xml中的目标安装文件配置maven安装插件

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

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

如果您有多个外部jar,只需对同一maven安装插件中的其他jar重复<execution>即可。

步骤2:在pom.xml文件中如上所示配置maven安装插件后,您必须像往常一样使用pom.xml中的这些jar:

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

注意,maven安装插件只将外部jar复制到本地的.m2 maven存储库中。就是这样。它不会自动将这些jar作为maven依赖项包含到项目中。

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

请注意,使用本地回购并不一定是个好主意。如果这个项目与其他人共享,那么当它不起作用时,其他人都会遇到问题和疑问,甚至在您的源代码管理系统中也无法使用jar!

尽管共享回购是最好的解决方案,但如果由于某种原因无法做到这一点,那么嵌入jar比本地回购要好。仅限本地的回购内容可能会导致很多问题,特别是随着时间的推移。

<dependency>
    <groupId>group id name</groupId>
    <artifactId>artifact name</artifactId>
    <version>version number</version>
    <scope>system</scope>
    <systemPath>jar location</systemPath>
</dependency>

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