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

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

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

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


当前回答

这是我们添加或安装本地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,因为它们是强制性的:)

其他回答

我找到了另一种方法来做到这一点,看看这里从Heroku的帖子

总结一下(抱歉有些复制和粘贴)

在根文件夹下创建一个repo目录:

yourproject
+- pom.xml
+- src
+- repo

运行这个命令将jar安装到本地的repo目录

mvn deploy:deploy-file -Durl=file:///path/to/yourproject/repo/ -Dfile=mylib-1.0.jar -DgroupId=com.example -DartifactId=mylib -Dpackaging=jar -Dversion=1.0

添加到你的pom.xml:

> <存储库 <!——其他存储库,如果有的话——> <库> <标识> project.local id > < / 项目<名称> < /名称> < url >文件:$ {project.basedir} /回购url > < / < /库> < /存储库> < >的依赖 com . example < / groupId < groupId > > < artifactId > mylib < / artifactId > 1.0 <版本> < /版本> < / >的依赖

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

在与CloudBees的人员进行了长时间的讨论之后,他们提出了一个有趣的解决方案:

创建一个伪Maven项目,将一个预先存在的JAR附加为主要工件,运行到所属的POM安装:安装文件执行。这里有一个类似POM的例子:

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>image-util-id</id>
                    <phase>install</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <file>${basedir}/file-you-want-to-include.jar</file>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <packaging>jar</packaging>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

但要实现这一目标,必须改变现有的项目结构。首先,您应该记住,对于每种这样的JAR,都应该创建不同的伪Maven项目(模块)。并且应该创建一个父Maven项目,包括所有子模块,即:所有JAR包装器和现有的主项目。结构可以是:

根项目(此包含父POM文件,包括所有带有模块XML元素的子模块)(POM打包) JAR 1包装器Maven子项目(POM包装) JAR 2包装器Maven子项目(POM打包) 主要现有的Maven子项目(WAR, JAR, EAR ....包装)

当通过mvn:install或mvn:packaging运行父模块时,将强制执行子模块。这可能是一个缺点,因为项目结构应该改变,但最后提供了一个非静态的解决方案

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

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