简体   繁体   中英

Import Maven local .jar as a lib

I made a simple Maven project for my class. According to the teachers tutorial we cannot upload it to our school repo due to some server issues, so we have to store it locally using altDeploymentRepository . I have the following pom.xml :

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

    <groupId>com.dpp</groupId>
    <artifactId>simple_lib</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <altDeploymentRepository>internal.repo::default::file://${project.basedir}/../${project.name}-mvn-repo</altDeploymentRepository>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

So in the directory with my Maven project I have two directories:

sample_lib
sample_lib-mvn-repo

In the second one, deep down in : sample_lib-mvn-repo\\com\\dpp\\sample_lib\\1.0-SNAPSHOT I have a .jar file which I want to import (but not using just .jar file like passing the path to it - I need to do this "Maven way", import it as Maven lib). Can I do it if the file is not stored on any remote repository, but on my hard drive?

Yes, you can add maven repository and point it to a local directory:

<repository>
   <id>local</id>
   <name>local</name>
   <url>file:${user.dir}/sample_lib-mvn-repo</url>
</repository>

https://maven.apache.org/guides/introduction/introduction-to-repositories.html

Given that your jar file is here sample_lib-mvn-repo\\com\\dpp\\sample_lib\\1.0-SNAPSHOT.jar, you then can add it as a dependency:

<dependency>
   <groupId>com.dpp</groupId>
   <artifactId>sample_lib</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

Running simply mvn install will install the file in your local repository. The local repository, by default, is in your home directory, under .m2\\repository .

Using your pom above, after running mvn install , you would have jar (and some other files) in .m2\\repository\\com\\dpp\\sample_lib\\1.0-SNAPSHOT .

To import this subsequently in another project, you would create a dependency in that project's pom like:

<dependency>
   <groupId>com.dpp</groupId>
   <artifactId>simple_lib</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

This all takes place only on your machine, and will not use any remote repository.

Now you have a local simulation of a repository.

You can import it using the repository tag as described in https://maven.apache.org/pom.html#Repositories . To specify a file make it a file url like file:

It's not exactly what you're asking. But a quick and dirty solution is to make a POM project (no source code).

Inside the POM project you have the main and external projects.

You can simply make a dependency on the other project.

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