Maven:将自定义外部JAR链接到我的项目的最佳方法是什么?

| 这是我学习Maven的前两天,但我仍然在基础方面苦苦挣扎。我有一个外部.jar文件(在公共存储库中不可用),我需要在我的项目中引用该文件,并且我试图弄清楚我的最佳选择是什么。 这是一个小型项目,没有用于库的中央存储库,因此它必须是本地存储库(以某种方式添加到源代码管理中,不知道它是否应该以这种方式工作?)或。 jar需要存储在任何正式存储库之外的磁盘上。 1)考虑到我希望项目和库都在源代码管理中,使用maven将.jar文件添加到项目引用中的最佳选择是什么? 2)我似乎仍然无法让Eclipse看到依赖关系。我手动将其添加到pom的部分,它在m2eclipse的Dependencies列表中很好地显示。 mvn编译和mvn软件包都成功,但是运行程序会导致:
Exception in thread \"main\" java.lang.Error: Unresolved compilation problems:
        LibraryStuff cannot be resolved to a type
这是将POM编辑为:
<dependency>
  <groupId>stuff</groupId>
  <artifactId>library</artifactId>
  <version>1.0</version>
  <systemPath>${lib.location}/MyLibrary.jar</systemPath>
  <scope>system</scope>
</dependency>
我是否应该执行mvn install:install-file甚至认为我已经按照上面的方法编辑了pom.xml? 谢谢!     
已邀请:
我认为您应该使用ѭ2来用库jar填充本地存储库,然后将范围从系统更改为编译。 如果您从maven开始,我建议直接使用maven而不是IDE插件,因为它会增加一层额外的复杂性。 至于错误,您是否将所需的jar放在类路径上?如果您正在使用库中的类型,则还需要在运行时中也可以访问它。这与Maven本身无关。 我不明白为什么要将库放到源代码管理中-它是针对源代码而不是二进制jar。     
您可以创建一个In Project存储库,因此您不必每次在新计算机上工作时都花3英镑。
<repository>
    <id>in-project</id>
    <name>In Project Repo</name>
    <url>file://${project.basedir}/libs</url>
</repository>

<dependency>
    <groupId>dropbox</groupId>
    <artifactId>dropbox-sdk</artifactId>
    <version>1.3.1</version>
</dependency>
/groupId/artifactId/version/artifactId-verion.jar 详细阅读此博客文章 http://charlie.cu.cc/2012/06/how-add-external-libraries-maven/     
这可以通过使用嵌套在元素内的元素轻松实现。 例如:
 <dependencies>
   <dependency>
     <groupId>ldapjdk</groupId>
     <artifactId>ldapjdk</artifactId>
     <scope>system</scope>
     <version>1.0</version>
     <systemPath>${basedir}\\src\\lib\\ldapjdk.jar</systemPath>
   </dependency>
 </dependencies>
参考:http://www.tutorialspoint.com/maven/maven_external_dependencies.htm     
Maven手册说要这样做:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
    
更新从那以后,我们才安装了自己的Nexus服务器,更加轻松,整洁。 在我们公司,我们有一些罐子,有些罐子很常见,但是没有存放在任何Maven仓库中,我们也不希望将它们放在本地存储中。 我们在Github上创建了一个非常简单的mvn(公共)仓库(但您可以将其托管在任何服务器上或本地): 请注意,这仅是管理一些很少更改的jar文件的理想选择 在GitHub上创建仓库:
https://github.com/<user_name>/mvn-repo/
在pom.xml中添加存储库 (请注意,完整路径原始文件将与回购名称有所不同)
<repository>
    <id>project-common</id>
    <name>Project Common</name>
    <url>https://github.com/<user_name>/mvn-repo/raw/master/</url>
</repository>
向主机(Github或专用服务器)添加依赖项 一种。您需要知道的是文件以@glitch提到的模式存储  
/groupId/artifactId/version/artifactId-version.jar
b。在主机上创建文件夹以匹配此模式。   即如果您有一个名为
service-sdk-0.0.1.jar
的jar文件,请创建文件夹
service-sdk/service-sdk/0.0.1/
,然后将jar文件
service-sdk-0.0.1.jar
放入其中。  C。通过尝试从浏览器下载jar进行测试(在本例中为
https://github.com/<user_name>/mvn-repo/raw/master/service-sdk/service-sdk/0.0.1/service-sdk-0.0.1.jar
) 向您的pom.xml文件添加依赖项:
<dependency>
    <groupId>service-sdk</groupId>
    <artifactId>service-sdk</artifactId>
    <version>0.0.1</version>
