![](/img/trans.png)
[英]In maven how to add a local maven project as a dependency with source code
[英]How can I add source code to my dependency libraries in Maven?
例如,我已经将我的依赖项包含在junit-addons中:junit-addons。 但是在maven存储库中没有任何源代码。 而且我知道它存在(我已经下载了它)。 如何修改依赖项以便使用本地项目中的库而不是maven存储库(我将省略来自存储库的junit-addons并使用本地JAR及其源代码)。
注意:我使用m2eclipse。
我已经非常直接地解决了这个问题:
我已经复制到文件夹${user.home}/.m2/repository/{group-name}/{artifactId}/{version}/
MAVEN标准之后的源文件: {artifactId}-{version}-sources.jar
它充当魅力! Eclipse检查本地存储库并查找源。
不过,我不知道这是否是MAVEN方式。
如何修改依赖项以便使用本地项目中的库而不是maven存储库
你不能。 如果源JAR在中央存储库中不可用,只需将文件系统中的源放在文件夹,JAR或zip文件夹中(您可以按照Maven的约定install:install-file
在本地存储库中install:install-file
源文件存档)和设置Eclipse使用它们(在Package Explorer中 右键单击 Maven Dependencies下的JAR,选择Java Source Attachment并设置Location路径 )。
我使用Artifactory存储库的免费版本。 我创建了一个jar文件{artifactId} - {version} -sources.jar,并将其上传到与二进制jar文件相同的group-id。 然后在我的pom中我添加了依赖:
<dependency>
<groupId>mygroupid</groupId>
<artifactId>artifact</artifactId>
<version>1.0</version>
<classifier>source</classifier>
</dependency>
在maven构建阶段,源jar被下载到我的本地存储库。 我使用netbeans 7.0,它会自动为我管理一切。 例如,右键单击方法并选择go toSource正确地将我带到jar中的源代码。
您可以使用install-file mojo将工件本地安装到本地maven存储库中。 如果您想与其他人(比如您的团队或其他工作站)共享此工件,您可以使用自己的存储库管理器(例如Nexus )并将其配置为任何存储库(例如中央存储库)的镜像。 Nexus将从中心获取(和缓存)工件。 此外,您可以将任何工件(如junit-addons源)上传到您的nexus安装。
要配置镜像,您必须编辑${user.home}/.m2/settings.xml
<settings>
<!-- SNIP -->
<mirrors>
<mirror>
<id>nexus-releases</id>
<mirrorOf>*</mirrorOf>
<!-- replace nexus.example.com with the location of your nexus installation -->
<url>http://nexus.example.com/releases</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
<id>nexus</id>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</profile>
</profiles>
</settings>
将其下载到本地存储库后,您可以复制它。 给它一个新的artifactId(例如libraryName-myVersion)并在pom中添加依赖项。 确保在pom中更改文件夹名称,jar名称,pom名称和artifactId本身。 将所有内容存储在本地存储库中 现在,您可以使用您的依赖项的黑客版本。
但说实话,我不认为这是一个好主意。 但也许在你的情况下它有助于/无法避免。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.