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


当前回答

我想要这样的解决方案-在pom文件中使用maven安装插件:

<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 repo中。现在,这个jar可以在这台机器上的任何maven步骤中使用(不要忘记在pom中使用<dependency></dependency>标记将这个依赖项作为其他maven依赖项)。也可以不将jar安装绑定到初始化步骤,而是绑定到您喜欢的任何其他步骤。

其他回答

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

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

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

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

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

步骤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依赖项包含到项目中。

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

指向${project.directory}/rebo/或file://uri的pom存储库可以与源驻留jarfiles一起工作,用于一个小项目的dep。这些信息不会泄漏到系统~/.m2 repo中,但如果未指定,则会影响maven版本选择与(通常是最近的)顶级版本的选择。

对于现有的dep,如果您可以使用“target”作为您最喜欢的结构或maven的默认值运行,则在分发时绑定的dep与shade类似,但更直接:

-cp“$PWD/target/classs:$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>

创建一个本地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>