简体   繁体   English

将Maven本地.jar导入为lib

[英]Import Maven local .jar as a lib

I made a simple Maven project for my class. 我为我的班做了一个简单的Maven项目。 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 . 根据教师教程,由于某些服务器问题,我们无法将其上载到学校回购中,因此我们必须使用altDeploymentRepository将其存储在本地。 I have the following pom.xml : 我有以下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: 因此,在我的Maven项目的目录中,我有两个目录:

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). 在第二个文件中,深入到: sample_lib-mvn-repo\\com\\dpp\\sample_lib\\1.0-SNAPSHOT我有一个要导入的.jar文件(但不像传递路径一样仅使用.jar文件-我需要执行此“ Maven方式”,将其导入为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: 是的,您可以添加maven存储库并将其指向本地目录:

<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 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: 假设您的jar文件在此处sample_lib-mvn-repo \\ com \\ dpp \\ sample_lib \\ 1.0-SNAPSHOT.jar,则可以将其添加为依赖项:

<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. 仅运行mvn install将文件安装在本地存储库中。 The local repository, by default, is in your home directory, under .m2\\repository . 默认情况下,本地存储库位于主目录中的.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 . 使用上面的pom,在运行mvn install ,您将在.m2\\repository\\com\\dpp\\sample_lib\\1.0-SNAPSHOT具有jar(和其他文件)。

To import this subsequently in another project, you would create a dependency in that project's pom like: 要将其随后导入另一个项目中,可以在该项目的pom中创建一个依赖项,例如:

<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 . 您可以按照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). 但是一种快速而肮脏的解决方案是制作一个POM项目(无源代码)。

Inside the POM project you have the main and external projects. 在POM项目中,您具有主项目和外部项目。

You can simply make a dependency on the other project. 您可以简单地依赖另一个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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