</dependency>
请享用     
不要使用systemPath。与人们在这里所说的相反,您可以将一个外部jar放在检出的项目目录下的文件夹中,并让Maven像其他依赖项一样找到它。这是两个关键步骤: 将\“ mvn install:install-file \”与-DlocalRepositoryPath一起使用。 配置存储库以指向您的POM中的路径。 这非常简单,您可以在此处找到分步示例: http://randomizedsort.blogspot.com/2011/10/configuring-maven-to-use-local-library.html     
如果您遇到相同的问题,并且正在使用spring-boot v1.4 +,则可以采用这种方式。 您可以使用includeSystemScope将系统范围的依赖项添加到jar中。 例如 我在项目中使用了oracle驱动程序。
<dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.3.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/extra-jars/ojdbc14-10.2.0.3.0.jar</systemPath>
    </dependency>
然后使includeSystemScope = true将jar包含在路径/ BOOT-INF / lib / **中
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>
并从资源中排除以避免重复包含,那么罐子就足够胖了〜
<build>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.jar</exclude>
            </excludes>
        </resource>
    </resources>
</build>
祝好运!     
将非Maven Jar添加到Maven项目的Maven方法 Maven项目和非Maven罐子 在构建部分中添加Maven安装插件
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>${version.maven-install-plugin}</version>
        <executions>

            <execution>
                <id>install-external-non-maven1-jar</id>
                <phase>clean</phase>
                <configuration>
                    <repositoryLayout>default</repositoryLayout>
                    <groupId>jar1.group</groupId>
                    <artifactId>non-maven1</artifactId>
                    <version>${version.non-maven1}</version>
                    <file>${project.basedir}/libs/non-maven1.jar</file>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                </configuration>
                <goals>
                    <goal>install-file</goal>
                </goals>
            </execution>
            <execution>
                <id>install-external-non-maven2-jar</id>
                <phase>clean</phase>
                <configuration>
                    <repositoryLayout>default</repositoryLayout>
                    <groupId>jar2.group</groupId>
                    <artifactId>non-maven2</artifactId>
                    <version>${version.non-maven2}</version>
                    <file>${project.basedir}/libs/non-maven2.jar</file>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                </configuration>
                <goals>
                    <goal>install-file</goal>
                </goals>
            </execution>
            <execution>
                <id>install-external-non-maven3-jar</id>
                <phase>clean</phase>
                <configuration>
                    <repositoryLayout>default</repositoryLayout>
                    <groupId>jar3.group</groupId>
                    <artifactId>non-maven3</artifactId>
                    <version>${version.non-maven3}</version>
                    <file>${project.basedir}/libs/non-maven3.jar</file>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                </configuration>
                <goals>
                    <goal>install-file</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
添加依赖项
<dependencies>
    <dependency>
        <groupId>jar1.group</groupId>
        <artifactId>non-maven1</artifactId>
        <version>${version.non-maven1}</version>
    </dependency>
    <dependency>
        <groupId>jar2.group</groupId>
        <artifactId>non-maven2</artifactId>
        <version>${version.non-maven2}</version>
    </dependency>
    <dependency>
        <groupId>jar3.group</groupId>
        <artifactId>non-maven3</artifactId>
        <version>${version.non-maven3}</version>
    </dependency>
</dependencies>
参考注解我是博客的所有者     
更改您的systemPath。
<dependency>
  <groupId>stuff</groupId>
  <artifactId>library</artifactId>
  <version>1.0</version>
  <systemPath>${project.basedir}/MyLibrary.jar</systemPath>
  <scope>system</scope>
</dependency>
    
pom.xml将查看您的本地存储库,以尝试查找与您的工件匹配的依赖项。 另外,您不应该真正使用系统范围或systemPath属性,这些属性通常是为JDK中的内容而不是JRE中的内容保留的 有关如何安装Maven工件的信息,请参见此问题。     
请注意,所有使用的示例
<repository>...</respository> 
需要外部
<repositories>...</repositories> 
封闭标签。从某些示例尚不清楚。     
最好的解决方案是安装存储库:Nexus或Artifactory。 If为您提供了放置此类内容的地方,并进一步通过从外部缓存您的内容来加快处理速度。 如果您要处理的东西是开源的,那么您也可以考虑将其放入中心。 请参阅指南。     
使用Eclipse Oxygen,您可以执行以下操作: 将您的库放在WEB-INF / lib中 项目->配置构建路径->添加库-> Web应用程序库 在安装项目时,Maven将使用它们。     
如果外部jar仅由Maven项目创建,则可以在系统上复制整个项目并运行
mvn install
在项目目录中。这会将jar添加到本地maven存储库的.m2目录中。 现在您可以添加
<dependency>
     <groupId>copy-from-the=maven-pom-of-existing-project</groupId>
     <artifactId>copy-from-the=maven-pom-of-existing-project</artifactId>
     <version>copy-from-the=maven-pom-of-existing-project</version>
</dependency>
这将确保您
mvn exec:java 
作品。如果您在这里使用建议
<scope>system</scope>
然后,您将必须在通过命令行执行使用时分别添加类。 您可以通过此处描述的以下命令添加外部罐子
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \\
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
    

要回复问题请先登录注册