我有一个专有的jar,我想作为依赖添加到我的pom .
但我不想将其添加到存储库 . 原因是我希望我的常用maven命令如 mvn compile 等开箱即用 . (没有要求开发人员自己将其添加到某个存储库) .
mvn compile
我希望jar在源代码控制中的第3方库中,并通过pom.xml文件的相对路径链接到它 .
可以这样做吗?怎么样?
我希望jar在源代码控制中位于第3方库中,并通过pom.xml文件中的相对路径链接到它 .
如果你真的想要这个(理解,如果你不能使用公司存储库),那么我的建议是使用"file repository"本地项目和 not use 一个 system 范围的依赖项 . 应避免使用 system 范围,这种依赖在许多情况下(例如在汇编中)不能很好地工作,它们会带来更多麻烦而不是好处 .
system
因此,相反,声明项目的本地存储库:
<repositories> <repository> <id>my-local-repo</id> <url>file://${basedir}/my-repo</url> </repository> </repositories>
使用带有localRepositoryPath参数的 install:install-file 在那里安装第三方库:
install:install-file
mvn install:install-file -Dfile = <path-to-file> -DgroupId = <myGroup> -DartifactId = <myArtifactId> -Dversion = <myVersion> -Dpackaging = <myPackaging> -DlocalRepositoryPath = <path>
Update: 在使用插件版本2.2时, install:install-file 似乎忽略了 localRepositoryPath . 但是,它适用于2.3版及更高版本的插件 . 因此,使用插件的完全限定名称来指定版本:
localRepositoryPath
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \ -Dfile=<path-to-file> -DgroupId=<myGroup> \ -DartifactId=<myArtifactId> -Dversion=<myVersion> \ -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
maven-install-plugin documentation
最后,声明它像任何其他依赖项(但没有 system 范围):
<dependency> <groupId>your.group.id</groupId> <artifactId>3rdparty</artifactId> <version>X.Y.Z</version> </dependency>
这是恕我直言,比使用 system 范围更好的解决方案,因为您的依赖将被视为一个好公民(例如,它将被包含在一个程序集中,等等) .
现在,我必须提到在公司环境中处理这种情况的“正确方法”(可能不是这里的情况)将是使用公司存储库 .
使用 system 范围 . ${basedir} 是你的pom目录 .
${basedir}
<dependency> <artifactId>..</artifactId> <groupId>..</groupId> <scope>system</scope> <systemPath>${basedir}/lib/dependency.jar</systemPath> </dependency>
但是,建议您将jar安装在存储库中,而不是将其提交给SCM - 毕竟这是maven试图消除的内容 .
这是我之前在Can I add jars to maven 2 build classpath without installing them?回答的另一种方法
当使用多模块构建时,这将达到极限,特别是如果在父项之外的子项目中引用下载的JAR . 这也通过在构建过程中创建POM和SHA1文件来减少设置工作 . 它还允许文件驻留在项目中的任何位置,而无需修复名称或遵循maven存储库结构 .
这使用maven-install-plugin . 为此,您需要设置一个多模块项目,并拥有一个代表构建的新项目,以便将文件安装到本地存储库中,并确保首先是一个 .
你的多模块项目pom.xml看起来像这样:
<packaging>pom</packaging> <modules> <!-- The repository module must be first in order to ensure that the local repository is populated --> <module>repository</module> <module>... other modules ...</module> </modules>
然后,repository / pom.xml文件将包含用于加载属于项目一部分的JAR的定义 . 以下是pom.xml文件的一些片段 .
<artifactId>repository</artifactId> <packaging>pom</packaging>
pom包装阻止它进行任何测试或编译或生成任何jar文件 . pom.xml的内容位于使用maven-install-plugin的构建部分 .
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <executions> <execution> <id>com.ibm.db2:db2jcc</id> <phase>verify</phase> <goals> <goal>install-file</goal> </goals> <configuration> <groupId>com.ibm.db2</groupId> <artifactId>db2jcc</artifactId> <version>9.0.0</version> <packaging>jar</packaging> <file>${basedir}/src/jars/db2jcc.jar</file> <createChecksum>true</createChecksum> <generatePom>true</generatePom> </configuration> </execution> <execution>...</execution> </executions> </plugin> </plugins> </build>
要安装多个文件,只需添加更多执行 .
我之前已经written about a pattern这样做了 .
它与Pascal提出的解决方案非常相似,尽管它将所有这些依赖项移动到专用存储库模块中,这样如果它是一个多模块构建,则不必在使用依赖项的任何地方重复它 .
这对我有用:让我说我有这种依赖
<dependency> <groupId>com.company.app</groupId> <artifactId>my-library</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/my-library.jar</systemPath> </dependency>
然后,像这样手动为系统依赖项添加类路径
<Class-Path>libs/my-library-1.0.jar</Class-Path>
完整配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifestEntries> <Build-Jdk>${jdk.version}</Build-Jdk> <Implementation-Title>${project.name}</Implementation-Title> <Implementation-Version>${project.version}</Implementation-Version> <Specification-Title>${project.name} Library</Specification-Title> <Specification-Version>${project.version}</Specification-Version> <Class-Path>libs/my-library-1.0.jar</Class-Path> </manifestEntries> <manifest> <addClasspath>true</addClasspath> <mainClass>com.company.app.MainClass</mainClass> <classpathPrefix>libs/</classpathPrefix> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.5.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/libs/</outputDirectory> </configuration> </execution> </executions> </plugin>
我们切换到gradle,这在gradle中工作得更好;) . 我们只是指定一个文件夹,我们可以将jar放入这样的临时情况 . 我们仍然将大多数 jar 定义为典型的依赖管理部分(即与maven相同) . 这只是我们定义的一个依赖项 .
所以基本上现在我们可以把我们想要的任何jar放到我们的lib目录中进行临时测试,如果它不是某个maven存储库的话 .
基本上,将其添加到pom.xml:
... <repositories> <repository> <id>lib_id</id> <url>file://${project.basedir}/lib</url> </repository> </repositories> ... <dependencies> ... <dependency> <groupId>com.mylibrary</groupId> <artifactId>mylibraryname</artifactId> <version>1.0.0</version> </dependency> ... </dependencies>
Pascal发布的解决方案的一小部分内容
当我沿着这条路线行进时,我在安装ojdbc jar时遇到了maven错误 .
[INFO] --- maven-install-plugin:2.5.1:install-file (default-cli) @ validator --- [INFO] pom.xml not found in ojdbc14.jar
添加-DpomFile后,问题得以解决 .
$ mvn install:install-file -Dfile=./lib/ojdbc14.jar -DgroupId=ojdbc \ -DartifactId=ojdbc -Dversion=14 -Dpackaging=jar -DlocalRepositoryPath=./repo \ -DpomFile=~/.m2/repository/ojdbc/ojdbc/14/ojdbc-14.pom
您可以使用eclipse生成可运行的Jar:Export / Runable Jar文件
9 回答
如果你真的想要这个(理解,如果你不能使用公司存储库),那么我的建议是使用"file repository"本地项目和 not use 一个
system
范围的依赖项 . 应避免使用system
范围,这种依赖在许多情况下(例如在汇编中)不能很好地工作,它们会带来更多麻烦而不是好处 .因此,相反,声明项目的本地存储库:
使用带有localRepositoryPath参数的
install:install-file
在那里安装第三方库:mvn install:install-file -Dfile = <path-to-file> -DgroupId = <myGroup>
-DartifactId = <myArtifactId> -Dversion = <myVersion>
-Dpackaging = <myPackaging> -DlocalRepositoryPath = <path>
Update: 在使用插件版本2.2时,
install:install-file
似乎忽略了localRepositoryPath
. 但是,它适用于2.3版及更高版本的插件 . 因此,使用插件的完全限定名称来指定版本:maven-install-plugin documentation
最后,声明它像任何其他依赖项(但没有
system
范围):这是恕我直言,比使用
system
范围更好的解决方案,因为您的依赖将被视为一个好公民(例如,它将被包含在一个程序集中,等等) .现在,我必须提到在公司环境中处理这种情况的“正确方法”(可能不是这里的情况)将是使用公司存储库 .
使用
system
范围 .${basedir}
是你的pom目录 .但是,建议您将jar安装在存储库中,而不是将其提交给SCM - 毕竟这是maven试图消除的内容 .
这是我之前在Can I add jars to maven 2 build classpath without installing them?回答的另一种方法
当使用多模块构建时,这将达到极限,特别是如果在父项之外的子项目中引用下载的JAR . 这也通过在构建过程中创建POM和SHA1文件来减少设置工作 . 它还允许文件驻留在项目中的任何位置,而无需修复名称或遵循maven存储库结构 .
这使用maven-install-plugin . 为此,您需要设置一个多模块项目,并拥有一个代表构建的新项目,以便将文件安装到本地存储库中,并确保首先是一个 .
你的多模块项目pom.xml看起来像这样:
然后,repository / pom.xml文件将包含用于加载属于项目一部分的JAR的定义 . 以下是pom.xml文件的一些片段 .
pom包装阻止它进行任何测试或编译或生成任何jar文件 . pom.xml的内容位于使用maven-install-plugin的构建部分 .
要安装多个文件,只需添加更多执行 .
我之前已经written about a pattern这样做了 .
它与Pascal提出的解决方案非常相似,尽管它将所有这些依赖项移动到专用存储库模块中,这样如果它是一个多模块构建,则不必在使用依赖项的任何地方重复它 .
这对我有用:让我说我有这种依赖
然后,像这样手动为系统依赖项添加类路径
完整配置:
我们切换到gradle,这在gradle中工作得更好;) . 我们只是指定一个文件夹,我们可以将jar放入这样的临时情况 . 我们仍然将大多数 jar 定义为典型的依赖管理部分(即与maven相同) . 这只是我们定义的一个依赖项 .
所以基本上现在我们可以把我们想要的任何jar放到我们的lib目录中进行临时测试,如果它不是某个maven存储库的话 .
基本上,将其添加到pom.xml:
Pascal发布的解决方案的一小部分内容
当我沿着这条路线行进时,我在安装ojdbc jar时遇到了maven错误 .
添加-DpomFile后,问题得以解决 .
您可以使用eclipse生成可运行的Jar:Export / Runable Jar文件