简体   繁体   中英

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

demo1 project is a springboot project,demo project is a springboot project too. I write a Java Class Test.java in demo1:

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

and install it on local maven repository:

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

In demo project, l make a dependency of demo1 project,the content of pom.xml:

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

when I build demo project,something error happened:

mvn clean package

the error message is here:

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

Probably you're using spring-boot-maven-plugin to create a runnable spring boot application artifact. This plugin creates an artifact that indeed has an extension *.jar but in fact its not a jar at all: it has all the dependencies in BOOT-INF/lib , it uses special entries in manifest, and so on. Java (JVM) cannot read this jar, spring boot application uses a special class-loader to read this structure which is fairly unique to spring boot applications.

If you want to see what I'm talking about - just open up the JAR of spring boot application with WinRAR/WinZIP or whatever and you'll see it yourself.

That's why in general you can't really make a dependency on a spring boot application.

Now, having said that, there are workarounds:

  1. The chances are that you don't really need to be dependent on the whole application, but rather on some "common" code - in this case consider refactoring it to the separate module and make both applications dependent on this module.

  2. Use This link for workaround at the level of plugin configurations in maven, you'll end up with additional jars that might do what you want.

The first solution is to add manually the JAR into your local Maven repository by using the Maven goal install:install-file. The use of the plugin is very simple as below:

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

Note that we didn't specify groupId, artifactId, version and packaging of the JAR to install. Indeed, since the version 2.5 of Maven-install-plugin, these information can be taken from an optionally specified pomFile.

These information can also be given in command line:

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

Where:

<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

For example:

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

You can then add the dependency to your Maven project by adding those lines to your pom.xml file:

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

use the maven-install-plugin in your pom.xml which will install the jar during the Maven “initialize” phase. To do this, you must specify the location of the jar you want to install. The best way is to put the JAR in a folder created at the root of the project (in the same directory as the pom.xml file).

Let's consider that the jar is located under /lib/app.jar. Below the configuration of 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.

You may encounter an error while adding the previous lines, add the following plugin to your project to allow the lifecycle mapping:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM