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

其他回答

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

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

建议

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

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();
    }
}

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

对我来说最简单的就是配置你的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>

这是我们添加或安装本地jar的方法

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>iamajar</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/iamajar.jar</systemPath>
    </dependency>

我给出了一些默认的groupId和artifactId,因为它们是强制性的:)