簡體   English   中英

為什么我不能從本地存儲庫導入 maven jar?

[英]why i can't import maven jar from local repository?

demo1 項目是一個 springboot 項目,demo 項目也是一個 springboot 項目。 我在 demo1 中編寫了一個 Java 類 Test.java:

package com.example.demo1;
public class Test {
    public void test() {
        System.out.println("11111111111111");
    }
}

並將其安裝在本地 Maven 存儲庫中:

mvn install:install-file "-DgroupId=com.example" "-DartifactId=demo1" "-Dversion=0.0.1-SNAPSHOT" "-Dpackaging=jar" "-Dfile=target/demo1-0.0.1-SNAPSHOT.jar"

在demo工程中,我對demo1工程做一個依賴,pom.xml的內容:

<dependency>
        <groupId>com.example</groupId>
        <artifactId>demo1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
</dependency>

當我構建演示項目時,發生了一些錯誤:

mvn clean package

錯誤信息在這里:

com.example.demo1 does not exists
Test cannot find symbol!

可能您正在使用spring-boot-maven-plugin創建一個可運行的 Spring Boot 應用程序工件。 該插件創建了一個確實具有擴展名 *.jar 的工件,但實際上它根本不是一個 jar:它在BOOT-INF/lib具有所有依賴項,它在清單中使用特殊條目,等等。 Java (JVM) 無法讀取這個 jar,spring boot 應用程序使用一個特殊的類加載器來讀取這個結構,這對於 spring boot 應用程序來說是相當獨特的。

如果你想看看我在說什么 - 只需用 WinRAR/WinZIP 或其他任何東西打開 Spring Boot 應用程序的 JAR,你就會自己看到它。

這就是為什么通常您不能真正依賴 spring boot 應用程序的原因。

現在,話雖如此,有解決方法:

  1. 很有可能你並不真的需要依賴整個應用程序,而是依賴一些“公共”代碼——在這種情況下,考慮將它重構為單獨的模塊,並使兩個應用程序都依賴於這個模塊。

  2. 使用此鏈接在 maven 中的插件配置級別上解決方法,您最終會得到額外的 jars,它們可能會執行您想要的操作。

第一個解決方案是使用 Maven 目標 install:install-file 將 JAR 手動添加到本地 Maven 存儲庫中。 該插件的使用非常簡單,如下所示:

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

請注意,我們沒有指定要安裝的 JAR 的 groupId、artifactId、版本和包裝。 實際上,從 Maven-install-plugin 的 2.5 版開始,這些信息可以從可選的指定 pomFile 中獲取。

這些信息也可以在命令行中給出:

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

在哪里:

<path-to-file>: Path to the JAR to install
<group-id>: Group id of the JAR to install
<artifact-id>: Artifact id of the JAR to install
<version>:  Version of the JAR

例如:

mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0

然后,您可以通過將這些行添加到您的 pom.xml 文件來將依賴項添加到您的 Maven 項目:

<dependency>
    <groupId>com.roufid.tutorials</groupId>
    <artifactId>example-app</artifactId>
    <version>1.0</version>
</dependency>

在你的 pom.xml 中使用 maven-install-plugin,它將在 Maven “初始化”階段安裝 jar。 為此,您必須指定要安裝的 jar 的位置。 最好的方法是將 JAR 放在項目根目錄下創建的文件夾中(與 pom.xml 文件在同一目錄中)。

讓我們考慮 jar 位於 /lib/app.jar 下。 在 maven-install-plugin 的配置下面:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.roufid.tutorials</groupId>
                <artifactId>example-app</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/app.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>


`${basedir}` represents the directory containing pom.xml.

添加前幾行時可能會遇到錯誤,請將以下插件添加到您的項目中以允許生命周期映射:

<pluginManagement>
    <plugins>
        <!--This plugin's configuration is used to store Eclipse m2e settings only. 
            It has no influence on the Maven build itself. -->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>aspectj-maven-plugin</artifactId>
                                <versionRange>[1.0,)</versionRange>
                                <goals>
                                    <goal>test-compile</goal>
                                    <goal>compile</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>
                                    org.apache.maven.plugins
                                </groupId>
                                <artifactId>
                                    maven-install-plugin
                                </artifactId>
                                <versionRange>
                                    [2.5,)
                                </versionRange>
                                <goals>
                                    <goal>install-file</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnIncremental>false</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM