简体   繁体   English

有没有办法从本地 jar 在 pom 文件中创建 maven 依赖项

[英]Is there a way to create maven dependency in pom file from a local jar

There is a java project which has too many jar files and it has become difficult to manage all of them.有一个 java 项目有太多的 jar 文件,并且很难管理所有这些文件。 I know there is a way to convert this project to a maven project but I don't want to manually add the dependency for each jar in my project to the pom file.我知道有一种方法可以将此项目转换为 maven 项目,但我不想手动将项目中每个 jar 的依赖项添加到 pom 文件中。

Is there any way to create the pom file using the local jar files such that the pom will contain the dependency tag for each jar.有没有办法使用本地 jar 文件创建 pom 文件,这样 pom 将包含每个 jar 的依赖标记。

Edit: I want to clarify that I do not want to add the jars from/to the local repository.编辑:我想澄清一下,我不想将 jars 从本地存储库添加到本地存储库。 The local repository approach will not work for me as the project will be used by multiple users across different systems.本地存储库方法对我不起作用,因为该项目将由跨不同系统的多个用户使用。

I wanted to create regular pom dependency entries along with groupId, artifactId and version for the jar files which I already have so that I can copy-paste it into the pom file when I convert my project to a Maven project.我想为我已经拥有的 jar 文件创建常规 pom 依赖项以及 groupId、artifactId 和版本,以便在将项目转换为 Maven 项目时可以将其复制粘贴到 pom 文件中。 As I have a large number of JARs in my project, I would have to do it all manually.由于我的项目中有大量 JARs,我必须手动完成所有操作。

Solution provided by @Milen Dyankov worked for me. @Milen Dyankov 提供的解决方案对我有用。 I was able to get the information I needed from most of the JAR files.我能够从大多数 JAR 文件中获得所需的信息。

This code这段代码

    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("< dependency>");
            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("</dependency>");
         });
        
        System.out.println("Those JAR files do not contain Maven metadata:");
        missingMavenData.forEach(System.out::println);
    }

will iterate over your jar files and try to find the Maven metadata in them.将遍历您的 jar 文件并尝试在其中找到 Maven 元数据。 It will generate the POM entries for those who have it and will list those that don't have it so you can add it manually.它将为拥有它的人生成 POM 条目,并列出那些没有它的人,以便您可以手动添加它。 I hope this helps.我希望这有帮助。

UPDATE:更新:

I added bom-helper:fromJars goal to BOM Helper Maven Plugin which does more or less the same thing as the above code.我将bom-helper:fromJars目标添加到BOM Helper Maven 插件,它与上述代码或多或少做相同的事情。 One can now simply add the plugin现在可以简单地添加插件

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

and configure it to call fromJars goal.并将其配置为调用fromJars目标。 It can also be called form command line.它也可以称为表单命令行。 For example:例如:

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

will update current pom with entries for all jars that have Maven metadata in them.将使用所有 jars 的条目更新当前 pom,其中包含 Maven 元数据。

You can Install manually the JAR into your local Maven repository您可以手动将 JAR 安装到本地 Maven 存储库中

First Way第一种方式

Follow the steps here按照这里的步骤

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

And in here you can be able to add these information in command line在这里您可以在命令行中添加这些信息

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

<path-to-file> mentioned path to the JAR to install <path-to-file>提到要安装的 JAR 的路径

<group-id> mentioned group id of the JAR to install <group-id>提到要安装的 JAR 的组 id

<artifact-id> mentioned artifact id of the JAR to install <artifact-id>提到要安装的 JAR 的工件 id

<version> mentioned version of the JAR <version>提到版本 JAR

Second Way第二种方式

You can create a different local Maven repository您可以创建不同的本地 Maven 存储库

Follow the Steps按照步骤

we can consider the new local Maven repository is named maven-repository and is located in ${basedir} (the directory containing pom.xml).我们可以考虑新的本地 Maven 存储库名为maven-repository ,位于${basedir} (包含 pom.xml 的目录)中。

Deploying the local JARs in the new local maven repository as below i am following在新的本地 maven 存储库中部署本地 JARs,如下所示

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

you can see which the maven deploy:deploy-file installs the artifact in a remote repository but in here repository is located in the local machine您可以看到 maven deploy:deploy-file将工件安装在远程存储库中,但此处存储库位于本地计算机中

After installing the JARs your need to add the repository in your pom.xml file安装 JARs 后,您需要在 pom.xml 文件中添加存储库

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

AFAIK there is not Maven way to do this. AFAIK 没有 Maven 方法可以做到这一点。

You could write a script that computes the SHA1 value of each jar and searches for the jar in your Maven repository.您可以编写一个脚本来计算每个 jar 的 SHA1 值,并在您的 Maven 存储库中搜索 jar。 It could, alternatively, open the jars and search for the pom.properties to extract the Maven coordinates.或者,它可以打开 jars 并搜索pom.properties以提取 Maven 坐标。

In any way, this is probably too much programming effort unless you need to do it for many projects.无论如何,这可能是太多的编程工作,除非您需要为许多项目这样做。

What you need is your local maven nexus that is only available in your server.您需要的是仅在您的服务器中可用的本地 maven 关系。 Considering you have jars that are only available to you, I guess that you have sources for them.考虑到您有 jars 仅供您使用,我想您有它们的来源。 As a result, you should be able to convert them into maven projects and deploy them into your local nexus.因此,您应该能够将它们转换为 maven 项目并将它们部署到您的本地连接中。 After fixing the structure, maven will download your dependencies and the dependencies' dependencies when resolving a project.修复结构后,maven 将在解析项目时下载您的依赖项和依赖项的依赖项。

Or you could use <scope>system</scope> , but that requires absolute paths and is generally not suggested.或者您可以使用<scope>system</scope> ,但这需要绝对路径,通常不建议使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM