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

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

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

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


当前回答

在我们的项目中起作用的是Archimedes Trajano写的,但是我们在.m2/settings.xml中有这样的东西:

 <mirror>
  <id>nexus</id>
  <mirrorOf>*</mirrorOf>
  <url>http://url_to_our_repository</url>
 </mirror>

*应该改为central。因此,如果他的回答不适合您,您应该检查settings.xml

其他回答

Maven安装插件使用命令行将jar安装到本地存储库中,POM是可选的,但你必须指定GroupId, ArtifactId, Version和Packaging(所有POM的东西)。

仅用于丢弃代码

设置scope == system,只需要创建groupId, artifactId和version

<dependency>
    <groupId>org.swinglabs</groupId>
    <artifactId>swingx</artifactId>
    <version>0.9.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/swingx-0.9.3.jar</systemPath>
</dependency>

注意:系统依赖关系不会复制到生成的jar/war中 (参见如何在使用maven构建的war中包含系统依赖项)

对我来说最简单的就是配置你的maven-compiler-plugin来包含你的自定义jar。这个例子将加载lib目录中的任何jar文件。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <includes>
                    <include>lib/*.jar</include>
                </includes>
            </configuration>
        </plugin>

如果您想要一个快速而简单的解决方案,您可以执行以下操作(尽管我不建议将此方法用于测试项目之外的任何项目,maven会详细地抱怨这是不合适的)。

为您需要的每个jar文件添加一个依赖项,最好使用perl脚本或类似的东西,并将其复制/粘贴到pom文件中。

#! /usr/bin/perl

foreach my $n (@ARGV) {

    $n=~s@.*/@@;

    print "<dependency>
    <groupId>local.dummy</groupId>
    <artifactId>$n</artifactId>
    <version>0.0.1</version>
    <scope>system</scope>
    <systemPath>\${project.basedir}/lib/$n</systemPath>
</dependency>
";

我只是想要一个快速而肮脏的变通办法……我不能从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;