Alexei
Alexei

Reputation: 15726

Add local external jar - should not point at files within the project directory

In my maven project I use custom external my-custom-external jar like this:

pom.xml

<dependencies>
    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>my-custom-external</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/libs/my-custom-external-1.0-SNAPSHOT.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>

I success build project my mvn verify but in console I has warning:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.myproject:prj_1:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.myproject:my-custom-extneral:jar should not point at files within the project directory, ${project.basedir}/libs/my-custom-extneral-1.0-SNAPSHOT.jar will be unresolvable by dependent projects @ line 62, column 25
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 

Upvotes: 24

Views: 56604

Answers (3)

waitingmyself
waitingmyself

Reputation: 624

You can use ${pom.basedir} to replace ${project.basedir}.

Upvotes: 54

GaidinD
GaidinD

Reputation: 362

I reached here because I faced a similar problem. And based on this answer by Prasann, I think I have a viable solution for this that will resolve most issues. Please feel free to correct me if I'm wrong.

Credit for the CheckSum related steps : Casey Jordan

  1. During development, install the local external jar.

    To do this, run the following command

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

    Fields inside < and > (including the < and >) need to be replaced with actual values.

  2. Rename the maven-metadata-local files that are generated to remove the local part.

  3. Copy the entire folder for this jar file (I'll call this as installed folder) from your local Maven repository to your project's lib folder. You need to maintain the folder structure from the root of the Maven repository.

    For example, if the absolute path for your installed folder is C:\Users\so_user\.m2\repository\my_group_id\my_artifact_id\, then your project lib folder should also look like ${project.basedir}\lib\my_group_id\my_artifact_id\.

    Also note that,

    • my_group_id in this point is the same as <group-id> in point #2. Just replace any periods (.) with a slash (\).

    • my_artifact_id in this point is the same as <artifact-id> in point #2. No changes required for this.

  4. In your pom.xml file, create a local repository by adding the following lines:

    <repositories>
       <repository>
          <id>any_name</id>
          <name>Any Name</name>
          <url>file://[path-to-lib-folder]</url>
       </repository>
    </repositories>
    
    • You can choose any name for id and name.
    • Replace [path-to-lib-folder] with the actual folder path. You can use ${project.basedir} or ${pom.basedir} here. No warning will pop up.
  5. Add a dependency to the external jar as normal.

    <dependencies>
       <dependency>
          <groupId>[group-id]</groupId>
          <artifactId>[artifact-id]</artifactId>
          <version>[version]</version>
       </dependency>
    </dependencies>
    

    Replace [group-id], [artifact-id] and [version] with the actual values from point #1.

Because you have provided the entire installed folder in your project lib folder, you do not need to run mvn install:install-file again when you transfer this project to a new system.

Upvotes: 8

Prasann
Prasann

Reputation: 1301

It is just a warning and you are okay to do this as long as it is used only in this project as it won't be visible for the dependent projects.

You can consider below two options for doing it right

  1. Install the 3rd party jar to your local repository and reference it just like any other dependency. Refer to this link for more details on installing the library
  2. If you want to refer it from your project, then add your project libs folder as a repository as shown below

    <repositories>
        <repository>
            <id>localrepository</id>
            <url>file://${project.basedir}/libs</url>
        </repository>
        .
        .
        .
        .
    </repositories>
    

Hope this helps

Upvotes: 9

Related Questions