如何将本地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

其他回答

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

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>

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

指向${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>

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

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

点击这个有用的链接,从mkyoung的网站进行同样的操作。您也可以查看maven指南