【问题标题】:Is there a way to create maven dependency in pom file from a local jar有没有办法从本地 jar 在 pom 文件中创建 maven 依赖项
【发布时间】:2020-01-24 05:38:32
【问题描述】:

有一个java项目有太多的jar文件,很难管理所有的jar文件。我知道有一种方法可以将此项目转换为 maven 项目,但我不想手动将项目中每个 jar 的依赖项添加到 pom 文件中。

有没有办法使用本地jar文件创建pom文件,这样pom将包含每个jar的依赖标签。

编辑:我想澄清一下,我不想将 jar 从本地存储库添加到本地存储库。 本地存储库方法对我不起作用,因为该项目将被不同系统的多个用户使用。

我想为我已经拥有的 jar 文件创建常规的 pom 依赖项以及 groupId、artifactId 和版本,以便在我将项目转换为 Maven 项目时可以将其复制粘贴到 pom 文件中。由于我的项目中有大量 JAR,因此我必须手动完成所有操作。

@Milen Dyankov 提供的解决方案对我有用。我能够从大多数 JAR 文件中获得所需的信息。

【问题讨论】:

  • 我认为您正在寻找的答案是here(重复)。
  • @GameDroids 不,op 不想枚举 pom 中的 jar;更不用说手动将罐子添加到本地.m2。有像 sbt 和 gradle 这样的工具可以让编写大量依赖项变得更容易。
  • 如果我理解正确,您想将所有本地 jar 文件自动导入到您的 pom 中吗?如果这是你想要的,我认为这是不可能的
  • 如果@AKMMM 是对的,即使有可能我也不会尝试...

标签: java maven jar pom.xml


【解决方案1】:

您可以手动将 JAR 安装到本地 Maven 存储库中

第一道

按照这里的步骤操作

mvn install:install-file -Dfile=<path-to-file>

在这里您可以在命令行中添加这些信息

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

&lt;path-to-file&gt; 提到要安装的 JAR 路径

&lt;group-id&gt; 提到要安装的 JAR 的组 id

&lt;artifact-id&gt; 提到要安装的 JAR 的工件 ID

&lt;version&gt;提到的JAR版本

第二种方式

您可以创建不同的本地 Maven 存储库

按照步骤进行

我们可以认为新的本地 Maven 存储库名为maven-repository,位于${basedir}(包含 pom.xml 的目录)。

在新的本地 maven 存储库中部署本地 JAR,如下所示,我正在关注

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true

您可以看到哪个 maven deploy:deploy-file 将工件安装在远程存储库中,但这里的存储库位于本地计算机中

安装 JAR 后,您需要在 pom.xml 文件中添加存储库

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>

【讨论】:

    【解决方案2】:

    AFAIK 没有 Maven 方法可以做到这一点。

    您可以编写一个脚本来计算每个 jar 的 SHA1 值并在您的 Maven 存储库中搜索该 jar。或者,它可以打开 jar 并搜索 pom.properties 以提取 Maven 坐标。

    无论如何,这可能是太多的编程工作,除非您需要为许多项目这样做。

    【讨论】:

      【解决方案3】:

      这段代码

          public static void main(String[] args) throws IOException {
              Set<String> missingMavenData = new HashSet<String>();
              String FOLDER = "/path/to/your/folder/with/jars";
      
              Files
               .walk(Paths.get(FOLDER), FileVisitOption.FOLLOW_LINKS)
               .map(Path::toFile)
               .filter(f -> f.isFile())
               .filter(f -> f.getName().endsWith(".jar"))
               .map(f -> {
                  try {
                      return new JarFile(f);
                  } catch (IOException e) {
                      e.printStackTrace();
                      return null;
                  }
               })
               .filter(Objects::nonNull)
               .map(jar -> {
                   Properties properties = null;
                   Enumeration<JarEntry> entries = jar.entries();
                   while (entries.hasMoreElements()) {
                       JarEntry jarEntry = entries.nextElement();
                       if (jarEntry.getName().matches("^META-INF/maven/.*/pom\\.properties$")) {
                           try {
                               properties = new Properties();
                               properties.load(jar.getInputStream(jarEntry));
                               break;
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       };
                   } 
                   if (properties == null) {
                       missingMavenData.add(jar.getName());
                   }
                   return properties;
               })
               .filter(Objects::nonNull)
               .forEach(properties -> {
                  System.out.println("<depencency>");
                  System.out.println("    <groupId>" + properties.getProperty("groupId")+ "</groupId>");
                  System.out.println("    <artifactId>" + properties.getProperty("artifactId")+ "</artifactId>");
                  System.out.println("    <version>" + properties.getProperty("version")+ "</version>");
                  System.out.println("</depencency>");
               });
      
              System.out.println("Those JAR files do not contain Maven metadata:");
              missingMavenData.forEach(System.out::println);
          }
      

      将遍历您的 jar 文件并尝试在其中找到 Maven 元数据。它将为拥有它的人生成 POM 条目,并列出那些没有它的人,以便您可以手动添加它。我希望这会有所帮助。

      更新:

      我将bom-helper:fromJars 目标添加到BOM Helper Maven Plugin,这与上面的代码或多或少是相同的。现在可以简单地添加插件

      <plugin>
          <groupId>com.commsen.maven</groupId>
          <artifactId>bom-helper-maven-plugin</artifactId>
          <version>0.2.0</version>
      </plugin>
      

      并将其配置为调用fromJars 目标。它也可以称为表单命令行。例如:

      mvn bom-helper:fromJars \
       -Dbom-helper.jarsFolder=/path/to/folder/with/jars \
       -Dbom-helper.inplace=true \
       -Dbom-helper.recursive
      

      将使用其中包含 Maven 元数据的所有 jar 的条目更新当前 pom。

      【讨论】:

      • 这对我有用。正是我想要的。处理了大部分 JAR 文件,让我的工作变得轻松多了。谢谢。
      【解决方案4】:

      您需要的是仅在您的服务器中可用的本地 maven nexus。考虑到您有仅供您使用的罐子,我想您有它们的来源。因此,您应该能够将它们转换为 maven 项目并将它们部署到您的本地连接中。修复结构后,maven 将在解析项目时下载您的依赖项和依赖项的依赖项。

      或者您可以使用&lt;scope&gt;system&lt;/scope&gt;,但这需要绝对路径,通常不建议使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        • 2013-07-19
        • 1970-01-01
        相关资源
        最近更新 更多