Maven 2在开发的实验/快速和粗糙的模型阶段快把我逼疯了。

我有一个pom.xml文件,它定义了我想要使用的web-app框架的依赖关系,我可以从该文件快速生成启动项目。然而,有时我想链接到一个尚未定义pom.xml文件的第三方库,因此我不会手动为第三方库创建pom.xml文件并安装它,并将依赖项添加到我的pom.xml中,我只想告诉Maven:“除了我定义的依赖项之外,还包括/lib中的任何jar。”

似乎这应该是简单的,但如果是,我错过了一些东西。

任何关于如何做到这一点的建议都非常感谢。除此之外,如果有一种简单的方法将maven指向/lib目录,并轻松地创建一个pom.xml,将所有附带的jar映射到一个依赖项,然后我可以将其命名为/ install并链接到它,这也足够了。


当前回答

您可以在项目上创建本地存储库

例如,如果你在项目结构中有libs文件夹

在libs文件夹中创建目录结构:/groupId/artifactId/version/artifactId-version.jar 在pom.xml中,您应该注册存储库 <库> <标识> ProjectRepo id > < / <名称> ProjectRepo < /名称> < url >文件:/ / $ {project.basedir} / libs url > < / < /库> 并像往常一样添加依赖项 < >的依赖 < groupId > groupId < / groupId > < artifactId > artifactId < / artifactId > <版本> > < /版本 < / >的依赖

仅此而已。

有关详细信息:如何在Maven中添加外部库(已存档)

其他回答

我只是想要一个快速而肮脏的变通办法……我不能从Nikita Volkov运行脚本:语法错误+它需要一个严格的jar名称格式。

我制作了这个Perl脚本,它与jar文件名的任何格式一起工作,它在xml中生成依赖项,这样它就可以直接复制粘贴到pom中。

如果你想使用它,确保你理解脚本在做什么,你可能需要改变lib文件夹和groupId或artifactId的值…

#!/usr/bin/perl

use strict;
use warnings;

open(my $fh, '>', 'dependencies.xml') or die "Could not open file 'dependencies.xml' $!";
foreach my $file (glob("lib/*.jar")) {
    print "$file\n";
    my $groupId = "my.mess";
    my $artifactId = "";
    my $version = "0.1-SNAPSHOT";
    if ($file =~ /\/([^\/]*?)(-([0-9v\._]*))?\.jar$/) {
        $artifactId = $1;
        if (defined($3)) {
            $version = $3;
        }
        `mvn install:install-file -Dfile=$file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=jar`;
        print $fh "<dependency>\n\t<groupId>$groupId</groupId>\n\t<artifactId>$artifactId</artifactId>\n\t<version>$version</version>\n</dependency>\n";
        print " => $groupId:$artifactId:$version\n";
    } else {
        print "##### BEUH...\n";
    }
}
close $fh;

使用<scope>system</scope>是一个很糟糕的想法,其他人已经解释了原因,手动将文件安装到本地存储库将使构建不可复制,并且使用<url>file://${project. bat。basedir}/repo</url>也不是一个好主意,因为(1)它可能不是一个格式良好的文件url(例如,如果项目签出在一个包含不寻常字符的目录中),(2)如果这个项目的POM被用作其他项目的依赖项,结果是不可用的。

假设您不愿意将工件上传到公共存储库,Simeon建议使用助手模块来完成这项工作。但现在有一个更简单的方法……

建议

使用non-maven-jar-maven-plugin。完全符合您的要求,没有其他方法的任何缺点。

对于那些在这里没有找到好的答案的人,这是我们正在做的事情,以获得一个包含所有必要依赖项的罐子。这个答案(https://stackoverflow.com/a/7623805/1084306)提到了使用Maven Assembly插件,但实际上并没有给出一个示例。如果你不从头到尾看完答案(答案相当长),你可能会错过它。将以下内容添加到pom.xml中将生成target/${PROJECT_NAME}-${VERSION}-jar-with-dependencies.jar

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- MainClass in mainfest make a executable jar -->
                <archive>
                  <manifest>
                    <mainClass>my.package.mainclass</mainClass>
                  </manifest>
                </archive>

            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <!-- bind to the packaging phase -->
                <phase>package</phase> 
                <goals>
                    <goal>single</goal>
                </goals>
              </execution>
            </executions>
        </plugin>

这并没有回答如何将它们添加到您的POM中,并且可能是一个无需动脑的问题,但只是将lib目录添加到您的类路径工作吗?我知道,当我需要一个外部jar,但又不想添加到Maven reppos时,我就会这样做。

希望这能有所帮助。

Java中scope='system'方法的解决方案:

public static void main(String[] args) {
        String filepath = "/Users/Downloads/lib/";
        try (Stream<Path> walk = Files.walk(Paths.get(filepath))) {

        List<String> result = walk.filter(Files::isRegularFile)
                .map(x -> x.toString()).collect(Collectors.toList());

                String indentation = "    ";
                for (String s : result) {
                    System.out.println(indentation + indentation + "<dependency>");
                    System.out.println(indentation + indentation + indentation + "<groupId>"
                            + s.replace(filepath, "").replace(".jar", "")
                            + "</groupId>");
                    System.out.println(indentation + indentation + indentation + "<artifactId>"
                            + s.replace(filepath, "").replace(".jar", "")
                            + "</artifactId>");
                    System.out.println(indentation + indentation + indentation + "<version>"
                            + s.replace(filepath, "").replace(".jar", "")
                            + "</version>");
                    System.out.println(indentation + indentation + indentation + "<scope>system</scope>");
                    System.out.println(indentation + indentation + indentation + "<systemPath>" + s + "</systemPath>");
                    System.out.println(indentation + indentation + "</dependency>");
                }

    } catch (IOException e) {
        e.printStackTrace();
    }
}