如何将源代码添加到 Maven 中的依赖库中?

发布于 2024-08-21 21:38:06 字数 184 浏览 2 评论 0原文

例如,我已将 junit-addons 包含在我的依赖项中:junit-addons。但是在maven仓库中没有任何源代码。我知道它存在(我已经下载了)。如何修改依赖项以便使用本地项目中的库而不是 Maven 存储库中的库(我会省略存储库中的 junit-addons 并使用本地 JAR 及其源代码)。

注意:我使用m2eclipse。

For instance, I have included into my dependencies junit-addons : junit-addons. But in the maven repository there isn't any source code. And I know it exists (I have downloaded it). How can I modify the dependencies in order to use libraries from my local project instead from the maven repository (I would omit the junit-addons from the respository and use a local JAR and its source code instead).

Note: I use m2eclipse.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

§对你不离不弃 2024-08-28 21:38:06

我以非常直接的方式解决了问题:

我已复制到文件夹 ${user.home}/.m2/repository/{group-name}/{artifactId}/{version}/ 遵循 MAVEN 标准的源文件:{artifactId}-{version}-sources.jar 它就像一个魅力! Eclipse 检查本地存储库并找到源。

不过,我不知道这是否是 MAVEN 的方式。

I've solved the problem in a very straight forward way:

I have copied into the folder ${user.home}/.m2/repository/{group-name}/{artifactId}/{version}/ the source file following MAVEN standard: {artifactId}-{version}-sources.jar and it works as a charm! Eclipse checks the local repository and finds the sources.

I don't know if this is the MAVEN way, though.

想你只要分分秒秒 2024-08-28 21:38:06

如何修改依赖项以便使用本地项目中的库而不是 Maven 存储库中的库

您不能。如果中央存储库中没有可用的源 JAR,只需将源放在文件系统上的某个文件夹、JAR 或 zip 中(您可以install:install-file 将源存档放在本地存储库,遵循 Maven 的约定)并设置 Eclipse 以使用它们(右键单击Package Explorer 中 Maven Dependencies 下的 JAR,选择 Java Source Attachment并设置位置路径)。

How can I modify the dependencies in order to use libraries from my local project instead from the maven repository

You can't. If a sources JAR isn't available in the central repository, just put the sources somewhere on your file system in a folder, JAR or zip (you could install:install-file a sources archive in your local repository, following Maven's conventions) and setup Eclipse to use them (right-click on the JAR under Maven Dependencies in the Package Explorer, select Java Source Attachment and setup the Location path).

清晰传感 2024-08-28 21:38:06

我使用免费版本的 Artifactory 存储库。我创建了一个 jar 文件 {artifactId}-{version}-sources.jar 并将其上传到存储库,并与二进制 jar 文件具有相同的组 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 中的源代码。

I use free version of Artifactory repository. I created a jar file {artifactId}-{version}-sources.jar and uploaded to the repository into the same group-id as binary jar file.
Then in my pom I added dependency:

<dependency>
            <groupId>mygroupid</groupId>
            <artifactId>artifact</artifactId>
            <version>1.0</version>
            <classifier>source</classifier>
        </dependency>

During maven build phase source jar was downloaded to my local repository.
I use netbeans 7.0 and it automatically managed everything for me. For example, right click on method and choosing go toSource correctly brings me to source code in the jar.

热血少△年 2024-08-28 21:38:06

将其下载到本地存储库后,您可以复制它。给它一个新的artifactId(例如libraryName-myVersion)并在pom.xml中添加依赖项。确保更改了 pom.xml 中的文件夹名称、jar 名称、pom 名称和 artifactId 本身。将所有内容存储在本地存储库中。现在您可以使用您的依赖项的破解版本。

但说实话,我不认为这是一个好主意。但也许它对你的情况有帮助/无法避免。

After you have downloaded it into your local repository, you could make a copy of it. Give it a new artifactId (e.g. libraryName-myVersion) and add dependencies in the pom. Make sure you change the folder names, jar names, pom names and the artifactId itself in the pom. Store everything in your local repository. Now you can use your hacked version of your dependency.

But to be honest, I do not thing this is a good idea to do. But maybe it helps/could not be avoided in your case.

行雁书 2024-08-28 21:38:06

您可以使用 安装文件 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>

You could use the install-file mojo to locally install artifacts into your local maven repository. If you want to share this artifact with others (say your team or another workstation), you could use your own repository manager (e.g. Nexus) and configure it as a mirror for any repository, e.g. central. Nexus will fetch (and cache) artifacts from central. Additionally, you may upload just about any artifact (like junit-addons sources) to your nexus installation.

In order to do configure a mirror, you'll have to edit ${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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文