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

Ale*_*lin 140 java maven-2 m2eclipse maven

这是我学习Maven的前几天,我仍然在努力学习基础知识.我有一个外部.jar文件(在公共存储库中不可用),我需要在我的项目中引用它,我正在试图找出我最好的选择.

这是一个没有库的中央存储库的小型项目,所以它必须是一个本地存储库(以某种方式添加到源代码控制,不知道它是否应该以这种方式工作?)或者.jar需要存储在任何正式存储库之外的磁盘.

1)我最好的选择是将.jar文件添加到项目的maven引用中,因为我希望项目和库都在源代码控制中?

2)我似乎还无法让Eclipse看到依赖.我手动将它添加到pom的部分,它在m2eclipse的Dependencies列表中显示正常.mvn compile和mvn包都成功,但运行程序导致:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
        LibraryStuff cannot be resolved to a type
Run Code Online (Sandbox Code Playgroud)

这是在将POM编辑为:

<dependency>
  <groupId>stuff</groupId>
  <artifactId>library</artifactId>
  <version>1.0</version>
  <systemPath>${lib.location}/MyLibrary.jar</systemPath>
  <scope>system</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我是否应该执行mvn install:install-file,即使我已经编辑了如上所述的pom.xml?

谢谢!

Cha*_* Wu 194

您可以创建In Project Repository,这样您就不必run mvn install:install-file每次都在使用新计算机

<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>
Run Code Online (Sandbox Code Playgroud)

/groupId/artifactId/version/artifactId-verion.jar

详细阅读此博客文章

http://charlie.cu.cc/2012/06/how-add-external-libraries-maven/

  • 使用上面的解决方案在执行"mvn clean package"时显示警告 - 缺少项目<dependency_name>的POM,没有可用的依赖项信息. (2认同)
  • 或者,您可以直接添加本地依赖项,如http://stackoverflow.com/a/22300875/640378 (2认同)
  • 我不得不使用file:/// $ {project.basedir}/libs(3个转发斜杠)而不是file:// $ {project.basedir}/libs (2认同)
  • 如果安装的jar不是Maven编译的jar,则还需要添加一个新的pom文件来定义元数据。为了避免所有这些手动麻烦,我建议使用`mvn install:install-file`,然后将整个目录结构从本地存储库复制到项目内存储库。 (2认同)
  • 请注意,在创建文件夹结构`/groupId/artifactId/version/artifactId-verion.jar`时,不要犯使用包名作为`groupId`的错误;相反,它应该被分成一个文件夹树。例如,对于 groupId `org.json`,路径应该类似于 `org/json/2.0/json-2.0.jar`,而不是 `org.json/2.0/json-2.0.jar` (2认同)

sta*_*ker 61

我认为您应该使用mvn install:install-file库jar填充本地存储库,然后您应该将范围从system更改为compile.

如果你从maven开始我建议直接使用maven而不是IDE插件,因为它增加了额外的复杂性.

至于错误,您是否将所需的jar放在类路径上?如果您正在使用库中的类型,则还需要在运行时访问它.这与maven本身无关.

我不明白为什么你想把库放到源代码控制 - 它是源代码而不是二进制jar.

  • 有关`mvn install :: install-file`的更多信息,请访问:http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/ (33认同)
  • 在本地存储库中使用`mvn install :: install-file`意味着克隆源代码的任何人都必须执行此手动步骤.否则,构建将打破开箱即用 (6认同)

Jig*_*hel 30

这可以通过使用嵌套在<dependency>元素内的<scope>元素轻松实现.

例如:

 <dependencies>
   <dependency>
     <groupId>ldapjdk</groupId>
     <artifactId>ldapjdk</artifactId>
     <scope>system</scope>
     <version>1.0</version>
     <systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
   </dependency>
 </dependencies>
Run Code Online (Sandbox Code Playgroud)

参考:http://www.tutorialspoint.com/maven/maven_external_dependencies.htm

  • 最干净、最简单。 (2认同)

Vla*_*sny 28

Maven手册说要这样做:

mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
Run Code Online (Sandbox Code Playgroud)

  • 此命令将lib安装到您的maven仓库中.这样做的缺点是,如果您尝试在不同的计算机上处​​理项目,则必须再次运行它. (4认同)

Ken*_*son 24

更新我们刚刚安装了自己的Nexus服务器,更加简单和清洁.

在我们公司,我们有一些罐子,我们一些罐子很常见,但没有托管在任何maven存储库中,我们也不想将它们放在本地存储中.我们在Github上创建了一个非常简单的mvn(公共)repo(但你可以在任何服务器或本地托管它):
请注意,这只是管理一些很少的chaning jar文件的理想选择

  1. 在GitHub上创建repo:
    https://github.com/<user_name>/mvn-repo/

  2. 在pom.xml中添加存储库
    (请注意,完整路径原始文件将与repo名称略有不同)

    <repository>
        <id>project-common</id>
        <name>Project Common</name>
        <url>https://github.com/<user_name>/mvn-repo/raw/master/</url>
    </repository>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 向主机(Github或私有服务器)添加依赖关系
    a.您需要知道的是,文件存储在@glitch
    /groupId/artifactId/version/artifactId-version.jar
    b提到的模式中.在主机上创建与此模式匹配的文件夹.
    即如果您有一个名为的jar文件service-sdk-0.0.1.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

  4. 添加依赖项到您的pom.xml文件:

    <dependency>
        <groupId>service-sdk</groupId>
        <artifactId>service-sdk</artifactId>
        <version>0.0.1</version>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 请享用


小智 11

不要使用systemPath.与人们在这里所说的相反,你可以将一个外部jar放在你签出的项目目录下的文件夹中,避免Maven发现它就像其他依赖项一样.以下是两个关键步骤:

  1. 将"mvn install:install-file"与-DlocalRepositoryPath一起使用.
  2. 配置存储库以指向POM中的该路径.

它非常简单,你可以在这里找到一个分步示例:http: //randomizedsort.blogspot.com/2011/10/configuring-maven-to-use-local-library.html


cra*_*eem 7

Maven方式将非maven jar添加到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>
Run Code Online (Sandbox Code Playgroud)

添加依赖项

<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>
Run Code Online (Sandbox Code Playgroud)

参考资料注意我是博客的所有者


che*_*ndu 6

如果您遇到同样的问题而且使用的是spring-boot v1.4 +,则可以这样做.

有一个includeSystemScope,您可以使用到系统范围的依赖性添加到罐子.

例如

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

然后使includeSystemScope = true将jar包含到路径/ BOOT-INF/lib/**中

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

并且从资源中排除以避免重复包含,jar是胖的〜

<build>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.jar</exclude>
            </excludes>
        </resource>
    </resources>
</build>
Run Code Online (Sandbox Code Playgroud)

祝好运!


Aun*_*ein 5

更改您的系统路径

<dependency>
  <groupId>stuff</groupId>
  <artifactId>library</artifactId>
  <version>1.0</version>
  <systemPath>${project.basedir}/MyLibrary.jar</systemPath>
  <scope>system</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)