这是我学习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
这是在将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?
谢谢!
答案 0 :(得分:188)
您可以创建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>
/groupId/artifactId/version/artifactId-verion.jar
详细阅读此博客文章
http://charlie.cu.cc/2012/06/how-add-external-libraries-maven/
答案 1 :(得分:58)
我认为您应该使用mvn install:install-file
使用库jar填充本地存储库,然后您应该将范围从system更改为compile。
如果你从maven开始我建议直接使用maven而不是IDE插件,因为它增加了额外的复杂性。
至于错误,您是否将所需的jar放在类路径上?如果您正在使用库中的类型,则还需要在运行时访问它。这与maven本身无关。
我不明白为什么要把库放到源代码控制中 - 这是源代码而不是二进制jar。
答案 2 :(得分:26)
使用&lt; scope&gt;可以很容易地实现这一点。嵌套在&lt; dependency&gt;内的元素元件。
例如:
<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
答案 3 :(得分:24)
Maven手册says to do这个:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
答案 4 :(得分:22)
更新我们刚刚安装了自己的Nexus服务器,更容易,更清洁。
在我们公司,我们有一些罐子,我们的罐子很常见,但没有托管在任何maven存储库中,我们也不想将它们放在本地存储中。
我们在Github上创建了一个非常简单的mvn(公共)仓库(但你可以在任何服务器或本地托管它):
注意 这只是管理一些很少的chaning 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或私人服务器)添加依赖关系
a。您需要知道的是,文件存储在@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>
享受
答案 5 :(得分:11)
不要使用systemPath。与人们在这里所说的相反,你可以将一个外部jar放在你签出的项目目录下的文件夹中,避免Maven发现它就像其他依赖项一样。以下是两个关键步骤:
这是相当简单的,你可以在这里找到一个分步示例: http://randomizedsort.blogspot.com/2011/10/configuring-maven-to-use-local-library.html
答案 6 :(得分:6)
Maven将非maven jar添加到maven项目的方法
Maven Project and non maven jars
在构建部分添加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>
References注意我是博客的所有者
答案 7 :(得分:4)
更改 systemPath 。
<dependency>
<groupId>stuff</groupId>
<artifactId>library</artifactId>
<version>1.0</version>
<systemPath>${project.basedir}/MyLibrary.jar</systemPath>
<scope>system</scope>
</dependency>
答案 8 :(得分:4)
如果您遇到同样的问题且使用的是弹簧启动 v1.4 + ,则可以这样做。
可以使用 includeSystemScope 将系统范围的依赖项添加到jar中。
e.g。
我正在使用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>
然后make includeSystemScope = true将jar包含到路径/ BOOT-INF / lib / **
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
并从资源中排除以避免重复包含,jar就是胖了〜
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.jar</exclude>
</excludes>
</resource>
</resources>
</build>
祝你好运!
答案 9 :(得分:3)
pom.xml将查看您的本地存储库以尝试查找与您的工件匹配的依赖项。 您也不应该使用系统范围或系统路径属性,这些属性通常保留给JDK中的内容而不是JRE
有关如何安装maven工件的信息,请参阅此question。
答案 10 :(得分:3)
请注意所有使用
的示例<repository>...</respository>
需要外部
<repositories>...</repositories>
附上标签。一些例子并不清楚。
答案 11 :(得分:2)
这里最好的解决方案是安装存储库:Nexus或Artifactory。如果给你一个放置这样东西的地方,进一步通过从外面缓存你的东西来加快速度。
如果你正在处理的事情是开源的,你也可以考虑将放入中心。
请参阅the guide。
答案 12 :(得分:1)
使用Eclipse Oxygen,您可以执行以下操作:
Maven将在安装项目时接受它们。
答案 13 :(得分:0)
我发现解决这个问题的最有效,最干净的方法是使用Github Packages
根据您的要求在GitHub上创建一个简单的空公共/私有存储库,无论您是否要公开托管外部jar。
在maven命令下面运行,以在上面创建的github存储库中部署外部jar
mvn deploy:deploy-file \
-DgroupId= your-group-id \
-DartifactId= your-artifact-id \
-Dversion= 1.0.0 -Dpackaging= jar -Dfile= path-to-file \
-DrepositoryId= id-to-map-on-server-section-of-settings.xml \
-Durl=https://maven.pkg.github.com/github-username/github-reponame-created-in-above-step
以上命令将在-Durl=
中提到的GitHub存储库中部署外部jar。
您可以在如何将依赖项作为GitHub软件包部署GitHub
Package Deployment Tutorial
之后,您可以使用以上步骤在maven groupId
中使用的artifactId
,version
和pom.xml
添加依赖项并运行mvn install
< / p>
Maven将从GitHub Packages注册表中获取外部jar的依赖关系,并在您的maven项目中提供。
为此,您还需要配置maven的settings.xml
以从GitHub软件包注册表中获取。