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


当前回答

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

其他回答

下载jar文件将jar文件复制到项目文件夹get inteliJ idea Maven命令区键入以下命令

mvn安装:安装文件-Dfile=YOUR_JAR_file_LOCATION*JARNAME.JAR-DgroupId=org.primefaces.themes-DastifactId=iMetro-Dversion=1.0.1-Dpackage=JAR*


例子:

mvn安装:安装文件-Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar-DgroupId=org.primefaces.themes-DartifactId=iMetro-Dversion=1.0.1-Dpackage=jar

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

如果您有带有库、源代码和javadoc的预编译jar文件,那么可以将它们安装到本地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安装插件用法。


或者,您可以使用maven源代码插件和maven javadoc插件自己使用它们的源代码和javadoc构建这些库,然后安装它们。

示例项目:库

<?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安装目标:

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>

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

在maven项目的根目录创建一个新文件夹,比如本地maven repo。

只需在pom.xml的<project>中添加一个本地repo:

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

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

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