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

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

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

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


当前回答

systemPath的问题是依赖项的jar不会作为可传递依赖项沿着工件分布。试试我在这里发布的内容:最好是将项目jar文件Mavenize,还是将它们放在WEB-INF/lib中?

然后像往常一样声明依赖项。

请阅读脚注。

其他回答

对于那些在这里没有找到好的答案的人,这是我们正在做的事情,以获得一个包含所有必要依赖项的罐子。这个答案(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>

这就是我所做的,它也围绕包的问题,它与签出的代码工作。

我在项目中创建了一个新文件夹,在我的情况下,我使用repo,但请随意使用src/repo

在我的POM中,我有一个不在任何公共专家存储库中的依赖项

<dependency>
    <groupId>com.dovetail</groupId>
    <artifactId>zoslog4j</artifactId>
    <version>1.0.1</version>
    <scope>runtime</scope>
</dependency>

然后,我创建了以下目录repo/com/dovetail/zoslog4j/1.0.1,并将JAR文件复制到该文件夹中。

我创建了下面的POM文件来表示下载的文件(这一步是可选的,但它删除了一个警告),并帮助下一个家伙找出我从哪里得到的文件。

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dovetail</groupId>
    <artifactId>zoslog4j</artifactId>
    <packaging>jar</packaging>
    <version>1.0.1</version>
    <name>z/OS Log4J Appenders</name>
    <url>http://dovetail.com/downloads/misc/index.html</url>
    <description>Apache Log4j Appender for z/OS Logstreams, files, etc.</description>
</project>

我创建的两个可选文件是POM的SHA1校验和和JAR,用于删除缺失的校验和警告。

shasum -b < repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.jar \
          > repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.jar.sha1

shasum -b < repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.pom \
          > repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.pom.sha1

最后,我将以下片段添加到pom.xml中,使我能够引用本地存储库

<repositories>
    <repository>
        <id>project</id>
        <url>file:///${basedir}/repo</url>
    </repository>
</repositories>

我发现了一个奇怪的解决办法:

使用Eclipse

创建简单的(非maven) Java项目 添加一个Main类 将所有罐子添加到类路径中 导出可运行JAR(这很重要,因为这里没有其他方法可以做到这一点) 选择将所需的库提取到生成的JAR中 决定牌照事宜 tadammm……将生成的jar安装到m2repo 将这个依赖项添加到其他项目中。

欢呼, Balint

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的东西)。